Spring Cloud Config 分布式配置中心【Finchley 版】

一、 介绍

1,为什么需要配置中心?

当服务部署的越来越多,规模越来越大,对应的机器数量也越来越庞大,靠人工来管理和维护服务的配置信息,变得困难,容易出错。
因此,需要一个能够动态注册和获取服务信息的地方,来统一管理服务名称和其对应的服务器列表信息,称之为服务配置中心。服务提供者在启动时,将其提供的服务名称、服务器地址注册到服务配置中心。服务消费者通过服务配置中心来获得需要调用的服务,通过相应的负载均衡算法,选择其中一台服务器开始调用。

2,什么是 Spring Cloud Config

spring cloud config 是 spring cloud 中的一个组件,分布式配置中心组件,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

二、 Spring Cloud Config 实战

2.1,构建Config Server

2.1.1,创建一个名为config-server 的 spring-boot项目,pom.xml依赖如下: 注意组件版本我用的是 Spring Boot 2.0.x

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.2,在入口类ConfigServerApplication 添加注解 @EnableConfigServer,开启Config配置中心服务端

2.3,在根目录新建的配置文件 config-dev.yml,并提交到git仓库,实例是mysql的数据原本配置信息,提交的github中,配置文件如下:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.10.100:3306/spring?useSSL=false
    username: test
    password: 123456

2.4,配置 application.yml ,配置如下:

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/jarvisqi/spring-cloud-microservice
          search-paths: config-server
          username:
          password:
          default-label: master
server:
  port: 9400
  • spring.cloud.config.server.git.url :指定git的仓库地址,改成你自己的
  • spring.cloud.config.server.git.search-paths :指定你的配置文件所在的目录
  • username,password 如果是github公开的就不用添加,自己的私有仓库自己添加

2.4,启动Config-Server,并访问:http://localhost:9400/config/dev 地址,看到如下信息,说明服务端正常运行:

{
    "name": "config",
    "profiles": [
        "dev"
    ],
    "label": null,
    "version": "e64289cb775e2ac7db7494c07d7e8c4933163daf",
    "state": null,
    "propertySources": [
        {
            "name": "https://github.com/jarvisqi/spring-cloud-microservice/config-server/config-dev.yml",
            "source": {
                "spring.datasource.driver-class-name": "com.mysql.jdbc.Driver",
                "spring.datasource.url": "jdbc:mysql://192.168.10.100:3306/spring?useSSL=false",
                "spring.datasource.username": "test",
                "spring.datasource.password": 123456
            }
        }
    ]
}

source 下面的key就是配置的信息,http请求地址和资源文件映射如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

2.2,构建Config Client

2.2.1,创建一个名为config-client 的 spring-boot项目,pom.xml依赖如下: 注意组件版本我用的是 Spring Boot 2.0.x

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.2.2,配置 application.yml ,配置如下:

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:9400
      name: config
      profile: dev
      label: master
server:
  port: 9410
  • spring.cloud.config.uri : 指定config-server 服务端地址,注意并不是git仓库地址
  • spring.cloud.config.name:注意 如果name值不写,会默认取 spring.application.name的值,资源文件名就变成了 config-client-dev,根据http请求地址和资源文件映射,肯定找不到,会报错,无法启动
  • spring.cloud.config.profile:一般就是 dev ,test ,prd,你自定义也行,因为我提交的文件写的是 dev
  • spring.cloud.config.label :git的分支

2.2.3,配置 boostrap.yml ,配置如下:

spring:
  cloud:
    config:
      uri: http://localhost:9400

注意,此处有坑,如果你不添加 boostrap.yml文件并重新指定指定 spring.cloud.config.uri,启动客户端会报错,会默认指定 端口:8888,不是自定义的端口,无法启动会报错,当然你可以直接用 boostrap.yml ,我看那此处很多人遇到问题,遇到问题就说降低版本到1.5.X就正确,这也能算解决方案?

2.2.4,添加 ConfigController 接口,并获取配置信息,代码如下:

@RestController
public class ConfigController {

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @RequestMapping("/dataconfig")
    public DataConfig getDataConfig() {
        DataConfig config = new DataConfig(driverClassName, url, username, password);
        return config;
    }
}

2.2.5,首先启动config-server,保证运行正常,再启动 config-client,并请求地址 http://localhost:9410/dataconfig,得到如下结果,说明配置中心完成:

{
    "driverClassName": "com.mysql.jdbc.Driver",
    "url": "jdbc:mysql://192.168.10.100:3306/spring?useSSL=false",
    "username": "test",
    "password": "123456"
}

以上,就可以单独部署配置中心,配置信息值需要git提交到指定的目录,微服务各个独立的服务应用都能获取。

附上源码:https://github.com/jarvisqi/spring-cloud-microservice.git

参考:

  • http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html

来源: https://www.cnblogs.com/JreeyQi/p/9526850.html

http://yyk.familydoctor.com.cn/content_21206_975492.html

http://yyk.familydoctor.com.cn/content_21206_975204.html

http://yyk.familydoctor.com.cn/content_21206_975245.html

http://yyk.familydoctor.com.cn/content_21206_975295.html

猜你喜欢

转载自blog.csdn.net/qq_42564846/article/details/82015115