SpringCloud config 配置中心git示例(没有注册到eureka 上)

一:github上创一个文件夹config-repo用来存放配置文件

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。

它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。

Spring cloud使用git或svn存放配置文件,默认情况下使用git,我们先以git为例做一套示例(没有注册到eureka 上)。

1.1 首先在github上面创建了一个文件夹config-repo用来存放配置文件,为了模拟dev和pord环境,我们创建以下四个配置文件:

// 注册到eureka上 开发环境
config-eureka-client-dev.yml

// 注册到eureka上 生产环境
config-eureka-client-prod.yml

// 没有注册到eureka上 开发环境
config-single-client-prod.yml

// 没有注册到eureka上 生产环境
config-single-client-prod.yml

在这里插入图片描述
每个配置文件中都写一个属性neo.hello,属性值分别是 config-eureka-client-dev, config-eureka-client-prod.yml,config-single-client-prod.yml
, config-single-client-prod.yml。下面我们开始配置server端

二.config server端

2.1 在主Maven工程中创建一个新的 Module 工程,命名为config-serve。采用Spring Initializr 的方式的方式创建。
在这里插入图片描述
2.2 config-serve 的 pom.xml 的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.springcloud</groupId>
        <artifactId>springcloud-hx</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>config-serve</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-serve</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.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-web</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

添加了依赖:

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

2.3 主Module 的 的 pom.xml 加上:
在这里插入图片描述
2.4 在启动类上加上 @EnableConfigServer 注解开启配置中心服务器的功能,代码如下:

package com.example.configserve;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;


//注解开启配置中心服务器的功能
@EnableConfigServer
@SpringBootApplication
public class ConfigServeApplication {

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

}

2.5 config-serve的 配置文件 application.yml 的内容如下:

server:
  port: 8770
spring:
  application:
    name: config-server
  cloud:
    config:
      label: master #配置仓库的分支
      server:
        git:
          uri: https://github.com/huangxuhenshuai/springcloud-config-demo     # 配置git仓库的地址
          search-paths: config-repo    # git仓库地址下的相对地址(放配置文件的文件夹),可以配置多个,用,分割。
          username: xxxxxxxxxxxxx   # git仓库的账号
          password: xxxxxxxxxxxx     # git仓库的密码


#Spring Cloud Config也提供本地存储配置的方式。
#我们只需要设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。
#也可以通过spring.cloud.config.server.native.searchLocations=file:E:/properties/属性来指定配置文件的位置。
#或spring.cloud.config.server.native.searchLocations=classpath:/properties/
#虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。


#github 上的配置文件
#config-eureka-client-dev.yml
#config-eureka-client-prodyml
#config-single-client-dev.yml
#config-single-client-prod.yml

  1. uri: 配置git仓库的地址
  2. search-paths: git仓库地址下的相对地址(放配置文件的文件夹),可以配置多个,用,分割。
  3. username: git仓库的账号
  4. password: git仓库的密码
  5. Spring Cloud Config也提供本地存储配置的方式。
    我们只需要设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。
  6. 也可以通过spring.cloud.config.server.native.searchLocations=file:E:/properties/属性来指定配置文件的位置。或spring.cloud.config.server.native.searchLocations=classpath:/properties/
  7. 虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。

2.6 测试
启动 config-serve,首先我们先要测试server端是否可以读取到github上面的配置信息,直接访问:http://localhost:8770/config-single-client/dev ,返回的信息如下:

      
{
    "name":"config-single-client",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"d64bbd0cc11d67f42041783fe795f2a750bb137f",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/huangxuhenshuai/springcloud-config-demo/config-repo/config-single-client-dev.yml",
            "source":{
                "neo.hello":"config-single-client-dev"
            }
        }
    ]
}

上述的返回的信息包含了配置文件的位置、版本、配置文件的名称以及配置文件中的具体内容,说明server端已经成功获取了git仓库的配置信息。

扫描二维码关注公众号,回复: 9052276 查看本文章

如果直接查看配置文件中的配置信息可访问:http://localhost:8770/config-single-client-dev.yml,返回:

neo:
  hello: config-single-client-dev

修改配置文件config-single-dev.yml 中配置信息为:neo.hello=config-single-client-dev666666, 再次在浏览器访问http://localhost:8770/config-single-client-dev.yml,返回:

neo:
  hello: config-single-client-dev666666

说明server端会自动读取最新提交的内容。

仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

  1. /{application}/{profile}[/{label}]
  2. /{application}-{profile}.yml
  3. /{label}/{application}-{profile}.yml
  4. /{application}-{profile}.properties
  5. /{label}/{application}-{profile}.properties

以config-single-client-dev.yml 为例子,它的application是config-single-client,profile是dev。client会根据填写的参数来选择读取对应的配置。

三.config client端

3.1 在主Maven工程中创建一个新的 Module 工程,命名为config-client。采用Spring Initializr 的方式的方式创建。
在这里插入图片描述
3.2 config-client 的 pom.xml 的内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.springcloud</groupId>
        <artifactId>springcloud-hx</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

添加了依赖:

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

3.3 主Module 的 的 pom.xml 加上:
在这里插入图片描述

3.4 config-client 的 配置文件 bootstrap.yml 的内容如下:

spring:
  application:
    name: config-client
  cloud:
    config:
      name: config-single-client
      profile: dev
      uri: http://localhost:8770/
      label: master
server:
  port: 8771

#特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.yml,config部分内容才能被正确加载。
#因为config的相关配置会先于application.yml,而bootstrap.yml的加载也是先于application.yml。


#github 上的配置文件
#config-eureka-client-dev.yml
#config-eureka-client-prodyml
#config-single-client-dev.yml
#config-single-client-prod.yml


#spring.application.name:对应{application}部分
#spring.cloud.config.profile:对应{profile}部分
#spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
#spring.cloud.config.uri:配置中心的具体地址
#spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。(config-service在eureka中的名称)
#spring.cloud.config.discovery.enabled: true   是否通过eureka发现config-service

特别注意:
上面这些与spring-cloud相关的属性必须配置在bootstrap.yml,config部分内容才能被正确加载。因为config的相关配置会先于application.yml,而bootstrap.yml的加载也是先于application.yml。

  1. spring.application.name:对应{application}部分
  2. spring.cloud.config.profile:对应{profile}部分
  3. spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
  4. spring.cloud.config.uri:配置中心的具体地址
  5. spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。(config-service在eureka中的名称)
  6. spring.cloud.config.discovery.enabled: true 是否通过eureka发现config-service

3.5 测试
新建一个 TestController 类,使用@Value注解来获取server端参数的值。代码如下:

package com.example.configclient.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Value("${neo.hello}")
    private String hello;

    @RequestMapping("/test")
    public String from() {
        return this.hello;
    }

}

启动 config-serve、config-client 服务,在浏览器上访问:http://localhost:8771/test 返回的信息如下:

config-single-client-dev

说明config-client 从 config-server获取了neo.hello 的属性,而config-server是从 git仓库读取的,如图:
在这里插入图片描述

接着,修改config-single-client-dev.yml 中配置信息为:neo.hello=config-single-client-dev666666 提交到github。

再次在浏览器访问http://localhost:8771/test,返回:config-single-client-dev,说明获取的信息还是旧的参数,这是为什么呢?

因为springboot项目只有在启动的时候才会获取配置文件的值,修改github信息后,client端并没有再次去获取,所以导致这个问题。

如何去解决这个问题呢?留到下一章我们在介绍。

发布了50 篇原创文章 · 获赞 75 · 访问量 8645

猜你喜欢

转载自blog.csdn.net/weixin_40991408/article/details/104189002
今日推荐