创建config-bus-server
- 引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
- 创建启动类
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigBusServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ConfigBusServerApplication.class)
.web(WebApplicationType.SERVLET)
.run(args);
}
}
- 新增application.yml配置文件
server:
port: 60002
spring:
application:
name: config-bus-server
rabbitmq:
host: 43.142.15.107
port: 5672
username: guest
password: guest
cloud:
config:
server:
git:
uri: https://github.com/afterglow-now/config-repo.git
force-pull: true
skipSslValidation: true
eureka:
instance:
preferIpAddress: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
service-url:
defaultZone: http://localhost:20000/eureka/
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
- 新增bootstrap.yml配置文件(加密相关)
encrypt:
key: clay
创建config-bus-client
- 引入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-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
- 创建启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigBusClientApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ConfigBusClientApplication.class)
.web(WebApplicationType.SERVLET)
.run(args);
}
}
- yml配置文件
server:
port: 61001
spring:
application:
name: config-bus-client
rabbitmq:
host: 43.142.15.107
port: 5672
username: guest
password: guest
cloud:
stream:
default-binder: rabbit
config:
name: config-consumer
discovery:
enabled: true
service-id: config-bus-server
profile: dev
label: main
eureka:
instance:
preferIpAddress: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}
client:
service-url:
defaultZone: http://localhost:20000/eureka/
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
myWord: ${word}
进行验证
- 将config-bus-client启动多份
- 访问两个连接验证结果
- 对配置中心进行配置刷新
- 访问2点进行验证
- 将端口改为其中一个config-client的节点,对节点进行单独刷新,其他节点不受影响