准备工作,
在码云上 创建 一个 项目,并在在目录下创建 spring_cloud_in_action/config-repo 层级目录,其中存储了应用名为shendu的多环境配置文件,配置文件中有一个from参数
部署好rabbitmq,可以参考 https://blog.csdn.net/super_rd?t=1
spring boot 版本是 1.5.14
eureka 注册中心
配置文件 |
server.port=1111 eureka.instance.hostname=localhost #由于该应用为注册中心,所有设置为false,代表不向注册中心注册自己 eureka.client.register-with-eureka=false #由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ |
启动类 |
package springcloud.eurekaserver;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer // 注解启动一个服务注册中心 @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } } |
pom 依赖 |
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> |
config server
配置文件 |
spring.application.name=config-server server.port=7001 spring.cloud.config.server.git.uri=https://gitee.com/shenduedu/spring_cloud_config_server/ # 配置仓库路径下的相对搜索位置,可以配置多个 spring.cloud.config.server.git.search-paths=spring_cloud_in_action/config-repo spring.cloud.config.server.git.username=shenduedu spring.cloud.config.server.git.password=a6615925 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
management.security.enabled=false spring.rabbitmq.addresses=192.168.86.132 spring.rabbitmq.port=5672 spring.rabbitmq.username=springcloud spring.rabbitmq.password=springcloud |
启动类 |
package springcloud.config_server_bus;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer;
@EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class ConfigServerBusApplication {
public static void main(String[] args) { SpringApplication.run(ConfigServerBusApplication.class, args); } } |
pom 依赖 |
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> |
config client
spring-boot-starter-actuator,spring-cloud-starter-bus-amqp 必须要加
创建 bootstrap.properties,并写出 |
spring.application.name=shendu spring.cloud.config.profile=dev spring.cloud.config.label=master #spring.cloud.config.uri=http://localhost:7001/ server.port=7002 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ # 开启通过服务来访问config server的功能 spring.cloud.config.discovery.enabled=true # 指定注册的服务名 spring.cloud.config.discovery.service-id=config-server
management.security.enabled=false spring.rabbitmq.addresses=192.168.86.132 spring.rabbitmq.port=5672 spring.rabbitmq.username=springcloud spring.rabbitmq.password=springcloud |
启动类 |
package springcloud.config_client_bus;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient @SpringBootApplication public class ConfigClientBusApplication {
public static void main(String[] args) { new SpringApplicationBuilder(ConfigClientBusApplication.class).web(true).run(args); } } |
controller |
package springcloud.config_client_bus;
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;
@RefreshScope @RestController public class TestController { @Value("${from}") private String from;
@RequestMapping("/from") public String from(){ return this.from; }
} |
pom 依赖 |
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> |
先返回 config_client的from请求,接着修改 dev配置文件的内容
,然后用postman 发送一个请求 到 config_server的 /bus/refresh/
这时再访问 config_client 的from 时已经更新了