zoukankan      html  css  js  c++  java
  • SpringCloud Config使用

    构建配置中心

    配置中心服务端配置

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

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

    配置需要增加如下配置

    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

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

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

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

    pom文件添加

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

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

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

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

    @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为例来进行操作

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

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

    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 即可将配置改变推送到配置

    欢迎搜索关注本人与朋友共同开发的微信面经小程序【大厂面试助手】和公众号【微瞰技术】,以及总结的分类面试题https://github.com/zhendiao/JavaInterview

    file
    file

  • 相关阅读:
    JS中的this
    VS下遇到未能加载文件或程序集 错误
    观察者模式实现INotifyPropertyChanged
    看书不仔细的下场
    Android 解析XML
    杂想
    Android 对话框用法
    Android之AlertDialog.Builder详解
    Android:开机自启动并接收推送消息
    Clojure:通过ZeroMQ推送消息
  • 原文地址:https://www.cnblogs.com/zhendiao/p/15023620.html
Copyright © 2011-2022 走看看