一、Spring Cloud Config
Config Server: 是一个看横向扩展的,集中式的配置服务器,它用于集中管理应用程序各个环境下配置,默认使用Git存储配置内容。
Config Client: 是一个Config Server的客户端,用于操作存储在Config Server上的配置属性,所有微服务都指向Config Server,启动的时候会请求它获取所需要的配置属性,然后缓存这些属性以提高性能。
代码简单实现
1.先配置一个eureka
// eureka server依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
// rureka server 配置
eureka:
instance:
hostname: localhost
instance-id:${spring.application.name}:${spring.application.instance_id:${server.port}}
client:
serviceUrl:
defaultZone: http://localhost:28761/eureka/
# 注册中心职责是维护服务实例,false:不检索服务
fetch-registry: false
# 此应用为注册中心,false:不向注册中心注册自己
register-with-eureka: false
// rureka server 启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class,args);
}
}
2.配置一个cofig服务,用git作为配置文件的远程仓库
// config server依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
// config server配置
server:
port: 8083
eureka:
client:
service-url:
defaultZone: http://localhost:28761/eureka/
register-with-eureka: true
// 可能一个项目中会有多个服务,对应多个配置文件,也就是服务A和服务B对应的配置文件在git上分别对应文件夹A和文件夹B,此时在配置git时可以使用 search-paths 属性,在这里git上的文件夹使用application name来区分的,所以就配了'{application}',注意加引号
spring:
application:
name: config
cloud:
config:
server:
git:
uri: http://gitlab.szjoin.net/jiean/cloud-confibg-server.git
clone-on-start: true
search-paths: '{application}'
username: 远程git用户名
password: 远程git密码
// config server启动类
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
git文件截图如下
启动好之后可以在浏览器中测试有没有配置好,浏览器中直接访问,http://localhost:8083/aquatic-sit.properties ,如果浏览器中可以看到对应的配置文件,说明config Server已配置好,再新建一个服务通过config service 获取 配置,也就是config client
3.创建一个服务,通过config client服务获取远程git上的配置
//依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
// 配置,这个是在bootstrap.yml,因为boostrap用于应用程序上下文的引导阶段,通常用于引导上下文从外部资源获取配置属性,比如Spring Cloud Config Server,或者 解密外部配置文件的属性等。 默认的Config Server地址是localhost:8888. 所以我们只能在bootstrap.yml或者bootstrap.properties中修改。
spring:
application:
name: flaward
cloud:
config:
uri: http://localhost:8083
profile: dev
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:28761/eureka/
register-with-eureka: true
//启动类
@SpringBootApplication
@EnableEurekaClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
以上都配好了,一个简单的获取远程git配置也就完成了,可以在config client编辑一个接口测试一下
@RequestMapping("/test")
public class TestController {
// configStr1对应的是远程git上配置文件中对应的属性,比如Str1spring.datasource.url,
// 浏览器中访问http://localhost:8082/test/getConfig,可以直接获取配置中Str1spring.datasource.url的值
@Value("${configStr1}")
private String configStr1;
@GetMapping("/getConfig")
public String getConfig(){
return configStr1;
}
}
刚对cloud的统一配置进行研究,也是踩过不少坑,最终还是参考这篇博客受到启发,贴上链接:https://blog.csdn.net/zhanglh046/article/details/78652017