大家好,又见面了,我是你们的朋友全栈君。
- 配置管理服务端 更新配置信息需要调用接口地址(post请求)/bus/refresh
1.1配置管理服务端需要的jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!– 注册中心–>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!– kafka消息中心 –>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
1.2.在bootstrap.yml中添加
spring:
application:
name: framework-config-service
#配置文件设置成本地模式
profiles:
active: native
#开启消息总线刷新
cloud:
bus:
enabled: true
config:
server:
native:
search-locations: F:/config/
#设置消息总线的地址
stream:
kafka:
binder:
brokers: 10.1.110.45:9092,10.1.110.44:9092
zkNodes: 10.1.110.45:2181,10.1.110.44:2181
server:
port: 8899
#关闭安全验证
management:
security:
enabled: false
#注册中心
eureka:
client:
service-url:http://localhost:8761/eureka/
1.3.启动项设置
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
@RefreshScope
public class ConfigBoostrap {
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(ConfigBoostrap.class, args);
}
}
-
配置文件管理客户端配置
2.1.需要的jar
<!– kafka消息中心 –>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
<!– 配置文件–>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!–信息采集 –>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.2.消息总线的配置需要在bootstrap.yml中设置
#配置消息总线
spring:
cloud:
stream:
kafka:
binder:
brokers: 10.1.110.45:9092,10.1.110.44:9092
zkNodes: 10.1.110.45:2181,10.1.110.44:2181
#启动配置文件中心读取
config:
discovery:
enabled: true
service-id: framework-config-service
profile: dev
application:
name: framework-common-service
#注册中心
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
security:
enabled: false
server:
port: 8760
2.3.在启动项添加@EnableDiscoveryClient注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
//@MapperScan(basePackages = “com.tzbank.common.dao”)
public class CommonBootstrap {
public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(CommonBootstrap.class, args);
SpringContextUtils.setApplicationContext(applicationContext);
}
}
2.4.需要动态刷新的数据,取值的写法
需要在有刷新的数据的类或者方法上加上
@RefreshScope注解
取值的方法有:
从import org.springframework.core.env.Environment;中取值
@Autowired
Environment environment;
或者直接绑定${value}中的value是对应配置文件的值
@Vlue(“${value}”)
Private String value;
转载于:https://my.oschina.net/youkun/blog/1834460
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/107456.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...