一,SpringCloud微服务:创建服务注册中心

一,什么是微服务架构

   将一个完整的系统,按各功能拆分开来并独立于各自服务器上的单元。该单元就是微服务。

   那么什么是“微服务架构”呢?简单的说,微服务架构就是将一个完整的应用从数据存储开始垂直拆分成多个不同的服务,每个服       务都能独立部署、独立维护、独立扩展,服务与服务间通过诸如RESTful API的方式互相调用。

二,SpringCloud简介

  SpringCloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理,服务发现,断路器,智能路由,微代理,控制总线,全局锁,决策竞选,分布式会话和集群状态管理等操作提供了一种简单的开发方式。

三,服务治理

  服务治理框架:Netflix Eureka,Consul,Zookeeper。

四,Spring Cloud Eureka

  Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。而Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速地在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),只能路由(Zuul),客户端负载均衡(Ribbon)等。

五,使用Spring Cloud Eureka实现服务治理。

  5.1创建“服务注册中心”

  (1)创建一个springboot工程,命名为eureka-server,并在pom.xml中引入需要的依赖内容。



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.3.RELEASE
		 
	
	com.spring
	boot
	0.0.1-SNAPSHOT
	eureka-server
	Demo project for Spring Boot

	
		1.8
		Greenwich.RELEASE
	

	
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-server
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

	
		
			spring-milestones
			Spring Milestones
			https://repo.spring.io/milestone
		
	

(2)通过@EnableEurekaServer 注解启动一个服务注册中心提供给其他应用进行对话。操作如下

 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}

}

(3),修改配置文件

server.port=8081
eureka.instance.hostname = localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry = false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

  eureka.c口ent.register-with-eureka: 由于该应用为注册中心,所以设置为 false, 代表不向注册中心注册自己。

  eureka.client.fetch-registry: 由于注册中心的职责就是维护服务实例,它并不需要去检索服务, 所以也设置为 false 。

(4),访问服务注册中心

  http://localhost:8081/eureka/

结果如下:

一,SpringCloud微服务:创建服务注册中心_第1张图片

 致此,一个服务注册中心便创建成功。

你可能感兴趣的:(springCloud)