资讯专栏INFORMATION COLUMN

SpringBoot统一配置中心

remcarpediem / 3343人阅读

摘要:创建统一服务项目可以使用来初始化项目,选择自己的以来就好。动态刷新配置目前如果我们修改了上的配置并不能马上生效,需要我们的客户端工程重启才行,现在需要改造成自动刷新。

一直使用springboot搭建后端项目,所有的配置都写到自己的resource目录下,随着微服务的项目越来越多,每个项目都需要自己的各种配置文件。而且后期一旦想要修改配置文件,就得重新发布一遍非常的麻烦,现在就来教教大家怎么统一在github上管理 这些配置,并做到一处修改处处生效,不需要重新发布项目。
1 创建统一服务项目

可以使用STS来初始化项目,选择自己的以来就好。



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.mike
    config-server
    0.0.1-SNAPSHOT
    config-server
    config server

    
        1.8
        Greenwich.SR1
    

    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.cloud
            spring-cloud-config-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

创建bootstrap.yml文件,当然你可以使用application.ymlapplication.properties

spring:
  application:
    name: config-repo
  cloud:
    config:
      server:
        git:
          uri: https://github.com/mike/config-repo.git   #github仓库地址
          username: mike      # 用户名
          password: 123456      # 密码

在github上创建一个config-repo仓库,并添加配置文件:

两个不同环境的配置
hello-pj-dev.yml

hello:
  text: hello spring dev

hello-pj-uat.yml

hello:
  text: hello spring uat

创建启动类:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

启动应用,访问:
http://localhost:8080/hello-pj-dev.yml
http://localhost:8080/hello-pj-uat.yml
你就可以看到远程的配置中心。

2 创建测试项目

pom



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.4.RELEASE
         
    
    com.mike
    hello-server
    0.0.1-SNAPSHOT
    hello-server
    hello server

    
        1.8
        Greenwich.SR1
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

创建bootstrap.yml文件,只能是这个文件,不能是application文件

server:
  port: 8082
spring:
  application:
    name: hello-pj
  cloud:
    config:
      profile: dev
      uri: http://localhost:8080/

这里面配置了读取远程配置文件的uri,注意application.name必须对应github上的文件名,最终访问的是application.name+profile

创建测试controller

@RestController
public class TestController {
    @Value("${hello.text}")
    private String text;
    
    @GetMapping("/say")
    public String sayHello(){
        return text;
    }
}

访问 http://localhost:8082/say,你就可以看到对应的配置。这样你只需要在github上管理所有的配置就行了,但是记得"config-server"工程得一直启动,通过它我们才能获取github上的配置。

3 动态刷新配置

目前如果我们修改了github上的配置并不能马上生效,需要我们的客户端工程重启才行,现在需要改造成自动刷新。

在客户端工程中加入新的依赖:


            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        

修改bootstrap.yml文件

server:
  port: 8082
spring:
  application:
    name: hello-pj
  cloud:
    config:
      profile: dev
      uri: http://localhost:8080/
    bus:
      trace:
        enabled: true
rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

注意需要借助rabbitmq,所以本地需要启动rabbitmq

在需要刷新的地方加入注解@RefreshScope

@RestController
@RefreshScope
public class TestController {
    @Value("${hello.text}")
    private String text;
    
    @GetMapping("/say")
    public String sayHello(){
        return text;
    }
}

这样我们既可以随时修改github上的配置文件,而不需要重启应用。

如果对你有帮助,希望可以关注下我的公众号:

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/74333.html

相关文章

  • 基于SpringCloud FinchleySR1 SpringBoot 2x vue elemen

    摘要:介绍是基于微服务基础脚手架对于日常开发而言提供基础权限控制,动态菜单,才用前后端分离架构,前台采用后台使用提供接口。对于以后开发,只需要在添加业务模块即可,大大减少工作量。 介绍 panda是基于SpringCloud Finchley.SR1 、SpringBoot 2.x、 vue、element-ui 微服务基础脚手架对于日常开发而言提供基础权限控制,动态菜单,才用前后端分离架构...

    lansheng228 评论0 收藏0
  • 两年了,我写了这些干货!

    摘要:开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章系列处理登录请求前后端分离一使用完美处理权限问题前后端分离二使用完美处理权限问题前后端分离三中密码加盐与中异常统一处理 开公众号差不多两年了,有不少原创教程,当原创越来越多时,大家搜索起来就很不方便,因此做了一个索引帮助大家快速找到需要的文章! Spring Boo...

    huayeluoliuhen 评论0 收藏0
  • 【技术杂谈】springcloud微服务之数据操作独立模块化

    摘要:而在这个微服务下,同样需要进行数据操作,我不可能还要在下再一次进行集成,这样大大的增加了代码量。其次,是将有关数据操作的都单独部署成一个模块,比如我集成的模块,集成的模块,使用作为内存缓存模块。 前言 相对于 spring 对 mybatis 以及 redis 等的整合所需要的各种配置文件,在 springboot 下,已经大大的简化了,你可能只是需要增加个依赖,加个注解,然后在配置文...

    tianyu 评论0 收藏0

发表评论

0条评论

remcarpediem

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<