一、环境
jdk:12
spring cloud:Greenwich.RELEASE
spring boot:2.1.0.RELEASE
spring-cloud-starter-feign:2.0.0.M2
eurekaServer: 前面提到用于服务注册
eurekaClient: 前面提到用于提供服务
configServer: 将新增用于提供分布式配置服务
二、创建分布式配置中心
1.建立一个名为configServer的maven工程,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.bry.springcloud</groupId>
<artifactId>configServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>configServer</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>12</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</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>
- 配置文件 application.yml 的内容如下:
spring:
application:
name: configServer
cloud:
config:
label: master
server:
git:
uri: https://admin@localhost:8443/r/springcloudconfig.git/
searchPaths: respo
username: admin
password: admin
server:
port: 6547
https://admin@localhost:8443/r/springcloudconfig.git/为我本地用gitblit搭建的一个git仓库地址
- 编写启动类 ConfigServerApplication.java
package com.bry.springcloud.eurekaserver;
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);
}
}
增加@EnableConfigServer表示这是一个配置服务器
三、改造eurekaClient工程
1.在 pom.xml 文件中增加新的依赖
......
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<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>
......
- 新增bootstrap.yml文件以配置 分布式配置中心
spring:
cloud:
config:
label: master
profile: dev
uri: http://localhost:6547/
label: master表示使用git上的master分支
http://localhost:6547/ 为配置服务地址
- 修改application.yml文件以增加 rabbitmq 连接信息和开放bus-refresh访问
......
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: admin
password: admin
management:
endpoints:
web:
exposure:
include: bus-refresh
......
5.修改服务提供文件 HelloService.java
package com.bry.springcloud.eurekaclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class HelloService {
@Value("${eureka.instance.hostname}")
String server;
@Value("${server.port}")
String port;
@Value("${greeter}")
String greeter;
@RequestMapping("/hello")
public String sayHello(String name) {
return "Hello " + name + " from " + server + ":" + port + ", I am " + greeter;
}
}
增加@RefreshScope表示该文件的自定义注入属性也可以刷新
四、测试
- 往 git仓库 https://admin@localhost:8443/r/springcloudconfig.git/ 上放入一个名为eurekaClient-dev.properties的文件(<>-.properties),内容如下:
greeter=Andy
- 启动eurekaServer工程
- 启动RabbitMQ
- 启动configServer工程
- 启动eurekaClient工程
- 在浏览器访问地址 http://localhost:6544/hello?name=bruce 可得反馈内容如下:
- 修改https://admin@localhost:8443/r/springcloudconfig.git/ 上的eurekaClient-dev.propertis的文件,内容改成如下:
greeter=Tom
- 向地址http://localhost:6544/actuator/bus-refresh发送post请求
- 在浏览器访问地址 http://localhost:6544/hello?name=bruce 可得反馈内容如下:
参考
分布式配置中心
消息总线
Spring Cloud Bus
Management Plugin
Spring Cloud Config Server
HttpClient接口测试遇到的问题及解决方案
Spring Cloud Bus的使用
备注:
可以从 https://github.com/zhoupinheng/springclouddemo 下载完整代码。