《重新定义Spring Cloud实战》——3.5 Eureka实战
3.5 Eureka实战
3.5.1 Eureka Server在线扩容
(1)准备工作
由于我们需要动态去修改配置,因此这里引入config-server工程,如代码清单3-1所示。
代码清单3-1 ch3-1\ch3-1-config-server\pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
启动类如代码清单3-2所示:
代码清单3-2 ch3-1\ch3-1-config-server\src\main\java\cn\springcloud\book\Ch31Config ServerApplication.java
@SpringBootApplication
@EnableConfigServer
public class Ch31ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(Ch31ConfigServerApplication.class, args);
}
}
配置文件如代码清单3-3所示:
代码清单3-3 ch3-1\ch3-1-config-server\src\main\resources\bootstrap.yml
spring:
application:
name: config-server
profiles:
active: native
server:
port: 8888
这里为了简单演示,我们使用native的proflie,即使用文件来存储配置,默认放在resources\config目录下。
另外由于要演示Eureka Server的动态扩容,这里还建立了一个eureka-server工程及eureka-client工程,分别见ch3-1\ch3-1-eureka-server、ch3-1\ch3-1-eureka-client。与第2章的工程的区别在于这里引入了spring-cloud-starter-config,另外两个工程都添加了一个QueryController用于实验,如代码清单3-4所示。
代码清单3-4 ch3-1\ch3-1-eureka-server\src\main\cn\springcloud\book\controller\QueryController.java
@RestController
@RequestMapping("/query")
public class QueryController {
@Autowired
EurekaClientConfigBean eurekaClientConfigBean;
@GetMapping("/eureka-server")
public Object getEurekaServerUrl(){
return eurekaClientConfigBean.getServiceUrl();
}
}
(2)1个Eureka Server
eureka-client的配置文件如代码清单3-5所示:
代码清单3-5 ch3-1\ch3-1-config-server\src\main\resources\config\eureka-client.yml
server:
port: 8081
spring:
application:
name: eureka-client1
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # one eureka server
eureka-server的配置文件如代码清单3-6所示:
代码清单3-6 ch3-1\ch3-1-config-server\src\main\resources\config\eureka-server-peer1.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # one eureka server
server
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
然后分别启动config-server、eureka-server(使用peer1的profile)、eureka-client,打开localhost:8761,可以观察注册的实例。
(3)2个Eureka Server
现在我们开始把Eureka Server的实例扩充一下,在ch3-1-eureka-server工程目录下,使用peer2的profile启动第二个Eureka Server,命令行如下:
mvn spring-boot:run -Dspring.profiles=active=peer2
其配置文件如代码清单3-7所示:
代码清单3-7 ch3-1\ch3-1-config-server\src\main\resources\config\eureka-server-peer2.yml
server:
port: 8762
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/ # two eureka server
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
第二个Eureka Server启动起来之后,要修改eureka-client.yml、eureka-server-peer1.yml的配置文件:
其中eureka-client.yml变为:
server:
port: 8081
spring:
application:
name: eureka-client1
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/ # two eureka server
修改eureka.client.serviceUrl,新增第二个Eureka Server的地址。
eureka-peer1.yml变为:
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8762/eureka/ # two eureka server
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
修改eureka.client.serviceUrl,指向第二个Eureka Server。
之后就是重启config-server,使配置生效。然后使用如下命令分别刷新eureka-client以及eureka-server-peer1,加载新配置:
~ curl -i -X POST localhost:8761/actuator/refresh
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 20 Jun 2018 11:10:10 GMT
["eureka.client.serviceUrl.defaultZone"]%
~ curl -i -X POST localhost:8081/actuator/refresh
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 20 Jun 2018 11:10:17 GMT
["eureka.client.serviceUrl.defaultZone"]%
之后分别调用queryController的方法,如下:
~ curl -i http://localhost:8761/query/eureka-server
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 20 Jun 2018 11:12:07 GMT
{"defaultZone":"http://localhost:8762/eureka/"}%
→ ~ curl -i http://localhost:8081/query/eureka-server
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 20 Jun 2018 11:12:15 GMT
{"defaultZone":"http://localhost:8761/eureka/,http://localhost:8762/eureka/"}%
可以看到Eureka Client端已经成功识别到两个Eureka Server,而原来peer1的Eureka Server请求的eureka.client.serviceUrl也指向了peer2,Eureka Server扩容成功。
(4)3个Eureka Server
将Eureka Server扩容到3个实例的话,这里使用peer3配置,如代码清单3-8所示。
代码清单3-8 ch3-1\ch3-1-config-server\src\main\resources\config\eureka-server-peer3.yml
server:
port: 8763
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/ # three eureka server
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
启动命令如下:
mvn spring-boot:run -Dspring.profiles.active=peer3
接下来修改eureka-client.yml配置,如下所示:
server:
port: 8081
spring:
application:
name: eureka-client1
eureka:
client:
serviceUrl:
defaultZone:
http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost: 8763/eureka/ # three eureka server
这里新增了peer3的Eureka Server地址。
修改eureka-server-peer1.yml,如下所示:
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/ # three eureka server
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
其eureka.client.serviceUrl.defaultZone指向了peer2和peer3。
修改eureka-server-peer2.yml的配置如下:
server:
port: 8762
eureka:
instance:
hostname: localhost
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/ # three eureka server
server:
waitTimeInMsWhenSyncEmpty: 0
enableSelfPreservation: false
其eureka.client.serviceUrl.defaultZone指向了peer1和peer3。
接下来重启config-server,然后分别刷新Eureka Client、Eureka Server的peer1和peer2的配置,如下所示:
~ curl -i -X POST http://localhost:8081/actuator/refresh
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 20 Jun 2018 11:24:57 GMT
["eureka.client.serviceUrl.defaultZone"]%
→ ~ curl -i -X POST http://localhost:8761/actuator/refresh
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 20 Jun 2018 11:25:17 GMT
["eureka.client.serviceUrl.defaultZone"]%
→ ~ curl -i -X POST http://localhost:8762/actuator/refresh
HTTP/1.1 200
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 20 Jun 2018 11:25:36 GMT
["eureka.client.serviceUrl.defaultZone"]%
之后分别访问各个实例的/query/eureka-server,可以看到Eureka Server的列表已经成功变为3个,扩容成功。
本小节举例的Eureka Server在线扩容,需要依赖配置中心的动态刷新功能,具体的就是/actuator/refresh这个endpoint。这里为了方便,使用的config-server是native的profile,因此修改后重启才生效,如果是使用git仓库,则无须重启config-server。
- 点赞
- 收藏
- 关注作者
评论(0)