狂神说java,springcloud笔记
Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。
客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。
随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。
概述
分布式系统面临的–配置文件问题
微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,所以一套集中式的,动态的配置管理设施是必不可少的。spring cloud提供了configServer来解决这个问题,我们每一个微服务自己带着一个application.yml,那上百个的配置文件修改起来,令人头疼!
什么是SpringCloud config分布式配置中心
spring cloud config 为微服务架构中的微服务提供集中化的外部支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置。
spring cloud config 分为服务端和客户端两部分。
服务端也称为 分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密,解密信息等访问接口。
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理。并且可用通过git客户端工具来方便的管理和访问配置内容。
其实简单来说就是我们可以通过服务server程序连接远程仓库Remote,客服端client程序通过服务端读取远端的程序。而远端的配置文件就可以代替以前配置到本地Local的application.yml相关配置。
spring cloud config 分布式配置中心能干嘛?
- 集中式管理配置文件
- 不同环境,不同配置,动态化的配置更新,分环境部署,比如 /dev /test /prod /beta /release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启,即可感知到配置的变化,并应用新的配置
- 将配置信息以REST接口的形式暴露
spring cloud config 分布式配置中心与GitHub整合
由于spring cloud config 默认使用git来存储配置文件 (也有其他方式,比如自持SVN 和本地文件),但是最推荐的还是git ,而且使用的是 http / https 访问的形式。
示例
在码云新建一个仓库
- 我仓库的https是 https://gitee.com/dongjixue/springcloud_config.git
- 写一个application.yml 用于server程序连接码云仓库的测试。
spring:
profiles:
active: dev
---
spring:
profiles: dev
application:
name: spring-cloud-config-dev
---
spring:
profiles: test
application:
name: spring-cloud-config-test
- 写一个application-client.yml作为客服端程序远程的application.yml配置。
spring:
profiles:
active: dev
---
server:
port: 9003
spring:
profiles: dev
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
---
server:
port: 9004
spring:
profiles: test
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
新建子模块 springcloud-config-server 作为服务端程序
- pom.xml
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
- 创建application.yml配置文件,Spring Cloud Config服务器从git存储库(必须提供)为远程客户端提供配置:
server:
port: 9001
spring:
application:
name: spring-boot-service
#连接远程仓库
cloud:
config:
server:
git:
# 注意是远程仓库的https的而不是ssh
uri: https://gitee.com/dongjixue/springcloud_config.git
# 通过 config-server可以连接到git,访问其中的资源以及配置~
- 主启动类
package com.dong.config;
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
- 启动启动类进行测试
访问:http://localhost:9001/application-dev.yml
结果:成功连接到远程,并且访问到application
spring:
application:
name: spring-cloud-config-dev
profiles:
active: dev
使用另一种方式访问:http://localhost:9001/master/application-dev.yml 结果同上
在使用一种方式访问:http://localhost:9001/application/master
结果如下:
{"name":"application","profiles":["master"],"label":null,"version":"df6a6cbe9d0998045ad8a9ddbf3000c702b6689e","state":null,"propertySources":[{"name":"https://gitee.com/dongjixue/springcloud_config.git/application.yml (document #0)","source":{"spring.profiles.active":"dev"}}]}
其它访问方式见下面。
- config远程访问的方式介绍
新建springcloud-config-client 作为客服端
- pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>
<version>1.4.6.RELEASE</version>
</dependency>
- bootstrap.yml 是系统级别的配置
# 系统级别的配置
spring:
cloud:
config:
name: application-client # 需要从git上读取的资源名称,不要后缀
profile: dev
label: master #label
uri: http://localhost:9001 #此处连接服务端地址
- application.yml 是用户级别的配置
# 用户级别的配置
spring:
application:
name: springcloud-config-client
- controller
package com.dong.configclient.controller;
@RestController
public class ConfigClientController {
@Value("${spring.application.name}")
private String applicationName; //获取微服务名称
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServer; //获取Eureka服务
@Value("${server.port}")
private String port; //获取服务端的端口号
@RequestMapping("/config")
public String getConfig(){
return "applicationName:"+applicationName +
"eurekaServer:"+eurekaServer +
"port:"+port;
}
}
- 主启动类
package com.dong.configclient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class,args);
}
}
- 测试启动eureka, springcloud-config-server和springcloud-config-client我们访问:http://localhost:7001/ 发现程序成功通过远程的eureka注册配置注册到了注册中心。
访问:http://localhost:9003/config结果:成功读取到远程的配置。
applicationName:springcloud-config-client
eurekaServer:http://localhost:7001/eureka/
port:9003
- 总结:通过搭建一个服务端程序去连接远程仓库,springcloud任意一个子模块我们都可以作为客服端通过访问服务端读取到远程配置。我们可以把一些公共的配置,如注册Eureka,连接数据库等等放到远程,这样客服端程序只需要读取远程的即可。不需要重复配置相同繁琐的配置。在远程还方便统一修改。
手动刷新数据
能解决什么问题
通过config配置中心,我们能够读取到远程的配置内容。当远程内容发生改变的时候,客服端程序不能及时刷新。需要通过重启客服端程序才能获取最新的配置。这种做法在不合理的,会造成很多的损失。
我们可以将远程文件:application-client.yml的profiles: dev的配置添加一个config.info配置,启动程序我们访问到的version是1,然后该version的值为2。然后继续访问发现值依然是1。这是因为没有刷新的原因。这时我们只能重启程序,才能访到version的值是2。
config:
info: "version=1"
修改springcloud-config-client模块
- 需要加监控,以便自己发生改变的时候能够被监控到。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- application.yml
# 暴露监控的端点
management:
endpoints:
web:
exposure:
include: "*"
- controller类上加一个刷新注解:@RefreshScope
package com.dong.configclient.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${spring.application.name}")
private String applicationName; //获取微服务名称
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServer; //获取Eureka服务
@Value("${server.port}")
private String port; //获取服务端的端口号
@Value("${config.info}")
private String info;
@RequestMapping("/config")
public String getConfig(){
return "applicationName:"+applicationName +"<br/>"+
"eurekaServer:"+eurekaServer +"<br/>"+
"port:"+port+"<br/>"+
"info"+info;
}
}
- 启动程序,将远程配置中version=1改为2 访问http://localhost:9003/config
结果:
applicationName:springcloud-config-client
eurekaServer:http://localhost:7001/eureka/
port:9003
infoversion=1
- 这时需要运维人员访问:POST方式访问: "http://localhost:9003/actuator/refresh"进行刷新客服端。
上面的方式是在命令台post方式访问链接,我们也可以通过postman以post方式访问该连接。
- 然后在访问:http://localhost:9003/config
结果成功刷新:
applicationName:springcloud-config-client
eurekaServer:http://localhost:7001/eureka/
port:9003
infoversion=2
参考教程 https://www.kuangstudy.com/