欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Cloud - 2 (Spring Cloud Config Server)

程序员文章站 2022-04-30 15:49:23
...

构建Spring Cloud配置服务器

实现步骤:

    1.  在Configuration Class标记@EnableConfigServer

    2.  配置文件目录(基于git)

  1. cloud.properties(默认) //默认环境,跟随代码仓库
  2. cloud-dev.properties(proflie="dev")//开发环境
  3. cloud-test.properties(proflie="test")//测试环境
  4. cloud-staging.properties(proflie="staging")//预发布环境
  5. 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>

其他内容:

查看服务列表:http://localhost:8080/actuator/

查看健康状况:http://localhost:8080/actuator/health