目录
🌭1.spring cloud Config是什么😶🌫️😶🌫️😶🌫️
🥓2.能干什么😶🌫️😶🌫️😶🌫️
🍿3.服务端配置😶🌫️😶🌫️😶🌫️
🥞4.客户端配置😶🌫️😶🌫️😶🌫️
🍳5.动态刷新 😶🌫️😶🌫️😶🌫️
1.spring cloud Config是什么😶🌫️😶🌫️😶🌫️
2.能干什么😶🌫️😶🌫️😶🌫️
3.服务端配置😶🌫️😶🌫️😶🌫️
3.1.创建工程😶🌫️😶🌫️😶🌫️
- 1.在父工程下创建
- 2.注意jdk和maven版本号
3.2.添加pom😶🌫️😶🌫️😶🌫️
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--eureka的Client端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--configCenter-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<!-- 不引入这个较旧的、没有维护的库,因为该版本不支持较新版本的RSA加密 -->
<exclusions>
<exclusion>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 该版本支持较新版本的RSA(sha2-256 / sha2-512) -->
<dependency>
<groupId>com.github.mwiede</groupId>
<artifactId>jsch</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
3.3.修改yml😶🌫️😶🌫️😶🌫️
在配置文件之前,先配置好自己的gitee或github
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
#gitee上面的仓库地址
uri: git@gitee.com:hqdmdxz/springcould-config.git
#搜索目录
search-paths:
- sprongcloud-config
#gitee的账号
username: 账号
#gitee的密码
password: 密码
#读取分支
lable: master
#注册到eureka
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
3.4.主启动类😶🌫️😶🌫️😶🌫️
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class);
}
}
3.5.配置类😶🌫️😶🌫️😶🌫️
因为spring-cloud-config-server还不支持github较新的rsa加密方法
@Configuration
public class MyConfig {
//Shim to fix the way jGit configures JSch
static {
JSch.setConfig("signature.rsa", "com.jcraft.jsch.jce.SignatureRSA");
}
}
3.6.测试😶🌫️😶🌫️😶🌫️
4.客户端配置😶🌫️😶🌫️😶🌫️
4.1.创建工程😶🌫️😶🌫️😶🌫️
- 1.在父工程下创建
- 2.注意jdk和maven版本
4.2.添加pom😶🌫️😶🌫️😶🌫️
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--eureka的Client端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--configClient-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
4.3.修改yml😶🌫️😶🌫️😶🌫️
server:
port: 3355
spring:
application:
name: config-client
cloud:
#客户端配置
config:
#分支名称
label: master
#配置文件名称
name: config
#读取后缀名称
profile: dev
#配置中心地址
uri: http://localhost:3344
#服务注册到eureka
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
4.4.主启动类😶🌫️😶🌫️😶🌫️
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class);
}
}
4.5测试😶🌫️😶🌫️😶🌫️
- 1.启动主程序类
- 2.浏览器访问
5.动态刷新 😶🌫️😶🌫️😶🌫️
5.1.修改3355模块😶🌫️😶🌫️😶🌫️
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
5.2.修改yml😶🌫️😶🌫️😶🌫️
暴露监控端点
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
5.3.修改业务类 😶🌫️😶🌫️😶🌫️
在业务类上面添加@RefreshScope注解
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
5.4.发送POST刷新😶🌫️😶🌫️😶🌫️
- 在修改内容之后,手动刷新客户端
curl -X POST "http://localhost:3355/actuator/refresh"