温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

构建Spring Cloud配置服务器的步骤

发布时间:2021-08-27 17:23:57 来源:亿速云 阅读:120 作者:chen 栏目:编程语言

这篇文章主要介绍“构建Spring Cloud配置服务器的步骤”,在日常操作中,相信很多人在构建Spring Cloud配置服务器的步骤问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”构建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;

  }无锡人流医院 http://www.wxbhffk.com/

  public static void main(String[] args) {

  SpringApplication.run(SpringCloudConfigServerClientApplication.class, args);

  }

  @Scheduled(fixedRate = 5*1000, initialDelay = 3*1000)

  public void autoRefresh() {

  Set 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. 依赖

  org.springframework.hateoas

  spring-hateoas

到此,关于“构建Spring Cloud配置服务器的步骤”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI