SpringCloud教程第6篇:分布式配置中心(Spring Cloud Config)(Finchley版本)

为什么使用config分布式配置中心

在微服务中,每个功能模块其实都可以拆分成一个单独的服务实例,如果项目够大,必然会有很多服务单元,每个服务单元都有一份配置文件需要维护,这显得不太好维护,而且不方便协作开发。

为了使服务实例的配置文件统一管理化,Spring Cloud Config提供了一套解决方案,建立一个配置服务中心,每个服务单元从config server中获取具体的配置文件,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。

为了保证系统的稳定,配置服务端config server可以进行集群部署,即使某一个实例,因为某种原因不能提供服务,也还有其他的实例保证服务的继续进行。

远程Git仓库:存储配置文件。
ConfigServer:分布式配置管理中心,用于维护自己的git仓库信息。
本地Git仓库:在ConfigServer中,每次客户端请求获取配置信息时,都会从git仓库获取最新的配置到本地,然后本地读取并返回,远程无法获取时,使用本地仓库信息。
从上图可以看出, Config Server 巧妙地通过 git pull 将配置信息存于本地,起到了缓存的作用,即使当 Git 服务端无法访问的时候,依然可以取 Config Server 中的缓存内容进行使用。 

一、简介

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

二、创建一个config server

1、新建一个子module,名为config-server,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>
    <groupId>com.bingo</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>分布式配置中心</description>

    <parent>
        <groupId>com.bingo</groupId>
        <artifactId>cloud-example</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能,代码如下:

package com.bingo;

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

@EnableDiscoveryClient //将服务注册到eureka中
@EnableConfigServer //开启配置服务器的功能
@SpringBootApplication
public class ConfigServerApplication {

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

}

3、需要在程序的配置文件application.properties文件配置以下:

spring.application.name=config-server
server.port=8888

#Git仓库
#配置git仓库地址
#spring.cloud.config.server.git.uri=https://github.com/bingo1994/test/
#配置仓库路径
#spring.cloud.config.server.git.searchPaths=respo
#配置仓库的分支
#spring.cloud.config.label=master
#访问git仓库的用户名
#spring.cloud.config.server.git.username=1445720842@qq.com
#访问git仓库的用户密码
#spring.cloud.config.server.git.password=wb940205



#启用本地文件
spring.profiles.active=native
#在本地新建spring-test-dev.properties文件测试
spring.cloud.config.server.native.searchLocations=file:D:/properties/

#spring.cloud.config.server.native.searchLocations=classpath:/shared

在本地新建spring-test-dev.properties

启动程序:访问http://localhost:8888/spring-test/dev

证明配置服务中心可以从远程或本地获取配置信息。

http请求地址和资源文件映射如下:

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

三、创建一个config client

1、创建一个子module,名为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>
    <groupId>com.bingo</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</name>
    <description>SpringCloud config的客户端</description>

    <parent>
        <groupId>com.bingo</groupId>
        <artifactId>cloud-example</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <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>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、修改配置(bootstrap.yml或者bootstrap.properties,不是application.properties)

spring:
  application:
    name:
      config-client
  cloud:
    config:
      name: spring-test
      profile:
        dev
      uri:
        http://localhost:8888/   #指明配置服务中心的网址
server:
  port: 8881

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/




# dev 开发环境配置文件 |  test 测试环境  |  pro 正式环境
#spring.cloud.config.profile=dev


#指定配置中心名称(如果使用eureka可以这样配置)
#  spring.cloud.config.discovery.service-id=config-server

配置说明:

  • spring.cloud.config.uri:配置中心的具体地址
  • spring.cloud.config.name:对应{application}部分
  • spring.cloud.config.profile:对应{profile}部分
  • spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
  • spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。

3、编写接口测试

package com.bingo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableDiscoveryClient //将服务注册到eureka中
@SpringBootApplication
public class ConfigClientApplication {

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

    @Value("${bingo.config.name}")
    private String name;
    @Value("${bingo.config.password}")
    private String password;

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

 注意:
1、测试会发现中午乱码(去config-server修改)。乱码按网上的测试不行,暂时没弄。
2、当修改文件内容,再次请求接口,返回内容不是最新


refresh手动刷新

spring已经为我们解决了这个问题,那就是客户端使用post去触发refresh,获取最新数据,需要依赖

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


1、对应的controller类加上@RefreshScope
2、配置management.endpoints.web.exposure.include=refresh(只暴露refresh,当然也可以暴露所有:=*)。测试时貌似只支持post方法

 

参考资料:

https://blog.csdn.net/forezp/article/details/81041028

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值