SpringCloud(六):配置中心 git示例

前言

本文章基于之前的项目实现
相关文章
SpringCloud(一):Eureka服务注册与发现
SpringCloud(二):Ribbon负载均衡
SpringCloud(三):Feign远程调用
SpringCloud(四):Eureka集群
SpringCloud(五):Hystrix熔断器 服务熔断与服务降级

1.Spring Cloud Config介绍

什么是配置中心?
使用微服务就意味着要将单体应用中的业务拆分成一个个的子服务,每个服务的粒度相对较小,因此,系统中将会出现大量的服务。由于每一个服务都需要必要的配置信息才能运行,所以也将会产生大量的配置文件,所以有一套对配置文件进行集中式、动态式的管理的设施就必不可少了。

常见对配置的管理有三种:

传统的配置方式:配置信息分散到系统各个角落方式,配置文件或者在代码中。
集中式配置中心:将应用系统中对配置信息的管理作为一个新的应用功能模块,进行集中统一管理,并且提供额外功能。
分布式配置中心:在分布式、微服务架构中,独立的配置中心服务。

为什么要用配置中心?

在微服务体系中,服务的数量以及配置信息日益增多,比如各种服务器参数配置、各种数据库访问参数配置、各种环境下配置信息的不同、配置信息修改之后实时生效等等,传统的配置文件方式或者将配置信息存放于数据库中的方式已无法满足开发人员对配置管理的要求,如:

安全性:配置跟随源代码保存在代码库中,容易造成配置泄漏。
时效性:修改配置,需要重启服务才能生效。
局限性:无法支持动态调整,例如日志开关、功能开关。

2.创建储存库

首先在github上面创建一个储存库,然后再库中创建一个文件夹config-repo用来存放配置文件,为了模拟生产环境,我们创建以下三个配置文件:

// 开发环境
config-dev.properties
// 测试环境
config-test.properties
// 生产环境
config-pro.properties

如下图:
在这里插入图片描述

每个配置文件中都写一个属性wxy.hello,属性值分别是 hello im dev/test/pro 。

3.server端
在原有的项目基础下创建config-server模块。如下图:
在这里插入图片描述

pom文件相关代码

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wxy</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.2</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <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>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

主要引入config依赖就可以了

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

配置文件

server.port=8027
spring.application.name=config-server
# 配置git仓库的地址
spring.cloud.config.server.git.uri=https://github.com/wxyhs/cloud-config.git
# git仓库地址下的相对地址,可以配置多个,用,分割。
spring.cloud.config.server.git.search-paths=config-repo
spring.cloud.config.server.git.username=你的git账号
spring.cloud.config.server.git.password=你的git密码
#有ssl协议才开启,没有可以不用
management.cloudfoundry.skip-ssl-validation=true
#配置仓库的分支
spring.cloud.config.server.git.default-label=main

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

启动类添加@EnableConfigServer注解激活对配置中心的支持

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

}

到此server端相关配置已经完成

测试

依次启动eureka-server、config-server服务。
直接访问http://localhost:8027/config/dev
返回信息如下:

{“name”:“config”,“profiles”:[“dev”],“label”:null,“version”:“b2141a14e1444a261cef3d7adc655120423d4df3”,“state”:null,“propertySources”:[{“name”:“https://github.com/wxyhs/cloud-config.git/file:C:\Users\23638\AppData\Local\Temp\config-repo-5411059747233665343\config-repo\config-dev.properties”,“source”:{“wxy.hello”:“dev”}}]}

上述的返回的信息包含了配置文件的位置、版本、配置文件的名称以及配置文件中的具体内容,说明server端已经成功获取了git仓库的配置信息。
如果直接查看配置文件中的配置信息可访问:http://localhost:8027/config-dev.properties,返回:wxy.hello: dev

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

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

4.client端

创建config-cient模块如下图:
在这里插入图片描述

添加pom依赖

<?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>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.4.5</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.wxy</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>
      <spring-cloud.version>2020.0.2</spring-cloud.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>

      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>

<!--springcloud2020 版本 把Bootstrap被默认禁用,同时spring.config.import加入了对解密的支持。对于Config Client、Consul、Vault和Zookeeper的配置导入,如果你需要使用原来的配置引导功能,那么需要将org.springframework.cloud:spring-cloud-starter-bootstrap依赖引入到工程中-->
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-bootstrap</artifactId>
         <version>3.0.2</version>
      </dependency>

      <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>


   <dependencyManagement>
      <dependencies>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
         </dependency>
      </dependencies>
   </dependencyManagement>

   <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <configuration>
               <skipTests>true</skipTests>
            </configuration>
         </plugin>
      </plugins>
      <finalName>tps</finalName>
   </build>

</project>

配置文件
需要配置两个配置文件,application.properties和bootstrap.properties

application.properties如下:

spring.application.name=config-client
server.port=8028

bootstrap.properties如下:

spring.cloud.config.name=config
spring.cloud.config.profile=pro
spring.cloud.config.uri=http://localhost:8027/
spring.cloud.config.label=main

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,便于扩展为高可用配置集群。
特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。

测试

/**
 * @Author: Wxy
 * @Description:测试配置中心
 * @Date created in 14:19 2021/5/26
 */
@RestController
public class HelloController {

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

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

依次启动eureka-server、config-server、config-client
访问http://localhost:8028/hello,返回pro表示server端获取到了参数。到此一个完整的服务端提供配置服务,客户端获取配置参数的例子就完成了。

过程中如果有小伙伴遇到问题或者有好的见解可以及时联系

gitHub地址

配置中心git地址

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值