分布式配置中心

服务配置的现状

在微服务系统中,每个系统不仅仅只有代码,他还需要连接其他资源,例如数据库的配置或功能性的开关等等。但是随着微服务系统的不断迭代,整个微服务系统可能成为一个网状结构,这个时候就要考虑整个微服务系统的扩展性、伸缩性、耦合性等等。其中一个很重要的环节就是配置管理的问题。
常见的配置类型:
服务配置:
1.数据库配置
2.MQ队列配置
3.redis缓存配置
各类开关
1.功能开关
2.业务开关
3.服务开关
业务配置
1.模块A
2.模块B
3.模块C

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

1.硬编码,(缺点:需要修改代码、繁琐、风险大)
2.写在properties里面(缺点:在集群环境下,需要替换和重启)
3.写在xml配置文件中,一般和应用一起打包(缺点:需要重新打包和重启)

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

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

spring cloud config配置中心,它解决了什么问题?

spring cloud config它解决了微服务配置的中心化、版本控制、品台独立、语言独立等问题。
其特性如下:
1.提供服务端和客户端支持(spring cloud config server 和spring cloud config client)
2.集中式管理分布式环境下的应用配置
3.基于spring环境,无缝与spring应用集成
4.可用于任何语言开发的程序
5.默认实现基于git仓库,可以进行版本管理

配置中心的服务端

配置中心服务端就是加入了连接git的配置,然后如果我们需要修改配置直接在git上修改,我们使用的git是码云速度比较快。
目录结构:
在这里插入图片描述
pom加入依赖:

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

配置文件中的代码:

spring.application.name=config-server
server.port=9030
eureka.client.serviceUrl.defaultZone=http://user:test@eureka2:8762/eureka/,http://user:test@eureka1:8761/eureka/
eureka.instance.perferIpAddress=true
#spring.cloud.config.server.git.uri=https://gitee.com/agan_jiagou/config
spring.cloud.config.server.git.uri=https://gitee.com/自己git/config
spring.cloud.config.server.git.username=自己git的名字
spring.cloud.config.server.git.password=自己git的密码

ConfigServerApplication代码:

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

注意这里配置主要是配置怎么连接git,但是必须是连接项目,所以要先在码云上面创建一个项目:
在这里插入图片描述
上面4个文件自己创建,然后再上传到上面。
这是文件的命名规则:
在这里插入图片描述
启动测试:
在这里插入图片描述

配置中心的客户端

客户端就不需要自己配置文件了,只需要开启读取配置中心的配置就OK了。
目录结构:
在这里插入图片描述
pom中加入依赖:

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

配置文件中加入代码:

spring.application.name=config-client
server.port=9031
eureka.client.serviceUrl.defaultZone=http://user:test@eureka2:8762/eureka/,http://user:test@eureka1:8761/eureka/
eureka.instance.perferIpAddress=true
#默认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

配置文件主要是指定配置中心的服务名。
ConfigClientApplication代码:

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

TestController代码:

@RestController
public class TestController {
	@Value("${book.config}")
	private String msg;
	@RequestMapping("/test")
	public String test() {
		return this.msg;
	}
}

启动测试:
在这里插入图片描述
说明,我们取到了配置中心的值。

配置中心原理

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
服务端自动拉取数据到本地,然后客户端读取本地仓库的数据。

这就是配置中心的简单列子和配置中心的原理,如果想要交流或者demo的话可以加我的qq997355706。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值