构建Spring Cloud配置服务器
实现步骤:
1. 在Configuration Class标记@EnableConfigServer
2. 配置文件目录(基于git)
- cloud.properties(默认) //默认环境,跟随代码仓库
- cloud-dev.properties(proflie="dev")//开发环境
- cloud-test.properties(proflie="test")//测试环境
- cloud-staging.properties(proflie="staging")//预发布环境
- cloud-prod.properties(proflie="prod")//生产环境
3. 服务端配置版本仓库
spring.cloud.config.server.git.uri=https://github.com/****test****/springcloud.git
spring.cloud.config.server.git.searchPaths=properties
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
完整配置项:
spring.application.name = config-server
server.port=9090
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.cloud.config.server.git.uri=https://github.com/****test****/springcloud.git
spring.cloud.config.server.git.searchPaths=properties
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
构建Spring Cloud配置客户端
实现步骤:
1. 创建 bootstrap.properties 或者 bootstrap.yml 文件
2. bootstrap.properties 或者 bootstrap.yml 文件中配置客户端信息(以下是bootstrap.properties )
##配置服务器URI
spring.cloud.config.uri = http://localhost:9090/
##配置客户端应用名称
spring.cloud.config.name = cloud
##激活配置
spring.cloud.config.profile = prod
##在github上的分支名
spring.cloud.config.label = master
3. 设置敏感性安全
management.endpoints.web.exposure.include=*
4. 设置健康检查
management.endpoint.health.show-details=always
@RefreshScope用法
@RestController
@RefreshScope
public class EchoController {
@Value("${my.name}")
private String myName;
@GetMapping("/my-name")
public String getName() {
return myName;
}
}
用户调用/actuator/refresh控制客户端配置更新
实现定时更新客户端
@SpringBootApplication
@EnableScheduling
public class SpringCloudConfigServerClientApplication {
private final ContextRefresher contextRefresher;
private final Environment environment;
@Autowired
public SpringCloudConfigServerClientApplication(ContextRefresher contextRefresher,
Environment environment) {
this.contextRefresher = contextRefresher;
this.environment = environment;
}
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerClientApplication.class, args);
}
@Scheduled(fixedRate = 5*1000, initialDelay = 3*1000)
public void autoRefresh() {
Set<String> updatePropertyNames = contextRefresher.refresh();
updatePropertyNames.forEach(name -> System.err.println(">>>>>>"+name+environment.getProperty(name)));
if(!updatePropertyNames.isEmpty()) {
System.err.printf("[Thread :%s] 当前配置已更新,具体项目:%s \n",
Thread.currentThread().getName(),
updatePropertyNames);
}
}
}
健康检查
意义
比如应用可以任意地输出业务健康、系统健康等指标
端点URI:/actuator/health
实现类:HealthEndpoint
健康指示器:HealthIndicator,
HealthEndpoint:HealthIndicator,一对多
自定义实现HealthIndicator
1. 实现AbstractHealthIndicator
public class MyHealthIndicator extends AbstracHealthIndicator{
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception{
builder.up().withDetail("MyHealthIndicator",">>Up>>");
}
}
2. 暴露MyHealthIndicator为bean
@Bean
public MyHealthIndicator myHealthIndicator(){
return new MyHealthIndicator();
}
3. 依赖
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>