Spring Cloud入门2——配置中心Config

Spring Cloud Config

配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。

也就是说,我们可以使用Spring Cloud Config来获取远程服务器上的配置信息。

可以分为两个部分:

服务端: 配置服务端,服务管理配置信息
客户端:客户端调用server端暴露接口获取配置信息

构建服务端

pom文件中增加以下依赖:

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

在程序application中添加@EnableConfigServer注解,表明这是一个Config Server端

@SpringBootApplication
@EnableConfigServer
public class SpringcloudConfigApplication {

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

在application.properties中配置远程仓库信息

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

# 远程仓库地址,具体到项目
spring.cloud.config.server.git.uri=...
# 项目下的具体路径可以配置多个
spring.cloud.config.server.git.searchPaths=...
# 用户名
spring.cloud.config.server.git.username=...
# 密码
spring.cloud.config.server.git.password=...

当然,我们也可以加载本地的配置文件,使用spring.profiles.active=native就会自动搜索resources目录下的文件。

也可以通过设置spring.cloud.config.server.native.searchLocations的方式指定具体位置。

在远程仓库里面存放四个分别代表不同环境的配置文件:

《Spring Cloud入门2——配置中心Config》

都存放了一个from属性,分别赋予不同的值。

启动服务端之后,我们可以通过url的方式对配置文件进行访问,访问关系如下:

url:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
上面的url会映射{application}-{profile}.properties对应的配置文件,{label}对应git上不同的分支,默认为master。

所以我们启动之后,可以通过http://localhost:7001/application/dev访问上面的application.dev文件

《Spring Cloud入门2——配置中心Config》

现在服务端已经完成了,那我们如何在客户端中获取服务端的配置呢?

新建一个项目,还是首先添加依赖:

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

然后再
bootstrap配置文件中增加如下配置:

spring.application.name=application
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/
server.port=7002

需要配置在bootstrap中才能生效,而不是在application配置文件中,bootstrap配置文件会比application更加早的加载。

上面的属性和url中的是一一对应的。

配置完成之后,我们要获取其中的属性,这个很显然使用@Value注解获取的。

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

当我们访问localhost:7002/from的时候,就可以得到配置中的from的值了。

在微服务架构中,就要实现高可用的服务,配置中心也是如此,所以我们需要将配置中心也注册在注册中心中,这样客户端就可以以服务的访问方式来获取配置信息了。

我们需要怎么做呢?

要在注册中心中进行注册,显然需要Eureka的依赖:

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

并且在application配置文件中向注册中心注册配置中心。

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

并且需要将配置中心作为一个服务客户端注册进去,所以主类中添加@EnableDiscoveryClient注解。

这样配置中心就注册完成了。

配置中心在服务注册中心中进行注册了,所以我们客户端就不再需要使用url对他进行访问了,而是使用注册中心中的服务来对他进行调用。

要调用在注册中心中的服务,首先自己也得在注册中心中注册一下:

加入依赖:

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

并且在bootstrap配置文件中添加如下配置:

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server

第一行:在注册中心中进行注册

第二行:启动服务发现的功能,开启了才能调用其它服务

第三行:发现的服务的名字

最后,在application主类中添加@EnableDiscoveryClient就完成了。

同样访问localhost:7002/from,可以得到from的值。

配置刷新

在上面的配置中,客户端是如何获得配置中心的配置信息的呢?是在bootstrap配置文件中配置,在程序启动时加载获取的。显然,在加载完成之后,如果配置中心中的信息发生变化的话,客户端的信息是不会跟着变化的,这是实际开发过程中所不允许的。我们应该怎么做呢?

我们需要增加一个监控模块:spring-boot-starter-actuator

在客户端中增加如下依赖:

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

并且在Controller中增加@RefreshScope注解。

最后,actuator给我们提供了一个/refresh接口,修改完git仓库信息之后,向这个接口发送一个POST信息,就会更新配置信息了。

    原文作者:Spring Cloud
    原文地址: https://blog.csdn.net/a60782885/article/details/69415527
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞