pom文件配置多个远程仓库地址_分布式配置中心Config

本文介绍了Spring Cloud Config配置中心的使用,包括配置中心的概念、案例、原理,以及如何在不重启服务的情况下使配置生效。文章详细讲解了如何配置多个远程仓库地址,客户端和服务端的配置,以及配置文件的命名规则。同时,还涉及到服务端的对称加密和用户安全验证。
摘要由CSDN通过智能技术生成

一 配置中心认识

1 服务配置的现状

2cc75b9c0348068a04ee95a658a071a8.png

2 常用的配置管理解决方案缺点

a、硬编码【缺点:需要修改代码,繁琐、风险大】

b、写在properties里面【缺点:在集群环境下,需要替换和重启】

c、写在xml配置文件中,一般个应用一起打包【缺点:需要重新打包和重启】

3 为什么要使用 spring cloud config 配置中心?

由于常用的配置管理有很大的缺点,故spring cloud config采用了集中式的配置中心来管理每个服务的配置信息。

spring cloud config配置中心,在微服务分布式系统中,采用服务端和客户端来提供可扩展的配置服务。配置中心负责管理所有的服务的各种环境配置文件。配置服务中心默认采用Git的方式存储配置文件,因此我们很容易部署修改,有助于对环境配置进行版本管理。

二 配置中心案例

1 编写配置中心服务端

1.1 创建项目,修改pom文件

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</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> 

1.2 修改启动类

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

1.3 添加测试配置文件

config-client.properties e-book=default1.0

config-client-dev.properties e-book=devt1.0

config-client-test.properties e-book=test.0

config-client-prod.properties e-book=prod1.0

6ce61b00e7e72be93654eabeae7a7e36.png

2 配置文件的命名规则与访问·

f7280125daa5c6b198f090b0d5b7665e.png

3 编写配置中心客户端

3.1 创建项目,修改pom文件

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

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

3.2 修改配置文件的名称。客户端配置文件名称必须为bootstrap.properties(优先级高于

application.properties)项目启动时优先加载该配置文件,否则读取信息会读取不到,因为配置信息都是从配置中心获取】

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

#设置服务注册中心地址,指向另一个注册中心 
eureka.client.serviceUrl.defaultZone=http://eureka1:8080/eureka/,http://eureka2:9090/eureka/

#默认 false,这里设置 true,表示开启读取配置中心的配置 
spring.cloud.config.discovery.enabled=true
#对应 eureka 中的配置中心 serviceId,默认是 configserver
spring.cloud.config.discovery.serviceId=config-server
#指定环境 
spring.cloud.config.profile=dev
#git 标签 
spring.cloud.config.label=master 

3.3 修改启动类

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

3.4 编写测试类

/@RestController
public class ResutController {

	@Value("${e-book}")
	private String msg;
	
	@RequestMapping("/msg")
	public String result() {
		
		return msg;
	}
}

三 配置中心原理

git服务器会从远程git拉取配置文件,并存入到本地git文件库,当远程git不可用时,会从本地git文件库拉取配置信息

本地仓库同步会分为两种方式:

1、git远程仓库发生该变,当config-server服务重启之后,会将远程仓库的内容同步到本地仓库中

2、config-server在运行时,若是有user服务从中获取配置信息,config-server服务会进行版本校验

若不一致,则将远程仓库同步到本地仓库中

2e40e68d5b7feaf5ea8fc73286bcd04e.png

四 git端修该配置,在不重启服务中如何让客户端生效

问题: 在Git端修改配置信息时,如何在不重启服务下,让客户端拿到最新的数据?

在config-server服务运行时,要是有服务向其读取配置信息,他会自动的将配置信息进行更新,所以只需要对

客户端在不需要重启的情况下,让其拿到最新的配置信息即可(配置文件只在启动时加载一次)

解决方案:

1、重启客户端,代价太大,不适用

2、利用springboot的actuator对服务进行刷新,

1 创建项目config-client-refresh

2 修改配置文件,添加坐标

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

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

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

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

3 修改配置文件 项目名要与Git中配置文件的前缀保持一致

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

#设置服务注册中心地址,指向另一个注册中心 
eureka.client.serviceUrl.defaultZone=http://eureka1:8080/eureka/,http://eureka2:9090/eureka/

#默认 false,这里设置 true,表示开启读取配置中心的配置 
spring.cloud.config.discovery.enabled=true
#对应 eureka 中的配置中心 serviceId,默认是 configserver
spring.cloud.config.discovery.serviceId=config-server
#指定环境 
spring.cloud.config.profile=dev
#git 标签 
spring.cloud.config.label=master


#springboot 默认开启了权限拦截 会导致 /refresh 出现 401,拒绝访问 
management.security.enabled=false

4 刷新请求的url

be106b415f9ccf3269b7ad842ddacbcf.png

5 需要配置文件的Bean对象的作用域刷新

//刷新作用域,由于该实例在sping中式单例的,只会被创建一次,想让其重新获取配置文件
//地信息,就要对其加上注解,让其刷新作用域
@RestController
@RefreshScope
public class ResutController {

	@Value("${e-book}")
	private String msg;
	
	@RequestMapping("/msg")
	public String result() {
		
		return msg;
	}
}

6 创建一个能发送post请求的项目

7 修改pom文件

<dependencies>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.4</version>
		</dependency>   
		<!-- log -->
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>

8 添加HttpClient工具类

9 测试

先启动config-server,在启动client-refresh,在Git对配置文件内容进行修改,再用HttpClient工具类进行项目的刷新

在访问config-server中的controller发现配置文件改变

五 服务端对称加密

1 对称加密

1.1、对称加密介绍

对称加密是最快速、最简单的一种加密方式,加密(encryption)与解密(decryption)用的 是同样的密钥(secret key)。

1.2、检查加密环境 http://127.0.0.1:9050/encrypt/status

1.2 创建项目

1.3 修改 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"

检查结果:{"description":"No key was installed for encryption service","status":"NO_KEY"} 没有为加密服务安装密钥。 三、设置加密环境 3 个步骤

1.4.设置秘钥 KEY encrypt.key=oldlu

1.5.未配置 JCE http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.htm l

下载解压后,把 jar 文件上传到需要安装 jce 机器上 JDK 或 JRE 的 security 目录下,覆盖源文件 即可。

JDK:将两个 jar 文件放到%JDK_HOME%jrelibsecurity 下 JRE:将两个 jar 文件放到%JRE_HOME%libsecurity 下

1.6.spring cloud bug Dalston.SR4、Dalston.SR3、Dalston.SR2 版本不能对配置文件加密,若需要调整到 Dalston.SR1

https://github.com/spring-cloud/spring-cloud-config/issues/767

1.7、加密演示

加密(post 请求):http://127.0.0.1:9030/encrypt

解密(post 请求):http://127.0.0.1:9030/decrypt

1.7.1 创建项目,修改pom文件

<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</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>  

1.7.2 修改配置文件

spring.application.name=config-server-encrypt-sym
server.port=9040

#设置服务注册中心地址,指向另一个注册中心 
eureka.client.serviceUrl.defaultZone=http://eureka1:8080/eureka/,http://eureka2:9090/eureka/

#Git 配置 
spring.cloud.config.server.git.uri=https://gitee.com/gao_shuais_grand_master/config
#spring.cloud.config.server.git.username= 
#spring.cloud.config.server.git.password= 

#配置密钥 
encrypt.key=oldlu

1.7.3测试

dc57b8d0dba4df1f5564eb64ceb0b593.png

2 获取加密后的配置信息

一 创建读取配置中心配置内容的项目-配置中心客户端

1 创建项目config-ego-product-provider

2 修改pom文件

3 创建 bootstrap.properties 配置文件

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

#设置服务注册中心地址,指向另一个注册中心 
eureka.client.serviceUrl.defaultZone=http://eureka1:8080/eureka/,http://eureka2:9090/eureka/

#默认 false,这里设置 true,表示开启读取配置中心的配置 
spring.cloud.config.discovery.enabled=true
#对应 eureka 中的配置中心 serviceId,默认是 configserver
spring.cloud.config.discovery.serviceId=config-server
#指定环境 
spring.cloud.config.profile=dev
#git 标签 
spring.cloud.config.label=master

4 创 建 上 传 到 git 远 程 仓 库 的 配 置 文 件 config-e-book-product-provider.properties

mybatis.type-aliases-package=com.sxt.pojo 
mybatis.mapper-locations=classpath:com/sxt/mapper/*.xml 
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/book-product?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull 
spring.datasource.username={cipher}c3429b5888ff1374922fd4451ecb762c675bd7bd062eff77e9291fde3d466172 
spring.datasource.password={cipher}c3429b5888ff1374922fd445 1ecb762c675bd7bd062eff77e9291fde3d466172 

5 将该配置文件上传到 git 的远程仓库中

a6b3d866f029d163122f2c56cbf1cce1.png

6 测试

74cb46cc58c097d2536590f007c5281f.png

六 用户安全验证

1 创建配置中心服务端

1.1 创建项目,修改pom文件

<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</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-security</artifactId>
		</dependency>

1.2 修改配置文件

spring.application.name=config-server-encrypt-sym-secrity
server.port=9040

#设置服务注册中心地址,指向另一个注册中心 
eureka.client.serviceUrl.defaultZone=http://eureka1:8080/eureka/,http://eureka2:9090/eureka/

#Git 配置 
spring.cloud.config.server.git.uri=https://gitee.com/gao_shuais_grand_master/config
#spring.cloud.config.server.git.username= 
#spring.cloud.config.server.git.password= 

#配置密钥 
encrypt.key=oldlu

# 安全认证 #开启基于 http basic 的安全认证 
security.basic.enabled=true
security.user.name=user
security.user.password=123456

2 创建配置中心客户端

1 创建项目,修改pom文件

<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>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

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

2 修改配置文件

spring.application.name=config-e-book-product-provider server.port=9001 
 
#设置服务注册中心地址,指向另一个注册中心 
eureka.client.serviceUrl.defaultZone=http://user:123456@eur eka1:8761/eureka/,http://user:123456@eureka2:8761/eureka/ 
#配置中心服务端的链接信息 #默认 false,这里设置 true,表示开启读取配置中心的配置 
spring.cloud.config.discovery.enabled=true 
#对应 eureka 中的配置中心 serviceId,默认是 
configserver spring.cloud.config.discovery.serviceId=config-server-encry ption-sym 
#git 标签 spring.cloud.config.label=master 
 
#安全保护 
spring.cloud.config.username=user 
spring.cloud.config.password=123456 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值