配置高可用注册中心
Eureka Server 的高可用就是指将服务注册中心本身向其他服务注册中心注册自己,这样就可以实现服务清单的同步,增强系统可用性,而不是单节点的服务注册中心。
在学习笔记一里,我们设置过如下两个参数
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
既然需要向其他服务中心注册自己,那么这两个参数我们就不需要配置,默认true。
我们在学习笔记一的基础上,构建一个双节点的注册中心集群。
分别创建application-peer1.properties和application-peer2.properties两个配置文件,配置内容如下:
peer1:
server.port=1130
spring.application.name=eureka-server
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2:1131/eureka/
peer2:
server.port=1131
spring.application.name=eureka-server
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1:1130/eureka/
这里peer1和peer2分别向对方注册自己。为了让上面的host形式的URL能正确访问到,我们需要添加对peer1和peer2的转换。
linux在/etc/hosts文件,windows在c:\windows\system32\drivers\etc\hosts文件
127.0.0.1 peer1
127.0.0.1 peer2
然后利用Profile配置分别启动这两个服务,
java -jar eureka-server-1.0.jar --spring.profiles.active=peer1
java -jar eureka-server-1.0.jar --spring.profiles.active=peer2
启动之后我们打开localhost:1130,效果如下:
可以看到在peer1的注册节点中,已经有了peer2。
至于服务提供方,我们还是以笔记一种的hello-service为例,只要改动如下配置
eureka.client.serviceUrl.defaultZone=http://peer1:1130/eureka/,http://peer2:1131/eureka/
可以观察到,此时hello-service同时注册到了两个服务中心。
服务发现与消费
现在已经有了服务注册中心与服务提供者,接下来我们应该做的就是发现服务并消费服务。创建一个Spring Boot工程,名为ribbon-consumer,在pom文件中引入:
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</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>
在应用主类中添加如下代码:
@EnableDiscoveryClient // 让应用注册为Eureka客户端应用,以获得发现服务的能力,
@SpringBootApplication
public class RibbonConsumerApplication {
@Bean
@LoadBalanced // 开启客户端负载均衡
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
}
创建ConsumerController类
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
public String helloConsumer() {
return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
}
}
其中访问路径为http://HELLO-SERVICE/hello 而不是具体的IP地址,这也符合我们的预期要求,符合服务治理的概念。
在application.properties在配置如下:
spring.application.name=ribbon-consumer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://peer1:1130/eureka/
启动应用之后,我们可以在1130与1131中都可以看到当前的注册服务,这与我们的高可用服务注册中心相关
此时,我们通过http://localhost:9000/ribbon-consumer访问,将会得到信息的返回。这个适合查看ribbon-consumer的控制台,我们可以查看到当前客户端维护的HELLO-SERVICE服务列表,以及其他一些也极为有用的信息。
我们再多请求几次,就会发现1993 1994两个HELLO-SERVICE的控制台,会交替打印输出日志,这也证明了当前客户端的调用是基于负载均衡的,我们关闭了其中一个服务(1993),会发现,客户端仍然能有效调用HELLO-SERVICE。