Spring Cloud是一个基于Spring Boot实现的微服务架构开发工具。它为微服务架构中设计的配置管理、服务治理、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话、集群状态管理等操作提供了一种简单快速的开发方式。
随着近年来微服务越来越普及,学习一下Spring Cloud还是很有必要的。

一、版本依赖
在使用不同的Spring Cloud版本时,需要注意Spring Boot的版本,否则会出现各种不兼容的问题。本次学习笔记均采用Spring Boot 2.0.5.RELEASE+Spring Cloud Finchley.RELEASE。

二、搭建服务注册中心
创建一个基础的Spring Boot工程,命名为eureka-server,在pom中引入如下starter:

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

在早起的Spring Cloud版本中,是引入spring-cloud-starter-eureka-server,此处需要将spring-cloud-starter-eureka-server 改成spring-cloud-starter-netflix-eureka-server。
通过@EnableEurekaServer , 启动一个服务注册中心提供给其他应用进行对话,如下代码即可:

@EnableEurekaServer // 启动一个服务注册中心提供给其他应用进行对话
@SpringBootApplication
public class EurekaServerApplication {

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

在默认情况下,服务注册中心也会将自己作为客户端来注册自己,此处我们需要禁止该行为,只需要在application.properties中添加如下配置:

server.port=1130

eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

其中
eureka.client.registerWithEureka=false表示禁止服务注册中心注册自己
eureka.client.fetchRegistry=false表示注册中心不需要去检索服务
完成如上配置之后,启动应用,访问 http://localhost:1130 出现如下界面
Spring Cloud 学习笔记一(服务治理)
因为现在还没有任何服务注册,所以在 Instances currently registered with Eureka这一栏为空。

三、创建注册服务提供者
上面完成了服务注册中心,这里我们尝试创建一个微服务加入到Eureka的服务治理体系中,向服务注册中心注册服务。
创建一个基础的Spring Boot工程,命名为eureka-client,在pom中引入如下starter:

        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>


	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

创建一个restful接口:

@RestController
public class HelloController {
	// private Logger logger = Logger.getLogger(getClass());

	@Autowired
	private Registration registration; // 服务注册

	@Autowired
	private DiscoveryClient client;// 服务发现客户端

	@Value("${server.port}")
	private String port;

	@RequestMapping(value = "/hello", method = RequestMethod.GET)
	public String index() {
		ServiceInstance instance = serviceInstance();
		// System.out.println("/hello,host:" + instance.getHost() + ",service_id:" +
		// instance.getServiceId());
		return "/hello,host:" + instance.getHost() + ",service_id:" + instance.getServiceId();
	}

	public ServiceInstance serviceInstance() {
		List<ServiceInstance> list = client.getInstances(registration.getServiceId());
		if (list != null && list.size() > 0) {
			for (ServiceInstance itm : list) {
				if (itm.getPort() == Integer.parseInt(port))
					return itm;
			}
		}
		return null;
	}

}

然后同样的,通过@EnableEurekaClient注解,自动配置向服务中心注册服务。

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {

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

当然,还是需要在application.properties中配置

server.port=1993

spring.application.name=hello-service

eureka.client.serviceUrl.defaultZone=http://localhost:1130/eureka/

其中
spring.application.name=hello-service将服务命名为hello-service
eureka.client.serviceUrl.defaultZone=http://localhost:1130/eureka/指定了服务注册中心的地址

将应用启动后,在eureka-server控制台中,我们能看到如下输出
Spring Cloud 学习笔记一(服务治理)
在eureka-client控制台中,我们能看到如下输出
Spring Cloud 学习笔记一(服务治理)
通过访问刚刚的 http://localhost:1130 地址,在Eureka的信息面板中,我们也能看到hello-service服务注册成功
Spring Cloud 学习笔记一(服务治理)

另外访问 http://localhost:1993/hello 将会返回
Spring Cloud 学习笔记一(服务治理)
这里的输出内容就是我们在DiscoveryClient接口对象中,从服务注册中心获取的服务相关信息。

分类:

技术点:

相关文章: