SpringCloud--SpringCloud Config构建统一微服务管理配置中心

在微服务架构中,微服务的配置管理一般有以下几个需求:

  • 集中管理配置。一个使用微服务架构的应用系统可能会包含成百上千个微服务,因此集中管理配置是非常有必要的。

  • 不同环境不同配置。例如,数据源配置在不同的环境(开发、测试、预发布、生产等)中时不同的。

  • 运行期间可动态调整。例如,可根据各个微服务的负载情况,动态调整数据源连接池大小或熔断阈值,并且在调整配置时不停止微服务。

  • 配置修改后可自动更新。如配置内容发生变化,微服务能够自动更新配置。

综上所述,对于微服务架构而言,一个通用的配置管理机制是必不可少的,常见做法是使用配置服务器管理配置。

SpringCloud Config简介

SpringCloud Config 为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Server和Config Client两部分。由于Config Server和Config Client都实现了对Spring Environment和PropertySource抽象的映射,因此,SpringCloud Config非常适合Spring应用程序,当然也可与任何其他语言编写的应用程序配合使用。

Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置内容(也可使用Subversion、本地文件系统或Vault存储配置,有兴趣的可以去讨论),因此可以很方便地实现对配置的版本控制与内容审计。

Spring Cloud Config与Eureka配合使用

首先在Git上创建配置中心仓库,直接在根目录下创建application.yml和consumer-server.yml配置文件。

一、构建Config Server

1、创建maven 项目,添加以下依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ms.cloud</groupId>
    <artifactId>ms-cloud-config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ms-cloud-config-server</name>
    <description>微服务配置中心</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.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>
    </properties>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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>Dalston.SR5</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>

2、application.yml配置文件如下:

ms:
  config:
    path: https://github.com/***/ms-cloud-config-repo.git


p: 9000
h: localhost:9001

spring:
  application:
    name: configServer
    version: 1.0.0
  profiles:
    active: dev


---
spring:
  profiles: dev
  cloud:
    config:
      discovery:
        enabled: true
      server:
        defaultLabel: develop
        git:
          uri: ${ms.config.path}

server:
  port: ${p}

eureka:
  instance:
    preferIpAddress: true
  client:
    serviceUrl:
      defaultZone: http://${h}/eureka/

---
spring:
  profiles: test
  cloud:
    config:
      discovery:
        enabled: true
      server:
        defaultLabel: develop
        git:
          uri: ${ms.config.path}

server:
  port: ${p}

eureka:
  instance:
    preferIpAddress: true
  client:
    serviceUrl:
      defaultZone: http://${h}/eureka/

---
spring:
  profiles: prod
  cloud:
    config:
      discovery:
        enabled: true
      server:
        defaultLabel: master
        git:
          uri: ${ms.config.path}

server:
  port: ${p}

eureka:
  instance:
    preferIpAddress: true
  client:
    serviceUrl:
      defaultZone: http://${h}/eureka/

3、启动类加上注解

@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

二、ConfigClient 配置

1、添加Config Client客户端依赖

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

2、bootstrap.yml配置如下

p: 10110
h: localhost:9001

server:
  port: ${p}

eureka:
  instance:
    preferIpAddress: true
  client:
    serviceUrl:
      defaultZone: http://${h}/eureka/

spring:
  application:
    name: consumer-server
    version: 1.0.0
  cloud:
    config:
      discovery:
        enabled: true
  profiles:
    active: dev

3、启动类添加注解

@EnableEurekaClient
@SpringBootApplication
public class ConsumerServerApplication {

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

4、测试类

@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Value("${common:unKnow}")
    private String common;

    @Value("${consumer:unKnow}")
    private String consumer;

    @RequestMapping(value = "/config")
    public String getConfigValue(){
        return String.format("common:%s,consumer:%s",common,consumer);
    }
}

三、测试

1、启动eureka-server
2、启动config-server,查看是否将configserver注册到eureka上
3、启动consumer-server(config Client)
4、访问测试路径,查看输出的属性是否是配置中心配置的

consumer-server在启动过程中会去eureka上获取配置中心configserver的应用信息,配置中心会从配置的Git仓库的根目录下查找到映射的application.yml文件和consumer-server.yml(和应用名相对应)文件,从文件里根据激活的profile获取对应profile下的配置。

如何指定选择GIT上不同分支的配置,比如master和develop上不同的配置,客户端可以配置spring.cloud.config.label=develop来指定要获取的配置分支。

拓展

Spring Cloud Config的配置加密及用户认证和配置刷新,基于篇幅就不记录了,有兴趣的可以去看周立老师的《Spring Cloud 与Docker 微服务架构实战》讲的比较细致,更多学习去翻书吧!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值