SpringCloudConfig
在远程仓库创建yml
在新建的仓库中创建需要被统一配置管理的配置文件。 配置文件的命名方式:{application}-{profile}.yml 或 {application}-{profile}.yml application为应用名称 profile用于区分开发环境,测试环境、生产环境等 如user-dev.yml,表示用户微服务开发环境下使用的配置文件。
创建配置中心服务
我们的配置中心服务也需要注册到注册中心
①添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
②启动类添加注解 @EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
③配置
server:
port: 12000
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/chen_j/SpringCloudConfigDemo.git # 配置仓库的http地址
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
④测试
http://localhost:12000/user-server-pro.yml
让服务的配置从配置中心获取
①添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
②删除本地application.yml创建bootstrap.yml
spring:
cloud:
config:
# 与远程仓库中的配置文件的application保持一致
name: user-server
# 远程仓库中的配置文件的profile保持一致
profile: pro
# 远程仓库中的版本保持一致
label: master
discovery:
# 使用配置中心
enabled: true
# 配置中心服务id
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
注意:name属性的设置一定要设置为服务的服务名,不然的话拉取不到对应的配置文件
③启动服务测试
SpringCloudBus
修改配置中心
①添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
②修改配置
增加rabbitmq的配置和触发消息总线的地址 注意rabbitmq相关配置应该写自己的。
server:
port: 12000
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/chen_junfengke/SpringCloudConfigDemo.git # 配置仓库的http地址
rabbitmq:
host: 47.103.2.1
username: guest
password: guest
port: 5672
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
management:
endpoints:
web:
exposure:
# 暴露触发消息总线的地址
include: bus-refresh
改造拉取配置的服务
①添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
②修改 bootstrap.yml
增加rabbitmq的配置
spring:
rabbitmq:
host: 47.103.2.1
username: guest
password: guest
port: 5672
cloud:
config:
# 与远程仓库中的配置文件的application保持一致
name: user-server
# 远程仓库中的配置文件的profile保持一致
profile: pro
# 远程仓库中的版本保持一致
label: master
discovery:
# 使用配置中心
enabled: true
# 配置中心服务id
service-id: config-server
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
③在需要自动刷新最新配置的Controller上增加@RefreshScope
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
@Value("${server.port}")
String port;
@Value("${test}")
String name;
@RequestMapping("/findList")
public List<User> findList(Integer id) throws InterruptedException {
List<User> users = new ArrayList<>();
users.add(new User(Integer.valueOf(port),name));
return users;
}
}
④测试
修改配置后可以使用Post请求访问 http://127.0.0.1:12000/actuator/bus-refresh 注意是POST请求。就可以在不重启服务的情况下获取最新配置。