ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringCloud Config使用

2021-07-17 16:03:31  阅读:168  来源: 互联网

标签:name Config spring SpringCloud 配置 zhao 使用 config cloud


构建配置中心

配置中心服务端配置

新建一个配置中心模块,且注册到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

标签:name,Config,spring,SpringCloud,配置,zhao,使用,config,cloud
来源: https://www.cnblogs.com/zhendiao/p/15023620.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有