Config 分布式配置文件
从git上下载使用配置文件,会自动下载到C盘的缓存文件,随着业务的变动读取升级功能。
基础
https://blog.csdn.net/qq_41520636/article/details/115848274
开始
git准备
选择gitee
创建一个新仓库
下载
https://tortoisegit.org/download/
新建文件config-dev.yml,dev代表开发版本
项目结构
新建config-server项目
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-parent</artifactId>
<groupId>com.hikktn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<dependencies>
<!-- config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- bus -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 8888
spring:
application:
name: config-server
# spring cloud config
cloud:
config:
server:
# git 的 远程仓库地址
git:
uri: https://gitee.com/hete/my_springcloud.git
label: master # 分支配置
#配置rabbitmq信息
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
# 将自己注册到eureka中
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
启动类
package com.hikktn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @ClassName ConfigServerApp
* @Description TODO
* @Author lisonglin
* @Date 2021/4/18 23:31
* @Version 1.0
*/
@SpringBootApplication
// 启用config server功能
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApp {
public static void main(String[] args){
SpringApplication.run(ConfigServerApp.class,args);
}
}
测试
- eureka-server
- config-server
- hystrix-consumer
- hystrix-provider
hystrix-consumer 子工程
注意:不要引入spring-cloud-config-server坐标,否则无法读取git上面的yml文件。
pom新增
<!--config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- bus -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
bootstrap.yml
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
cloud:
config:
# 配置config-server地址
# uri: http://localhost:9527
# 配置获得配置文件的名称等信息
name: config # 文件名
profile: dev # profile指定, config-dev.yml
label: master # 分支
# 从注册中心去寻找config-server地址
discovery:
enabled: true
service-id: CONFIG-SERVER
#配置rabbitmq信息
rabbitmq:
host: 192.168.135.143
port: 5672
username: guest
password: guest
virtual-host: /
# 自动加载url
management:
endpoints:
web:
exposure:
include: '*'
控制器
@RestController
@RequestMapping("/order")
@RefreshScope // 开启刷新功能
public class OrderController {
@Autowired
private GoodsFeign goodsFeign;
@Value("${hikktn.username}")
private String username;
@Value("${hikktn.version}")
private String version;
@GetMapping("/goods/{id}")
public AbstractResult findOne(@PathVariable("id") int id) {
AbstractResult<Goods> result = goodsFeign.findGoodsById(id);
Goods goods = new Goods();
goods.setTitle(result.getData().getTitle()+":"+username+"客户端版本:"+version);
result.setData(goods);
System.out.println("结果:"+result);
return result;
}
}
hystrix-provider 子工程
pom新增
<!--config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- bus -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
bootstrap.yml
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
cloud:
config:
# 配置config-server地址
# uri: http://localhost:8888
# 配置获得配置文件的名称等信息
name: config # 文件名
profile: dev # profile指定, config-dev.yml
label: master # 分支
# 从eureka寻找config链接
discovery:
enabled: true
service-id: config-server
#配置rabbitmq信息
rabbitmq:
host: 192.168.135.143
port: 5672
username: guest
password: guest
virtual-host: /
# 自动刷新配置文件
management:
endpoints:
web:
exposure:
include: '*'
控制器
@RestController
@RequestMapping("/goods")
@RefreshScope // 开启刷新功能
public class GoodsController {
@Autowired
private GoodsService goodsService;
@Value("${hikktn.username}")
private String username;
@Value("${hikktn.version}")
private String version;
public AbstractResult<Goods> findOne(@PathVariable("id") int id) {
Goods goods = goodsService.findOne(id);
//如果id == 1 ,则出现异常,id != 1 则正常访问
if (id == 1) {
//1.造个异常
int i = 3 / 0;
}
goods.setTitle(goods.getTitle() + ":" + port + ":" + username + ",服务端版本:" + version);//将端口号,设置到了 商品标题上
AbstractResult<Goods> result = new AbstractResult<Goods>(1, 200, "服务端正常访问", goods);
return result;
}
}
测试
启动下面五个项目
- config-server
- eureka-server
- api-server
- hystrix-consumer
- hystrix-provider
变更gitee上面的配置文件
测试,访问 gitee上面的配置文件,如果成功,那么config的代码没有问题。
可以看到下面通过不同端口访问,config文件还是没有获取到最新文件。
打开cmd,ctrl+R
输入
curl -X POST http://localhost:8001/actuator/refresh
curl -X POST http://localhost:9000/actuator/refresh
修改不同的ip和端口来更新config,但是接口太多了,很麻烦,使用bus一键刷新。
curl -X POST http://localhost:8888/actuator/bus-refresh
bus的使用