创建项目:star.spring.io 选择需要的jar
Web,Actuator,config-server
在git上建立要读取的配置文件
gupao.properties
gupao-prod.properties
配置application.properties
#应用名称
spring.application.name=config-server
#端口
server.port=9090
#配置文件路径
spring.cloud.config.server.git.uri=file:///D:/Git/code/spring-cloud-server-lession2/jackxiet
#开放env查看权限
endpoints.env.sensitive=false
在application.java启动类上添加@EnableConfigService标记为配置服务器
创建项目:star.spring.io 选择需要的jar
Web,Actuator,config-client
配置appliaction.properties
#应用名称
spring.application.name=config-client
#开放env访问权限
endpoints.env.sensitive=false
#开放refresh动态刷新权限
endpoints.refresh.sensitive=false
配置bootstrap.properties
#连接到的服务器URI
spring.cloud.config.uri=http://localhost:9090/
#配置客户端应用名称:{application}
spring.cloud.config.name=gupao
#profile 是激活的配置
spring.cloud.config.profile=prod
#label 在git中指的分支名称
spring.cloud.config.label=master
URI:{uri}/{application}-{profile}.properties
@Scheduled
/**scheduled是spring提供的定时事件,
*fixedRate:周期时间
*initialDelay:初始化启动时间
*该方法为定时从服务器刷新配置属性
*spring向服务器拉去配置信息的对象
*private final ContextRefresher contextRefresher;
*环境对象
*private final Environment environment;
**/
@Scheduled(fixedRate = 5*1000,initialDelay = 3*1000)
public void autorefesh(){
//刷新配置信息
Set<String> updateProperties = contextRefresher.refresh();
updateProperties.forEach(properties ->
System.err.printf("当前线程:%s,更新的key:%s ,value:%s",
Thread.currentThread().getName(),
properties,
environment.getProperty(properties)
)
);
}
@RefreshScope:用于需要动态从服务端获取属性的bean类上面,
每次刷新该bean也能拿到最新配置。
hateoas
rest服务发现工具,类似wdsl文件能告诉你服务端提供了哪些服务。
当需要了解服务端有哪些服务时,可以添加该dependency,详情去spring.io搜索hateoas
health健康检查
意义:可以任意输出业务健康,系统健康,内存健康等信息
端点URI: /health
/**scheduled是spring提供的定时事件,
*fixedRate:周期时间
*initialDelay:初始化启动时间
*该方法为定时从服务器刷新配置属性
*spring向服务器拉去配置信息的对象
*private final ContextRefresher contextRefresher;
*环境对象
*private final Environment environment;
**/
@Scheduled(fixedRate = 5*1000,initialDelay = 3*1000)
public void autorefesh(){
//刷新配置信息
Set<String> updateProperties = contextRefresher.refresh();
updateProperties.forEach(properties ->
System.err.printf("当前线程:%s,更新的key:%s ,value:%s",
Thread.currentThread().getName(),
properties,
environment.getProperty(properties)
)
);
}
实现类:HealthEndpoint
健康指示器:HealthIndicator
自定义实现HealthIndicator
1.创建一个类继承HealthIndicator
public class MyHealthIndicator extends AbstractHealthIndicator{
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
builder.down().build();
}
}
2.在@Configuration或者@SpringBootApplication类中定义@bean 暴露该类作为健康指标
@Bean
public MyHealthIndicator myHealthIndicator(){
return new MyHealthIndicator();
}
3.在application.properties文件中配置开启health访问权限和security权限
#在没有权限的情况下,endpoints.env.sensitive=false仅能看见应用的状态
endpoints.env.sensitive=false
#在没有权限的情况下,endpoints.health.sensitive=false可以看见所有信息
endpoints.health.sensitive=false
以下是权限及可见的信息范围
management.security.enabled |
endpoints.health.sensitive |
Unauthenticated |
Authenticated (with right role) |
---|---|---|---|
false | * | Full content | Full content |
true | false | Status only | Full content |
true | true | No content | Full content |
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。