Java   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了SpringCloud Config使用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

构建配置中心

配置中心服务端配置

新建一个配置中心模块,且注册到eureka中,在其他服务的基础上增加如下配置
pom文件增加配置服务端设置

@H_262_8@        <!--config配置中⼼服务端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

配置需要增加如下配置

@H_262_8@spring:
  application:
    name: zhao-service-config
  cloud:
    config:
      server:
        git:
          username: xxx@qq.com
          password: xxx
          search-paths:
             - zhao-config-repo
          uri: https://gitee.com/kylezhen/zhao-config-repo.git
      label: main
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

需要注意的是我们尽量还是使用gitee作为远程配置中心的拉取地址,否则会因为github网络不畅出现各种问题。配置完成之后我们在启动类加入@EnableConfigServer

@H_262_8@@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigApplication9007 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication9007.class,args);
    }
}

即完成配置中心服务端配置,通过服务端直接访问配置文件
file

配置中心客户端配置以及手动刷新

pom文件添加

@H_262_8@        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>

配置文件重命名为bootstrap.yml之后增加对配置中心的使用。bootstrap.yml是系统级别的,优先级⽐application.yml⾼,应⽤启动时会检查这个配置⽂件,在这个配置⽂件中指定配置中⼼的服务地址,会⾃动拉取所有应⽤配置并且启⽤。配置暴露健康检查等端点接⼝,以更新配置

@H_262_8@spring
  cloud:
    config:
      name: zhao-service-resume
      profile: dev
      label: main
      uri: http://localhost:9007
management:
  endpoints:
    web:
      exposure:
        include: "*"

经过配置之后增加配置访问的内容

@H_262_8@@RestController
@requestMapping("/config")
public class ConfigController {

    // 和取本地配置信息一样
    @Value("${zhao.namE}")
    private String name;
//    @Value("${mysql.url}")
//    private String mysqlUrl;


    // 内存级别的配置信息
    // 数据库,redis配置信息

    @GetMapping("/viewconfig")
    public String viewconfig() {
        return "zhaoname==>" + name;
    }
}

访问改获取配置的接口
file
但是这样无法获取最新配置,我们需要在获取配置的配置类上加入@refreshScope注解。并且在更改后手动向使用配置文件的服务健康检查接口发送POST请求才能更新
file
返回为空表示无变更数据,上述为正常获取到配置文件变更

借助Spring Cloud Bus动态刷新配置

网上的教程多以官方支持的Rabbitmq和kafka作为基础来实现,我这里以阿里自己的Rocketmq为例来进行操作

@H_262_8@        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-rocketmq</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

在配置中心的服务端和客户端中进行相应的配置

@H_262_8@spring:
  application:
    name: zhao-service-config
  cloud:
    config:
      server:
        git:
          username: @qq.com
          password: xxx
          search-paths:
             - zhao-config-repo
          uri: https://gitee.com/kylezhen/zhao-config-repo.git
      label: main
    bus:
      enabled: true

rocketmq:
  name-server: 127.0.0.1:9876

通过访问http://localhost:9007/actuator/bus-refresh 即可将配置改变推送到配置

file
file

大佬总结

以上是大佬教程为你收集整理的SpringCloud Config使用全部内容,希望文章能够帮你解决SpringCloud Config使用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: