使用Eureka实现服务治理
作用:实现服务治理(服务注册与发现)
简介:
Spring Cloud Eureka是Spring Cloud Netflix项目下的服务治理模块。而Spring Cloud Netflix项目是Spring Cloud的子项目之一,主要内容是对Netflix公司一系列开源产品的包装,它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。
项目实战:
服务注册中心 :eureka-server
作用:服务注册中心提供服务注册功能
服务提供方:eureka-client
作用:注册服务到服务注册中心
服务注册中心 :eureka-server
新建一个springboot项目:eureka-server,其pom.xml配置如下:
1 2 UTF-8 3 UTF-8 4 1.8 5 6 7 8 org.springframework.cloud 9 spring-cloud-starter-eureka-server10 11 12 13 14 15 org.springframework.cloud16 spring-cloud-dependencies17 Dalston.SR118 pom19 import20 21 22
想要实现一个服务注册中心的功能非常简单,只需要在项目的启动类EurekaServerApplication上使用@EnableEurekaServer注解即可
1 @EnableEurekaServer2 @SpringBootApplication3 public class EurekaServerApplication{4 5 public static void main(String[] args) {6 new SpringApplicationBuilder(EurekaServerApplication.class)7 .web(true).run(args);8 }9 }
默认情况下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在application.properties配置文件中增加如下信息:
1 spring.application.name=eureka-server2 server.port=10013 eureka.instance.hostname=localhost4 eureka.client.register-with-eureka=false5 eureka.client.fetch-registry=false
启动EurekaServerApplication,访问 http://localhost:9001/可以看到Eureka的页面,从红框的位置可以看到没有任务服务实例注册到当前的服务注册中心
服务提供方 :eureka-client
每一个实例注册之后需要向注册中心发送心跳,当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。
新建一个springboot项目:eureka-client,其pom.xml配置如下:
1 2 UTF-8 3 UTF-8 4 1.8 5 6 7 8 org.springframework.cloud 9 spring-cloud-starter-eureka10 11 12 org.springframework.boot13 spring-boot-starter-web14 15 16 17 18 19 org.springframework.cloud20 spring-cloud-dependencies21 Dalston.SR122 pom23 import24 25 26
想要实现一个服务提供方也很简单,只要在项目的启动类EurekaClientApplication上使用@EnableEurekaClient注解即可
1 @EnableEurekaClient 2 @SpringBootApplication 3 public class EurekaClientApplication { 4 5 public static void main(String[] args) { 6 new SpringApplicationBuilder( 7 EurekaClientApplication.class) 8 .web(true).run(args); 9 }10 }
在application.properties中进行如下配置
spring.application.name=eureka-clientserver.port=9002eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka/
通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。
eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。
使用server.port属性设置不同的端口。
启动EurekaClientApplication类
刷新 http://localhost:9001/,可以看到咱们的服务提供方已经注册到了服务注册中心
在新建一个DiscoveryController
使用discoveryClient.getServices()获取已经注册的服务名,使用@value将配置文件中的信息赋值到ip
@RestControllerpublic class DiscoveryController { @Autowired private DiscoveryClient discoveryClient; @Value("${server.port}") private String ip; @GetMapping("/client") public String client() { String services = "Services: " + discoveryClient.getServices()+" ip :"+ip; System.out.println(services); return services; }}
访问:http://localhost:9002/client
最后说明一下@EnableEurekaClient 与@EnableDiscoveryClient这两个注解
首先这个两个注解都可以实现服务发现的功能,在spring cloud中discovery service有许多种实现(eureka、consul、zookeeper等等)
@EnableEurekaClient基于spring-cloud-netflix。服务采用eureka作为注册中心,使用场景较为单一。
@EnableDiscoveryClient基于spring-cloud-commons。服务采用其他注册中心。