通过后台动态改变配置参数,实现开关的效果。
前提:假设你已经能够实现通过配置可以获取远程仓库的配置信息。
总结:
(1)验证步骤:
① 先启动config-server,再启动config-client,请求http://localhost:8884/hi,显示“你好zhangjun”
②修改远程仓库的myName值为“zhangjun66”,发送post请求(看清楚是post)访问这个URL“http://localhost:8884/refresh” 显示更新到的属性名称。
③再次请求http://localhost:8884/hi,显示“你好zhangjun66”,完成验证。
(2)踩到的坑
①以上配置针对springcloud的版本:Camden.SR7,目前只知道Finchley.M9版本已经做过变更,原有配置会报错。
为了更加了解清楚,贴出工程所有的配置(特别注意springboot、springcloud的版本信息)
pom文件:
4.0.0
com.jun
config-server
0.0.1-SNAPSHOT
jar
config-server
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.1.RELEASE
UTF-8
UTF-8
1.8
Finchley.M9
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
启动类配置:
package com.jun.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
application.roperties的配置如下:
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/zhangjun0001/springCloudFun/
spring.cloud.config.server.git.searchPaths=properties
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
4.0.0
com.jun
config-client
0.0.1-SNAPSHOT
jar
config-client
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.4.5.RELEASE
UTF-8
UTF-8
1.8
Camden.SR7
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
启动类配置:
package com.jun.configclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@RefreshScope
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${myName}")
String foo;
@RequestMapping(value = "/hi")
public String hi(){
return "你好" + foo;
}
@RequestMapping(value = "/hello")
public String hello(){
return "你好";
}
}
bootstrap.properties的配置:(bootstrap是引导启动参数,不可以配置在application级别的配置文件中(application.**))
spring.application.name=config-client
server.port=8884
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
management.security.enabled=false
前提:已经搭建好注册中心
我的注册中心是:
http://localhost:8761/eureka/
1、改造config-server
pom添加依赖:
org.springframework.cloud
spring-cloud-starter-eureka
启动类添加注解:
@EnableDiscoveryClient
application.properties添加:
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
2、改造config-client
pom添加:
org.springframework.cloud
spring-cloud-starter-eureka
启动类添加注解:
@EnableEurekaClient
bootstrap.properties改为:
spring.application.name=config-client
server.port=8884
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri= http://localhost:8888/
management.security.enabled=false
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
注意:一定要写上(因为这个排查了1个小时,没吃饭)
spring.cloud.config.label=master spring.cloud.config.profile=dev
之前顺手给注释掉了。