SpringCloudConfig和bus的基本使用
文章目录
一、SpringCloudConfig服务端配置
(1)添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
(2)修改配置
spring:
application:
name: config-center
cloud:
config:
server:
git:
uri: git地址
search-paths:
- 项目名
username: 用户名
password: 密码
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
(3)启动类
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterApplication3005 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication3005.class,args);
}
}
(4)访问地址
http://config服务的IP:PORT/master/config-test.yml
注意:如果上面没有配置git的用户名和密码,很可能出现如下的错误:
Authentication is required but no CredentialsProvider has been registered
二、SpringCloudConfig客户端配置
(1)引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
</dependency>
(2)创建配置文件bootstrap.yml
server:
port: 3006
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:3005
name: config
profile: test
label: master
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
(3)主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication3006 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication3006.class,args);
}
}
(4) 获取配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@Value("${server.port}")
private String port;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo+"/t"+port;
}
}
(5)使用curl发送post请求
curl -X POST "http://ip:config客户端/actuator/refresh"
当git上出现更新的时候,客户端也会更新,如果不采用上述方式,服务端会更新,客户端不能更新。
以上内容都是单应用节点的,当出现多节点需要引入bus进行下述配置
三、SpringCloudBus的配置
以下使用的是rabbitmq的方式
(1)引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(2)增加rabbitmq的配置
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
(3)使用curl 发送post请求
curl -X POST "http://localhost:3005/actuator/bus-refresh"
如果单独想刷新个别应用,使用如下方式:
curl -X POST "http://localhost:3005/actuator/bus-refresh/${spring.application.name}:${server.port}"
s p r i n g . a p p l i c a t i o n . n a m e 和 {spring.application.name}和 spring.application.name和{server.port}需要替换成当前需要刷新的服务的配置文件中的内容
efresh"
**如果单独想刷新个别应用,使用如下方式:**
```java
curl -X POST "http://localhost:3005/actuator/bus-refresh/${spring.application.name}:${server.port}"
' s p r i n g . a p p l i c a t i o n . n a m e 和 {spring.application.name}和 spring.application.name和{server.port}需要替换成当前需要刷新的服务的配置文件中的内容