《重新定义Spring Cloud实战》——3.5 Eureka实战

举报
华章计算机 发表于 2019/06/04 15:06:32 2019/06/04
【摘要】 本书摘自《重新定义Spring Cloud实战》——书中第3章,第3.5.1节,作者是许进、叶志远、钟尊发、蔡波斯、方志朋、郭芳碧、朱德明。

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。


【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。