1 Star 0 Fork 0

gupaoedu / jackxiet

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Spring cloud 实现配置中心

建立服务端

  1. 创建项目:star.spring.io 选择需要的jar

    Web,Actuator,config-server

  2. 在git上建立要读取的配置文件

    gupao.properties
    gupao-prod.properties

  3. 配置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
  4. 在application.java启动类上添加@EnableConfigService标记为配置服务器

建立客户端

  1. 创建项目:star.spring.io 选择需要的jar

    Web,Actuator,config-client

  2. 配置appliaction.properties

    #应用名称
    spring.application.name=config-client
    #开放env访问权限
    endpoints.env.sensitive=false
    #开放refresh动态刷新权限
    endpoints.refresh.sensitive=false
  3. 配置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

    -> http://localhost:9090/gupao-prod.properties

  4. @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)
                            )
            );
        }

  5. @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

空文件

简介

暂无描述 展开 收起
Java
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/gupao-v/jackxiet.git
git@gitee.com:gupao-v/jackxiet.git
gupao-v
jackxiet
jackxiet
master

搜索帮助

14c37bed 8189591 565d56ea 8189591