springcloud config配置中心

Posted 欧欧专栏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springcloud config配置中心相关的知识,希望对你有一定的参考价值。

配置中心简介

config server 可以从本地或git仓库里读取配置,配置可以放置在一个module里进行管理。

配置高可用config server

把config-server 配置到eureka-server服务里统一管理,可以把config-server配置成集群:

新建config server 模块

a)引入依赖

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

 

b)配置文件 application.yml

spring:
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared
#          search-locations: file:E://Java/本地磁盘
  profiles:
     active: native
  application:
    name: config-server

#把实例注册到eureka server服务里管理
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

 

在项目resource里建shared文件夹,并新建文件config-client-dev.yml,添加如下内容

server:
  port: 8783

foo: foo version 1

 

c)启动类添加注解

@EnableConfigServer

 

d) 共用的配置,即文件可以继承

我们在远程仓库中创建了三个对应不同环境的配置文件user-service-dev.yml,user-service-test.yml,user-service-prod.yml,假如在不同环境下有很多共用的配置,我们可以再创建一个user-service.yml用来存放共同配置,springcloud-config取配置时,会将你的对应环境配置与user-service.yml合并后再返回。application.yml文件和所有的文件进行合并后再返回。

新建config client 模块

a)引入依赖

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

 

b)配置文件,bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      fail-fast: true
      discovery:
        enabled: true
        serviceId: config-server
  profiles:
    active: dev

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

 

根据{spring.application.name}-{profiles.active}名称到config-server 模块shared目录下查询配置文件

c)启动类不用添加其它注解

d)使用,和本地配置使用一样

@SpringBootApplication
@RestController
@EnableEurekaClient
public class ConfigClientApplication {

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

    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/foo")
    public String hi(){
        return foo;
    }
}

 

测试

在浏览器输入地址:http://localhost:8783/foo

使用Spring Cloud Bus刷新配置

如果我们想修改配置文件,又不用重启项目立即生效,则需要用到spring cloud bus刷新配置。

修改config client模块

只需求修改config-client模块,即可实现动态刷新

a)引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
    

 

b)修改配置文件,添加如下内容,要先安装RabbitMQ ,

  rabbitmq:
    host: localhost
    port: 15672
    username: guest
    password: guest
    publisher-confirms: true
    virtual-host: /

#先把安全策略设置为false management: security: enabled:
false

 

c)在使用配置的类上添加 @RefreshScope注解

@RestController
@RefreshScope
public class ConfigClientApplication {
    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/foo")
    public String hi(){
        return foo;
    }
}

 

测试

修改config-server项目 目录shared下的config-client-dev.yml配置文件内容,然后 发送 post请求 http://localhost:8781/bus/refresh,再http://localhost:8783/foo,你会发现配置文件修改内容已生效

以上是关于springcloud config配置中心的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloud集成Config分布式配置中心

SpringCloud Config 配置中心

springcloud应用配置中心config的安全设置

springcloud-config配置中心初级篇

springcloud config配置中心

SpringCloud分布式配置中心Config