Spring Cloud 系列(二) 配置中心-Config

序号名称链接
1 Spring Cloud 系列(二) 配置中心-Confighttps://blog.csdn.net/qq_38130094/article/details/91456800
2Spring Cloud系列(三) Netflix Eureka注册中心https://blog.csdn.net/qq_38130094/article/details/93992709
3Spring Cloud 系列(四) Netflix ribbon负载均衡https://blog.csdn.net/qq_38130094/article/details/97240915
4Spring Cloud 系列(五) Netflix Hystrixhttps://blog.csdn.net/qq_38130094/article/details/97626558

配置中心简介

       Config简介

Spring Cloud Config是SpringCloud团队创建,用来为SpringCloud微服务架构提供分布式 配置支持。

分为服务端与客户端两部分:

  1. 服务端:也成为分布式配置中心,是一个独立的微服务应用,用来连接配置仓库并未客户 端提供获取配置信息等接口。
  2. 客户端:是微服务架构中各微服务应用或基础设施,通过指定的配置中心来管理应用资源 与业务相关的配置内容。启动时从配置中心加载配置信息

集群架构图:

单点架构图:


在spring cloud中,有分布式配置中心组件spring cloud config,它支持配置文件放在在配置服务的内存中,也支持放在远程Git仓库里。引入spring cloud config后,我们的外部配置文件就可以集中放置在一个git仓库里,再新建一个config server,用来管理所有的配置文件,维护的时候需要更改配置时,只需要在本地更改后,推送到远程仓库,所有的服务实例都可以通过config server来获取配置文件,这时每个服务实例就相当于配置服务的客户端config client,为了保证系统的稳定,配置服务端config server可以进行集群部署,即使某一个实例,因为某种原因不能提供服务,也还有其他的实例保证服务的继续进行。它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

     1.  配置中心服务端

  • 依赖 spring-cloud-config-server
  • 启动类添加 @EnableConfigServer 注解
  • spring.cloud.config.server.git.uri
  • endpoints.env.sensitive = false

1.1创建配置中心服务端项目( 版本为1.5.2)

创建一个取名为spring-cloud-config-server-demo,其pom.xml:


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.20.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Edgware.SR5</spring-cloud.version>
	</properties>

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

1.2启动类

在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能,

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

/**
 * Config配置中心启动类 加上@EnableConfigServer注解
 */
@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigServerDemoApplication {

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

1.3配置文件application.properties文件配置以下:

#配置中心的实例名
spring.application.name=config-server
## 配置中心提供端口
server.port=9090
## 配置中心的仓库的地址
spring.cloud.config.server.git.uri=https://github.com/ssy-githup/config

## 配置中心本地备份地址
spring.cloud.config.server.git.basedir=git-config

### 本地文件目录
#spring.profiles.active=native
#spring.cloud.config.server.native.search-locations=file:///E:/SpringCloud/config

## Actuator
#先把Actuator的安全验证关闭
management.security.enabled=false
	#http://127.0.0.1:8090/health
	   #服务到底可不可用,可能还
	#http://127.0.0.1:8090/env
	#http://127.0.0.1:8090/beans

1.4测试配置中心服务端

访问地址:http://127.0.0.1:9090/yunxi-dev.properties  出现和gitHup上一样的信息即可


 

  2.  配置中心客户端

  • 依赖spring-cloud-config-client
  • spring.cloud.config.uri
  • spring.cloud.config.name
  • spring.cloud.config.profile
  • spring.cloud.config.label
  • endpoints.env.sensitive = false
  • endpoints.refresh.sensitive = false

2.1创建配置中心客户端项目(版本为1.5.2)

创建一个取名为spring-cloud-config-client-demo,其pom.xml:


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.20.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Edgware.SR5</spring-cloud.version>
	</properties>

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

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

2.2配置文件文件

application.properties文件

## 客户端的实例名
spring.application.name=config-server
## 客户端提供端口
server.port=8090

## 配置Actuator 开关
management.security.enabled=false

bootstrap.properties文件 项目启动时加载的的文件

## Config Server配置信息
## 配置中心的服务地址
spring.cloud.config.uri=http://localhost:9090/
## Environment 的 application 客户端的应用名称
spring.cloud.config.name=yunxi
## spring.profiles.active配置
spring.cloud.config.profile=dev
## 配置的版本(git/svn 分支)
spring.cloud.config.label=master

 

2.3启动类

在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringCloudConfigClientDemoApplication {

	@Value("${name}")
	private String name;

	@GetMapping("/hello")
	public String getName(){
		return name;
	}
	public static void main(String[] args) {
		SpringApplication.run(SpringCloudConfigClientDemoApplication.class, args);
	}
}

2.3测试使用客户端

访问地址:http://127.0.0.1:8090/hello  出现和服务端上一样的name信息


3、动态配置属性Bean

  • @RefreshScop
  • RefreshEndpoint
  • ContextRefresher

config-server配置中心服务端每次刷新都会去拿最新的数据

config-clint 配置中心客户端则会缓存数据

这就造成了:当我修改git上的配置文件提交后,客户端不会更新数据

解决方案一:

1:调用客户端的refresh() 方法;这样会拉去到新的属性值;弊端:不能说每次修改配置文件后都要调用这个refresh()方法

2:加入注解 @RefreshScope

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@RefreshScope
public class SpringCloudConfigClientDemoApplication {

	@Value("${name}")
	private String name;

	@GetMapping("/hello")
	public String getName(){
		return "测试="+name;
	}
	public static void main(String[] args) {
		SpringApplication.run(SpringCloudConfigClientDemoApplication.class, args);
	}
}

调用:http://127.0.0.1:8090/hello 可以看到name属性会随着git上文件的变化而变化

解决方案二:

实现自动刷新

 

4、高可用分布式配置中心

  • 传统模式

  • 服务模式

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值