0
点赞
收藏
分享

微信扫一扫

三、Spring cloud之服务器配置和客户端配置

一、搭建Spring Cloud Config Server

引入依赖

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

(一)基于文件系统(File System)

(1)创建本地仓库

1、激活应用配置服务器

在引导类上标注​​@EnableConfigServer​

/**
* 注解:@EnableConfigServer,激活配置服务端
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

2、创建本地目录
  理解 Java 中的 ​​​${user.dir}​​​:简单点说,就是当前项目所在物理路径。比如项目所在目录为 ​​E:/spring-cloud​​​ ,那么 ​​user.dir = E:/spring-cloud​​​。
  在IDEA 中​​​\src\main\resources​​​目录下,创建一个名为 configs 目录,它的绝对路径:​​${user.dir}\config-server\src\main\resources\configs​

3、配置 git 本地仓库 URI(在 application.properties中配置)

#配置服务器文件系统 git 仓库(PS:user.dir = E:\springDemo\spring-cloud-basis 项目根目录)
#使用 ${user.dir} 减少平台文件系统的不一致
spring.cloud.config.server.git.uri: ${user.dir}\config-server\src\main\resources\configs

  application.properties 配置信息如下:

#配置服务端应用名称
spring.application.name=spring-cloud-config-server
#配置服务端应用端口
server.port=9090


#开启 actuator 所有端点
management.server.port=9091
# * 在yml文件中属于关键字,需要加引号"*"
management.endpoints.web.exposure.include=*

#配置服务器文件系统 git 仓库(PS:user.dir = E:\springDemo\spring-cloud-basis 项目根目录)
#使用 ${user.dir} 减少平台文件系统的不一致
spring.cloud.config.server.git.uri: ${user.dir}\config-server\src\main\resources\configs

4、给应用 myapplication 创建三个环境(profile)的配置文件

//默认(default)应用配置文件
myapplication.properties:

//测试环境(test)应用配置文件
myapplication-test.properties

//生产环境(prod)应用配置文件
myapplication-prod.properties

5、在本地目录中创建 git 仓库

//进入本地目录
cd E:\springdemo\spring-cloud-basis\config-server\src\main\resources\configs`

//创建 git 仓库
git init .
//添加文件进 git 仓库,并提交
git add .
git commit -m "第一次提交"

(2)测试配置服务器
  通过浏览器测试应用为“myapplication”,Profile 为“test”的配置内容:​​​/{application}/{profile}[/{label}]​​​(“[]”代表可选)
  案例:​​​http://localhost:9090/myapplication/test​

{

"name": "myapplication",
"profiles": [
"test"
],
"label": null,
"version": "5aedc815b44412047ea27dfb4811a7bba3bc5c73",
"state": null,
"propertySources": [
{
"name": "E:\\springDemo\\spring-cloud-basis\\config-server\\src\\main\\resources\\configs/myapplication-test.properties",
"source": {
"name": "myapplication-test"
}
},
{
"name": "E:\\springDemo\\spring-cloud-basis\\config-server\\src\\main\\resources\\configs/myapplication.properties",
"source": {
"name": "myapplication"
}
}
]

}

  注意:当指定了 Profile 时,默认的 Profile (不指定)配置信息也会输出。

(二)基于远程 git 仓库

1、激活应用配置服务器

在引导类上标注​​@EnableConfigServer​

2、配置远程 git 仓库地址

#配置服务器远程 git 仓库(GitHub)
spring.cloud.config.server.git.uri: https://github.com/pc-nju/spring-cloud-config.git

3、本地 clone 远程 git 仓库

git clone https://github.com/pc-nju/spring-cloud-config.git

4、在克隆下来的项目中,创建三个环境(profile)的配置文件

//默认(default)应用配置文件
myapplication.properties

//测试环境(test)应用配置文件
myapplication-test.properties

//生产环境(prod)应用配置文件
myapplication-prod.properties

5、提交本地配置文件到远程 git 仓库

//增加文件
git add .
//提交到本地仓库
git commit -m "增加配置文件"
//推送到远程仓库
git push

6、配置强制拉取远程 git 内容

spring.cloud.config.server.git.force-pull: true

  测试案例同上!

(三)“基于文件系统”和“基于远程 git 仓库”对比:
  最大的不同是:一旦配置文件发生改变,分布式系统中所有模块都会从远程 git 仓库中拉取到最新的配置信息。

二、配置 Spring Cloud 配置客户端(Spring Cloud Config Client)

引入依赖

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

1、创建 Spring Cloud Config Client 应用

  创建一个名为 config-client 应用

2、ClassPath 下创建 bootstrap.properties

3、配置 bootstrap.properties 和 application.properties

  配置以 spring.cloud.config. 开头的配置信息

#配置 客户端应用 关联的 应用(通过该选项与 服务端 相连)
# spring.cloud.config.name 是可选的,若未配置,采用 ${spring.application.name
}
spring.cloud.config.name=myapplication

#关联 profile
spring.cloud.config.profile=prod

#关联 label
spring.cloud.config.label=master

#配置 配置服务器URI
spring.cloud.config.uri=http://127.0.0.1:9090=http://127.0.0.1:9090

  配置 application.properties 信息:

#配置客户端应用名称
spring.application.name=spring-cloud-config-client
#配置客户端应用端口
server.port=8080

#开启 actuator 所有端点
management.server.port=8081
# * 在yml文件中属于关键字,需要加引号"*"
management.endpoints.web.exposure.include=*

4、启动 客户端应用(服务端应用 要先启动)

2018-11-01 18:43:54.280  INFO 7860 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://127.0.0.1:9090
2018-11-01 18:44:01.630 INFO 7860 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=myapplication, profiles=[prod], label=master, version=375ff9699274bb4dbc763f0ae4f52eebfaf17afd, state=null
2018-11-01 18:44:01.631 INFO 7860 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/pc-nju/spring-cloud-config.git/myapplication-prod.properties'}, MapPropertySource {name='https://github.com/pc-nju/spring-cloud-config.git/myapplication.properties'}]}

三、测试Spring Cloud 配置客户端
  通过浏览器访问http://localhost:8081/actuator/env(PS:如果访问404,可以将 客户端应用 的 application.properties 文件改为 application.yml文件,即可访问)

{
"name": "configService:configClient",
"properties": {
"config.client.version": {
"value": "375ff9699274bb4dbc763f0ae4f52eebfaf17afd"
}
}
}, {
"name": "configService:https://github.com/pc-nju/spring-cloud-config.git/myapplication-prod.properties",
"properties": {
"name": {
"value": "myapplication.com"
}
}
}, {
"name": "configService:https://github.com/pc-nju/spring-cloud-config.git/myapplication.properties",
"properties": {
"name": {
"value": "myapplication"
}
}
}

  通过具体的配置项 name :​​http://localhost:8081/actuator/env/name​​

四、分布式配置架构
  通过以上,总结得出 Spring Cloud Config 分布式架构
三、Spring cloud之服务器配置和客户端配置_spring

五、动态配置属性 Bean
  和“自动装配”很相似,参见​三十四、Springboot 自动装配 1、自定义配置属性 Bean

public class User {
private Long id;
private String name;
}

2、将 User 关联配置项

import org.pc.domain.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author 咸鱼
* @date 2018/11/1 19:38
*/
@Configuration
@ConditionalOnProperty(prefix = "my.user", name = "enabled", matchIfMissing = true)
public class UserConfiguration {
@Bean
@ConfigurationProperties(prefix = "my.user")
public User user(){
return new User();
}
}

3、  通过浏览器访问http://localhost:8081/actuator/env

"my.user.id": {
"value": "1",
"origin": "class path resource [application.properties]:12:12"
}, "my.user.name": {
"value": "pc",
"origin": "class path resource [application.properties]:13:14"
}

4、通过 Postman 调整配置项(不适用于Spring Boot2.0.6)
  POST 方法提交参数 my.user.id=001 到​​​/env​

my.user.id=001
my.user.name=pc

  调整后在本地访问该 user 时,内容就会变化。

  **问题:**若 spring-cloud-config-client 需要调整所有机器的配置如何操作?
  注意:配置客户端应用的所关联的分布式配置内容,优先于传统application.properties(application.yml)或者 bootstrap.properties(boostrap.yml).
  解决办法如下:
(1)调整 远程git仓库 配置文件信息:​​​myapplication-prod.properties​

#应用名为 myapplication 线上环境配置
name = myapplication.com
#动态配置属性 Bean(调整的地方)
my.user.id = 007
my.user.name = rose

(2)将调整后的配置文件推送到远程 git 仓库

(3)刷新本地配置文件
  我们在更新了远程的配置文件之后,客户端应用 无法感知远程仓库中的配置文件发生了变化,所以需要我们手动去刷新本地配置文件。手动刷新本地配置文件有两种方式:

  方式一:/refresh Endpoint
  在更新了远程的配置文件之后,用Postman,调用 ​​​http://localhost:8081/refresh​​ ,POST请求。

  方式二:编码方式(ContextRefresher)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Set;

/**
* 注解@EnableScheduling:开启定时任务,激活@Scheduled
*/
@SpringBootApplication
@EnableScheduling
public class ConfigClientApplication {
/**
* 这是用来刷新 客户端应用 远程配置的工具类
*/
private final ContextRefresher contextRefresher;
@Autowired
public ConfigClientApplication(ContextRefresher contextRefresher) {
this.contextRefresher = contextRefresher;
}

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

/**
* 1秒中刷新配置文件一次
*/
@Scheduled(fixedRate = 1000L)
public void update(){
Set<String> keys = contextRefresher.refresh();
if (!keys.isEmpty()){
System.out.println("本次更新的配置项:" + keys);
}
}
}

  第二种方式更实用,但是得控制好刷新的频率!!!!


举报

相关推荐

0 条评论