您的位置:首页 > 编程语言 > Java开发

08.Spring Cloud学习笔记之分布式配置中心Spring Cloud Config

2017-09-13 10:26 246 查看

前言

为了更好的解决分布式环境下多台服务实例的配置统一管理问题,Spring Cloud提供了一套完整的分布式配置管理解决方案(Spring Cloud Config)。它用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端和客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息

快速入门

新建Spring-Cloud-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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>spring-cloud-parent</artifactId>
<groupId>com.roberto.springcloud</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>spring-cloud-config-server</artifactId>

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


在启动类添加@EnableConfigServer注解,开启Spring Cloud Config的支持

package com.roberto.springcloud;

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

@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigServerApplication {
public static void main(String args[]) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}


在配置文件中添加如下内容

server:
port: 9094
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/HenryHuang_212/Spring-Cloud-Config-Manager search-paths: pathA,pathB
#         password:
#         username:


上述Git仓库地址为我个人做测试使用的,你们也可以直接使用该配置,详细配置内容可以自行Clone后查看

配置参数详解:

spring.cloud.config.server.git.uri:配置Git仓库地址
spring.cloud.config.server.git.searchPaths:配置仓库路径下的相对搜索位置,可以配置多个
spring.cloud.config.server.git.username:访问Git仓库的用户名
spring.cloud.config.server.git.password:访问Git仓库的密码


访问配置的URL与配置文件的映射关系如下:

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


上面的URL会映射{application}-{profile}.properties/{application}-{profile}.yml对应的配置文件,其中{label}对应Git上不同的分支,默认为maste。如当访问http://localhost:9094/applicationa/dev/master获取到如下配置信息



Spring-Cloud-Config-Server是通过git clone命令将配置内容复制了一份在本地进行存储,然后读取并返回给微服务应用加载,Config-Server通过本地仓库暂存可以有效防止Git仓库出现故障而引起的无法加载配置信息的情况

客户端配置

在确定配置服务中心已经正常运作,下面我们尝试在微服务应用中获取上述配置信息,创建一个实例工程Spring-Cloud-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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>spring-cloud-parent</artifactId>
<groupId>com.roberto.springcloud</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<artifactId>spring-cloud-config-client</artifactId>

<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>
</dependencies>
</project>


添加bootstrap.yml配置文件,内容如下

server:
port: 9095
spring:
cloud:
config:
uri: http://localhost:9094 profile: dev
label: master
application:
name: applicationA


添加Controller,注入配置并验证

package com.roberto.springcloud.controller;

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

@RestController
public class ConfigClientController {
@Value("${profile}")
private String profile;

@GetMapping("/profile")
public String getProfile() {
return this.profile;
}
}


启动项目访问http://localhost:9095/profile查看加载配置是否成功



配置规则对应关系

spring.application.name:对应配置文件规则中的{application}部分
spring.cloud.config.profile:对应配置文件规则中的{profile}部分
spring.cloud.config.label:对应配置文件规则中的{label}部分
spring.cloud.config.uri:对应配置中心Spring-Cloud-Config-Server的地址


注:这里使用的是bootstrap.yml文件进行配置而非application.yml,因为有的配置属性必须在bootstrap.yml文件中配置才能正确加载

占位符配置URI

{application}、{profile}、{label}这些占位符除了用于标识配置文件的规则之外,还可以用于Config Server中对Git仓库地址的URI配置,比如我们可以通过{application}占位符来实现一个应用对应一个Git仓库目录的配置效果,具体配置实现如下

spring:
cloud:
config:
server:
git:
uri: https://gitee.com/HenryHuang_212/{application}[/code] 

模式匹配

server:
port: 9094
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/HenryHuang_212/common-config repos:
applicationA:
pattern: applicationA*/dev*,applicationA*/test*
uri: https://gitee.com/HenryHuang_212/applicationA-config cloneOnStart: false
applicationB:
pattern: applicationB*/dev*,applicationB*/test*
uri: https://gitee.com/HenryHuang_212/applicationB-config cloneOnStart: true


以上配置表示在请求配置文件时候先通过表达式匹配,根据匹配结果来指定Git仓库地址,applicationA*/dev*分别对应配置文件规则中的{application}和{profile},如果正则表达式均未能命中则使用默认Git仓库地址,即spring.cloud.config.server.git.uri的地址

配置加密

Spring Cloud Config提供了对属性进行加解密的功能,以保护配置文件的信息安全,在使用Spring Cloud Config加解密功能时,有一个必要的前提需要我们注意,为了启用该功能,我们需要在配置中心的运行环境中安装不限长度的JCE版本,我们可以从Oracle官网中下载到它,它是一个安装包,解压后可以看到下面三个文件,下载地址:Java Cryptography Extension (jce)

README.txt
local_policy.jar
US_export_policy.jar


我们需要将local_policy.jar,US_export_policy.jar两个文件复制到$JAVA_HOME/jre/lib/security目录下,覆盖原来的内容,到这里加解密的准备工作就完成了

相关端点

/encrypt/status:查看加密功能状态的端点
/key:查看秘钥的端点
/encrypt:对请求body内容进行加密的端点
/decrypt:对请求body内容进行解密的端点


对称加密

在配置文件中指定秘钥信息

encrypt:
key: robertohuang


启动项目访问http://localhost:9094/encrypt/status查看加密功能状态



通过postman发送加密请求



通过postman发送解密请求



非对称加密

Spring Cloud Config的配置中心不仅可以使用对称性加密,还可以使用非对称性加密(比如RSA秘钥对),虽然非对称性加密的秘钥生成与配置相对复杂一下,但是它具有更高的安全性

首先通过keytool工具来生成密钥对,keytool是JDK中的一个秘钥和证书管理工具,它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向其他用户/服务认证自己)或数据完整性以及认证服务,它的位置在%JAVA_HOME%\bin\keytool.exe

生成秘钥的具体命令如下:



如果不想逐步输入那些提示消息,可以使用-dname参数来直接指定,如:

-dname "CN=A,OU=B,O=C,L=D,ST=E,C=F"


秘钥库口令与秘钥口令可使用-storepass和-keypass来直接指定,默认情况下创建的秘钥只有90天有效期,如果想要调整它的有效期,可以通过增加-validity参数来实现,在执行完生成秘钥命令后,会在当前执行目录下生成一个config-server.keystore文件,我们复制该文件到classpath目录下

修改application.yml配置文件,内容如下

encrypt:
key-store:
location: classpath:config-server.keystore
alias: config-server
password: 123456
secret: 123456


通过postman发送加密请求



通过postman发送解密请求



Spring Cloud Config中通过在属性值前使用{cipher}前缀来标注该内容是一个加密值,在微服务客户端加载配置时,配置中心会自动为带有{cipher}前缀的值进行解密,格式如下

paramName: '{cipher}encryptedValue'


安全保护

Spring Cloud Config Server安全保护和Eureka的安全认证配置是一样的,首先在pom.xml添加如下依赖

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


然后修改配置文件,配置认证用户名和密码

security:
user:
name: roberto
password: roberto123


通过如上两个步骤即可为Spring Cloud Config Server完成安全保护设置,由于Spring Cloud Config Server设置了安全保护,在Spring Cloud Config Client连接到该服务获取配置信息的时候会返回401错误,所以需要通过配置的方式在客户端加入安全信息来通过校验,如

spring:
cloud:
config:
username:roberto
passowrd:roberto123


Spring Cloud Config整合Eureka

在Config-Server的pom.xml文件添加如下依赖

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


修改application.yml配置文件添加eureka配置,内容如下:

eureka:
client:
serviceUrl:
defaultZone: http://roberto:roberto123@localhost:8761/eureka instance:
prefer-ip-address: true


在启动类上添加@EnableEurekaClient注解

package com.roberto.springcloud;

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

@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class SpringCloudConfigServerApplication {
public static void main(String args[]) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}


在Config-Client的pom.xml文件添加如下依赖

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


修改bootstrap.yml配置文件,内容如下

server:
port: 9095
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
profile: dev
application:
name: applicationA

eureka: client: serviceUrl: defaultZone: http://roberto:roberto123@localhost:8761/eureka instance: prefer-ip-address: true


在启动类添加@EnableEurekaClient注解

package com.roberto.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class SpringCloudConfigClientApplication {
public static void main(String args[]) {
SpringApplication.run(SpringCloudConfigClientApplication.class, args);
}
}


访问localhost:9095/profile查看加载的配置信息是否正确



动态刷新配置

在Config-Client的pom.xml文件中添加如下依赖

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


在配置文件修改后,通过访问/refresh端点即可动态刷新配置文件



通过返回的数据我们可以知道,profile的参数配置被刷新了

该通能可以与Git仓库的Web Hook功能进行关联,通过Spring Cloud Bus来实现以消息总线的方式进行配置的变更通知,并且完成集群上的批量配置更新
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息