一、简介
ConfigServer使用了Spring Cloud Bus之后(引入Spring Cloud Bus用来操作消息队列),会对外提供一个接口,叫做bus-refresh,远端git访问这个接口ConfigServer就会把配置更新的信息发送到消息队列(RabbitMQ)里面,ConfigServer 和order服务通过消息队列(RabbitMQ)来传递消息
二、Spring Cloud Bus实操
1、config-server
- pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imooc</groupId>
<artifactId>config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>config</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<!-- 引入依赖 -->
<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>
<!-- 引入eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- 引入bus-amqp 消息队列-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!-- 引入config-monitor 自动刷新配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<!-- 引入actuator 自动刷新配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 版本依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 打包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- 仓库地址-->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
- bootstrap.yml
spring:
application:
name: config #配置服务名称
#配置config-server
cloud:
config:
server:
git:
uri: https://github.com/AmyZurich/zwj #地址
username: XXX # 用户名
password: XXX # 密码
#配置文件拉取地址
#basedir: /Users/Amy/Documents/ideaproject/config/basedir
rabbitmq:
host: 192.168.3.233
username: guest
password: guest
port: 5672
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
#配置端扣号
server:
port: 8808
management:
endpoints:
web:
exposure:
include: "*"
- ConfigApplication
@SpringBootApplication
@EnableDiscoveryClient //开启发现服务功能
@EnableConfigServer //开启服务端功能
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
- 测试
测试地址:http://localhost:8808/order-dev.yml
2、config-client
- pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<!-- 父项目 -->
<parent>
<artifactId>order</artifactId>
<groupId>com.imooc</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>order-server</artifactId>
<!-- 引入依赖 -->
<dependencies>
<!-- 引入web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 引入mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 引入lombok 可以省去get set方法 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 引入gson 转换数据-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<!-- 引入feign 应用通信-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入order-common-->
<dependency>
<groupId>com.imooc</groupId>
<artifactId>order-common</artifactId>
</dependency>
<!-- 引入product-client -->
<dependency>
<groupId>com.imooc</groupId>
<artifactId>product-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- 引入cloud-config-client 统一配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- 引入bus-amqp 消息队列-->
<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>
<!-- 版本依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 打包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- 仓库地址 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
- bootstrap.yml
spring:
application:
name: order #配置服务名称
#统一配置中心
cloud:
config:
discovery:
enabled: true
service-id: CONFIG #configServer 服务的名字
profile: dev
bus:
trace:
enabled: true
#消息队列
rabbitmq:
host: 192.168.3.233
username: guest
password: guest
port: 5672
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
- OrderApplication
@SpringBootApplication //开启服务发现功能
@EnableDiscoveryClient
//扫描商品的包路径
@EnableFeignClients(basePackages = "com.imooc.product.client")//开启Feign功能
@RefreshScope
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
- Controller
@Data
@Component
@ConfigurationProperties("girl")
@RefreshScope //刷新配置
public class GirlConfig {
private String name;
private Integer age;
}
@RestController
@RefreshScope //刷新配置
public class GirlController {
@Autowired
private GirlConfig girlConfig;
@GetMapping("/girl/print")
public String print() {
return "name:" + girlConfig.getName() + " age:" + girlConfig.getAge();
}
}
- 测试
测试地址:http://localhost:8802/girl/print
3、自动刷新配置
- 获取natapp提供的外网网址,即可实现本地实时开发调试了,natapp教程。
- 打开git上配置仓库的地址,添加webhooks
上面的Payload URL就填写我们的配置中心触发刷新的地址,当然这里不能写localhost啦,要外网访问地址才行。