#|spring-cloud-config使用
实现功能 可通过git仓库或本地文件进行配置 支持手动更新配置查看客户端配置
本文代码 https://github.com/tothis/spring-cloud-learn/tree/master/config
http://localhost:8082/config-client/dev
http://localhost:8082/config-client-dev.json
http://localhost:8082/config-client-dev.yaml
http://localhost:8082/config-client-dev.yml
http://localhost:8082/config-client-dev.properties
刷新配置
curl -X POST http://localhost:8082/actuator/bus-refresh
properties yml 相同的键显示properties的值
配置中心服务端
- pom.xml
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-bus-amqp
- application.yml
server:
port: 8002
spring:
application:
name: config-server
profiles:
# 使用本地文件时需写入native
active: native
rabbitmq:
host: 192.168.92.134
port: 5672
username: guest
password: guest
cloud:
# https://docs.spring.io/spring-cloud-config/docs/2.2.4.RELEASE/reference/html
config:
label: master
server:
# git:
#uri: https://github.com/tothis/spring-cloud-learn
#search-paths: config/profile
native:
# search-locations: classpath:profile
search-locations: file:D:/IDEAProjects/project/spring-cloud-learn/config/profile
eureka:
client:
service-url:
defaultZone: http://192.168.92.134:8001/eureka
management:
endpoints:
web:
# 默认前缀为'/actuator'
# base-path: /
exposure:
# include: bus-refresh
include: '*'
- ConfigServerApplication.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
【#|spring-cloud-config使用】配置中心客户端
- pom.xml
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-bus-amqp
- bootstrap.yml
# bootstrap配置优先于application配置文件 配置不会被覆盖
spring:
cloud:
config:
label: master
profile: dev
# 配置中心服务地址 使用eureka后此配置失效
# uri: http://localhost:8002
discovery:
enabled: true
serviceId: config-server
eureka:
client:
service-url:
defaultZone: http://192.168.92.134:8001/eureka
- application.yml
server:
port: 9001
spring:
application:
name: config-client
rabbitmq:
host: 192.168.92.134
port: 5672
username: guest
password: guest
- ConfigClientApplication.java
package com.example;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
/**
* 当前controller支持热刷新配置中心配置
*/
@RefreshScope
@SpringBootApplication
public class ConfigClientApplication {@Value("${version}")
private String test;
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}/**
* a.properties a.yml 相同的键显示properties的值
*/
@GetMapping
public String test() {
return test;
}
}
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入