springcloud入门之配置中心

开发分布式系统如果还是各个服务配置文件单独配置肯定是不行的,springcloud使用的解决方案是搭建配置中心将并指定一个配置文件路径如git项目对配置文件进行统一管理。
在Spring Cloud中,提供了分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在实现cloud config中,主要有两个角色:作为配置中心连接配置路径的 config server,连接配置中心读取配置的config client。

使用springcloud 搭建一个入门的配置中心 config server

基本步骤:
1.新建一个springboot项目,依赖选择 config server,eureka,web

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2.程序启动类Application加上注解@EnableConfigServer注解开启配置服务器的功能

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

3.配置文件application.yml配置服务和配置文件路径

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

server:
  port: 8040
spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/eacdy/spring-cloud-study/   # 配置git仓库的地址
          search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。
          username: # git仓库的账号
          password: # git仓库的密码

本项目git仓库为测试仓库,可直接使用测试
注:配置中心项目除了在导入eureka依赖并在程序启动类Application上加上注解@EnableConfigServer表示该项目是配置中心,还需要在配置文件中配置
eureka:
client:
register-with-eureka: false
fetch-registry: false

避免配置中心向自己发布服务导致程序报错服务启动不了。如果不加项目启动时报错:Cannot execute request on any known server

启动项目,可以通过一下url测试是否能够正常获取到git仓库中的配置信息
http://localhost:8040/microservice-config-client-dev.properties
http://localhost:8040/microservice-config-client/dev

使用springcloud搭建入门配置服务client

基本步骤
1.新建springboot项目,依赖选择 config,web,actuator

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

        <!-- client需要添加以下依赖,否则访问/refresh将会得到404 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

注:作为config client需要添加以下依赖,否则访问/refresh将会得到404
controller需要添加@RefreshScope注解,否则配置无法刷新

2.配置文件配置bootstrap.yml(自行创建文件)

spring:
  application:
    name: microservice-config-client # 对应config-server所获取的配置文件的{application}
  cloud:
    config:
      uri: http://localhost:8040/
      profile: dev # 指定profile,对应config-server所获取的配置文件中的{profile}
      label: master # 指定git仓库的分支,对应config-server所获取的配置文件的{label}

application.yml配置本地项目启动加载如服务端口server.port=8041
注:为什么不直接把配置全部直接写入application.yml而是另建bootstrap.yml:
因为config部分的配置先于 application.yml 被加载,而 bootstrap.yml 中的配置会先于 application.yml 加载,如果直接写在application.yml会导致项目启动时无法读取到配置中心配置文件导致报错。

3.编写controller类测试配置读取情况

@RestController
@RefreshScope
public class TestConfigTontroller {
    //这个@Value会根据配置的配置中心地址找到git仓库对应的配置和本地服务的配置文件
    @Value("${profile}")
    private String configValue;
    @RequestMapping("testConfig")
    public String test(){
        return "读取到配置中心:"+configValue;
    }
}

浏览器访问url :http://localhost:8041/testConfig 可以看到浏览器返回git配置文件的配置

springcloud config 的client实现读取配置的大体路径:
config-server从git仓库读取到配置文件,config-client再通过访问config-server获取到配置属性

附上项目源码:
config-server:https://pan.baidu.com/s/1eSq5sPk
config-client:https://pan.baidu.com/s/1pLyux9D


版权声明:本文为chenhaotao原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。