目录
一、版本介绍
SpringBoot版本2.1.9;SpringCloud版本Greenwich.SR3
二、Eureka介绍
Spring Cloud Eureka 是使用Netfix Eureka 来实现服务注册和发现,其不仅包含服务端也包含客户端。
Eureka服务端,也称为服务注册中心,
三、搭建服务注册中心
搭建Springboot工程,引入依赖如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
通过@EnableEurekaServer注解启动服务注册中心提供给其他应用进行对话,代码如下:
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
默认情况下,该服务注册中心也会将自己作为客户端来尝试注册它自己,因此我们要禁用这种行为;而且注册中心的职责是维护服务实例,不需要去检索服务;所以需要在application.properties设置如下:
server.port=8888
eureka.instance.hostname=localhost
#禁用注册自己
eureka.client.register-with-eureka=false
#禁用检索
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
启动之后访问:http://localhost:8888/ ;就可以查看到Eureka的信息板,如下图:
四、注册服务提供者
构建springBoot应用加入到Eureka的服务治理体系中。
在新工程中引入Eureka依赖和web依赖,如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
创建IndexController,接口index,通过注入DiscoveryClient对象,在启动类中增加注解@EnableDiscoveryClient,并打印服务信息;如下:
@RestController
@ResponseBody
public class IndexController {
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/index",method = RequestMethod.GET)
public String index(){
List<ServiceInstance> indexService = client.getInstances("indexService");
for (ServiceInstance ServiceInstance :
indexService) {
System.out.println("host:"+ServiceInstance.getHost()+"serviceId:"+ServiceInstance.getServiceId()+
"scheme:"+ServiceInstance.getScheme()+"instanceId:"+ServiceInstance.getInstanceId());
}
return "Hello Word";
}
}
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
还需要在application.properties 文件中为服务命名通过spring.application,并通过eureka.client.service-url.defaultZone指定注册中心地址;如下:
server.port=8080
spring.application.name=indexService
eureka.client.service-url.defaultZone = http://localhost:8888/eureka/
服务启动后就可以访问http://localhost:8888/ 查看到 Instances currently registered with Eureka一栏中有服务的注册信息;如下图:
五、高可用注册中心
在微服务架构这样的分布式环境中,我们要考虑发生故障的情况,所以在部署注册中心服务是必须要高可用,即搭建高可用服务注册中心的集群。EurekaServer的设计中,所有节点既是服务提供方,也是服务消费方,注册中心也一样。我们之前设置eureka.client.register-with-eureka=false #禁用检索 eureka.client.fetch-registry=false参数就是为了不让服务注册自己。高可用的EurekaServer实际就是将自己作为服务向其他注册中心注册自己,这样就可以额形成一组互相注册的服务注册中心,以实现服务清单的互相让步,达到高可用的效果。
创建application-peer1.properties,作为peer1服务中心的配置,并将eureka.client.service-url.defaultZone指向peer2;如下:
spring.application.name=eurekaServer
server.port=9999
eureka.instance.hostname=peer1
eureka.client.service-url.defaultZone=http://peer2:1111/eureka/
创建application-peer2.properties,作为peer1服务中心的配置,并将eureka.client.service-url.defaultZone指向peer1;如下:
spring.application.name=eurekaServer
server.port=1111
eureka.instance.hostname=peer2
eureka.client.service-url.defaultZone=http://peer1:9999/eureka/
在C:\Windows\System32\drivers\etc目录下的hosts文件中添加对peer1和peer2的转换;增加如下:
127.0.0.1 peer1
127.0.0.1 peer2
再通过spring.profiles.active属性来分别启动peer1和peer2:
##*号代表你的包名
java -jar ******* --spring.profiles.active=peer1
java -jar ******* --spring.profiles.active=peer2
访问http://localhost:9999/或者http://localhost:1111/ 就可以看到registered-replicas有了peer2或者peer1节点的服务了,如下
通过在服务提供方的配置文件中增加如下配置,既可以将服务注册到Eureka集群中:
server.port=8080
spring.application.name=indexService
eureka.client.service-url.defaultZone =http://peer1:9999/eureka/,http://peer2:1111/eureka/
再次访问http://localhost:9999/或http://localhost:1111/既可以看到如下图,服务indexService被注册到了peer1和peer2中。
六、服务发现与消费
首先我们用命令启动端口号分别为8081和8082的indexService服务;
Java -Jar *******.jar --server.port=8081
Java -Jar *******.jar --server.port=8082
创建工程来实现服务消费者,取名为ribbon-consumer,引入依赖web、eureka和ribbon;pom文件如下:
<dependencies>
<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-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
在主类上增加@EnableDiscoveryClient注解,使该应用注册为客户端;同时在该主类中创建RestTemplate的springbean实例,并通过 @LoadBalanced注解开启客户端负载均衡。
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
};
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
创建ConsumerController类,并用restTemplate调用indexService服务;代码如下:
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
public String helloConsumer(){
return restTemplate.getForEntity("http://indexService/index",String.class).getBody();
}
}
在application.properties中配置eureka服务注册中心的位置,需要和indexService一样;如下:
spring.application.name=ribbon-consumer
server.port=9000
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
启动ribbon-consumer服务后,访问http://localhost:1111/可以看到该服务的信息;
通过向http://localhost:9000/ribbon-consumer发起请求,会成功返回helloWord 。并在ribbon-consumer服务的控制台看到如下信息:
在两个indexService的控制台会交替打印一些信息: