Spring Cloud Bus整合Spring Cloud Config(基于Rabbitmq实现)

目录

1、config-server-github子项目

1)、启动类需要添加@EnableConfigServer和@EnableEurekaClient注解

2)、pom.xml中的maven依赖

3)、配置文件

3、config-client子项目

1)、启动类需要添加@EnableConfigServer和@EnableEurekaClient注解

2)、添加一个获取配置信息的Controller

3)、pom.xml中的maven依赖(与config-server-github一致)

4)、配置信息

3、启动服务(服务端和客户端)

1)、Rabbitmq界面信息

2)、使用消息总线,获取配置信息


    在实现消息总线之前需要先对Spring Cloud Config实现配置中心方式(可以参考:Spring Cloud Config三种配置服务)或者Spring Cloud Consul的配置中心(可以参考:Spring Cloud Consul配置中心)有一定的理解。所以当服务每一种服务应用或者多个应用都依赖于相同的配置,每个服务有几百台,当服务客户端每隔一定时间就去获取配置服务器的信息,那么配置服务器的压力依然是很大的。所以,基于消息方式的Spring Cloud Bus就有用了。

    我们再看一下,之前的配置服务器和配置客户端通信模式为:

    现在由于配置客户端(1,2...n)非常多,则基于消息中线的方式通信模型为:

    项目地址为:https://github.com/kevin-lihongmin/spring-cloud-project-kevin/tree/master/bus-demo下的spring-cloud-config服和config-server-github,直接将原来的spring-cloud-config服务中的config-server-github和config-client模块拿过来,进行一定的修改。 项目说明:

1、之前的Spring Cloud Config是使用客户端配置服务端的地址:spring.cloud.config.uri = http://127.0.0.1:9050方式进行拉起服务端配置 。

2、当前会将配置的客户端和服务端都注册到注册中心

1、config-server-github子项目

1)、启动类需要添加@EnableConfigServer和@EnableEurekaClient注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerGithubApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigServerGithubApplication.class, args);
	}
}

2)、pom.xml中的maven依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

3)、配置文件

    application.properties

# 服务名称
spring.application.name=config-server-github
# 配置服务端口
server.port=9510

    bootstrap.properties

# git的远程仓库
spring.cloud.config.server.git.uri = https://github.com/kevin-lihongmin/demo.git
# 是否强制从远程获取
spring.cloud.config.server.git.force-pull = true

ribbon.eureka.enabled=true
eureka.client.serviceUrl.defaultZone = http://127.0.0.1:8761/eureka/,\
http://127.0.0.1:8760/eureka/,http://127.0.0.1:8759/eureka/

# spring cloud bus 刷新配置
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.password=guest
spring.rabbitmq.username=guest
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
spring.cloud.bus.virtual-host=/

# bus-refresh 或者 bus-env
management.endpoints.web.exposure.include=bus-refresh

3、config-client子项目

1)、启动类需要添加@EnableConfigServer和@EnableEurekaClient注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigClientApplication.class, args);
	}
}

2)、添加一个获取配置信息的Controller

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 *
 * {@link RefreshScope } 开启更新功能
 *
 * @author kevin
 * @date 2019/5/24 11:09
 * @since
 */
@RefreshScope
@RestController
public class ConfigController {

    @Value("${kevinName}")
    private String kevinName;

    @GetMapping("kevinName")
    public String getProperties() {
        return kevinName;
    }
}

3)、pom.xml中的maven依赖(与config-server-github一致)

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.1.1.RELEASE</version>
</dependency>

4)、配置信息

    application.properties

server.port=9511
# 配置客户端应用名称
spring.application.name=config-client

# 启用端点 env
management.endpoint.env.enabled=true
# 暴露端点 env 配置多个,隔开
management.endpoints.web.exposure.include=*

    bootstrap.properties

# 关联的{application}, 如果该名称没有配置,则直接或者spring.application.name
spring.cloud.config.name = config-client
# 关联的{profile}
spring.cloud.config.profile = test
# 关联的{label}
spring.cloud.config.label = master
# 配置服务器的Uri(这里进行关闭),但是需要配置 spring.cloud.config.discovery.serviceId
# spring.cloud.config.uri = http://127.0.0.1:9050

spring.cloud.config.discovery.enabled=true
# 获取配置的服务
spring.cloud.config.discovery.serviceId=config-server-github

ribbon.eureka.enabled=true
eureka.client.serviceUrl.defaultZone = http://127.0.0.1:8761/eureka/,\
http://127.0.0.1:8760/eureka/,http://127.0.0.1:8759/eureka/

# spring cloud bus 刷新配置
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.password=guest
spring.rabbitmq.username=guest
spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
spring.cloud.bus.virtual-host=/

# bus-refresh 或者 bus-env
management.endpoints.web.exposure.include=bus-refresh

3、启动服务(服务端和客户端)

1)、Rabbitmq界面信息

    启动服务端和客户端后,在rabbitmq平台上可以看见注册的connections、exchange、queue如下:

2)、使用消息总线,获取配置信息

1、在浏览器中输入http://localhost:9511/kevinName获取客户端的配置信息,如下:

2、在github中修改文件(配置服务器的git地址)

3、使用postman的Post请求,在浏览器中输入http://127.0.0.1:9511/actuator/refresh刷新客户端

配置客户端日志信息如下:

2019-06-13 17:05:24.130  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Unregistering ...
2019-06-13 17:05:24.136  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/192.168.31.217:config-client:9511 - deregister  status: 200
2019-06-13 17:05:24.145  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient
2019-06-13 17:05:24.146  INFO 11172 --- [nio-9511-exec-4] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2019-06-13 17:05:24.148  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2019-06-13 17:05:24.153  INFO 11172 --- [nio-9511-exec-4] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON encoding codec LegacyJacksonJson
2019-06-13 17:05:24.153  INFO 11172 --- [nio-9511-exec-4] c.n.d.provider.DiscoveryJerseyProvider   : Using JSON decoding codec LegacyJacksonJson
2019-06-13 17:05:24.153  INFO 11172 --- [nio-9511-exec-4] c.n.d.provider.DiscoveryJerseyProvider   : Using XML encoding codec XStreamXml
2019-06-13 17:05:24.153  INFO 11172 --- [nio-9511-exec-4] c.n.d.provider.DiscoveryJerseyProvider   : Using XML decoding codec XStreamXml
2019-06-13 17:05:24.203  INFO 11172 --- [nio-9511-exec-4] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2019-06-13 17:05:24.203  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2019-06-13 17:05:24.203  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2019-06-13 17:05:24.203  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2019-06-13 17:05:24.203  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Application is null : false
2019-06-13 17:05:24.203  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2019-06-13 17:05:24.203  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2019-06-13 17:05:24.203  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2019-06-13 17:05:24.206  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : The response status is 200
2019-06-13 17:05:24.206  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2019-06-13 17:05:24.207  INFO 11172 --- [nio-9511-exec-4] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2019-06-13 17:05:24.208  INFO 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1560416724208 with initial instances count: 2
2019-06-13 17:05:24.209  INFO 11172 --- [nio-9511-exec-4] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application CONFIG-CLIENT with eureka with status DOWN
2019-06-13 17:05:24.209  WARN 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1560416724209, current=DOWN, previous=STARTING]
2019-06-13 17:05:24.209  INFO 11172 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/192.168.31.217:config-client:9511: registering service...
2019-06-13 17:05:24.210  INFO 11172 --- [nio-9511-exec-4] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application CONFIG-CLIENT with eureka with status UP
2019-06-13 17:05:24.210  WARN 11172 --- [nio-9511-exec-4] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1560416724210, current=UP, previous=DOWN]
2019-06-13 17:05:24.210  INFO 11172 --- [nio-9511-exec-4] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application UNKNOWN with eureka with status DOWN
2019-06-13 17:05:24.210  INFO 11172 --- [nio-9511-exec-4] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application UNKNOWN with eureka with status UP
2019-06-13 17:05:24.214  INFO 11172 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/192.168.31.217:config-client:9511 - registration status: 204
2019-06-13 17:05:24.214  INFO 11172 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/192.168.31.217:config-client:9511: registering service...
2019-06-13 17:05:24.216  INFO 11172 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONFIG-CLIENT/192.168.31.217:config-client:9511 - registration status: 204

4、再次刷新浏览器http://localhost:9511/kevinName

猜你喜欢

转载自blog.csdn.net/it_lihongmin/article/details/91466638