配置文件
新建一个文件夹,创建application-test.properties文件,内容是:
name=default
test=www
提交到svn库
server端
- 新建spring boot项目, 命名为configServer
- pom.xml 引入两个包
<!-- 服务端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- svn依赖包 -->
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
</dependency>
- application.yml
server:
port: 8888
spring:
cloud:
config:
enabled: true
server:
svn:
uri: http://localhost:81/svn/StartKit/cloudConfig
username: mi #svn用户名
password: mi #svn密码
default-label: config #读取目录,默认是trunk
profiles:
active: subversion
logging:
levels:
org.springframework.boot.env.PropertySourcesLoader: TRACE
org.springframework.cloud.config.server: DEBUG
- ServerApplication
@SpringBootApplication
@EnableConfigServer
public class ServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ServerApplication.class).web(true).run(args);
}
}
-
启动项目,访问 http://localhost:8888/application/test, 注意命名,application是application-test.properties的前缀,test是后缀
-
请求结果
{"name":"application","profiles":["test"],"label":null,"version":"34","state":null,"propertySources":[{"name":"http://localhost:81/svn/StartKit/cloudConfig/config/application-test.properties","source":{"name":"default","test":"www"}},{"name":"http://localhost:81/svn/StartKit/cloudConfig/config/application.properties","source":{"name":"default","test":"test"}}]}
client端
- pom.xml 添加依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- bootstrap.properties
server.port=9999
spring.application.name=config-client
#配置规则为{spring.cloud.config.name}-{spring.cloud.config.profile}.properties
#指定配置文件前缀
spring.cloud.config.name=application
#指定配置文件后缀
spring.cloud.config.profile=test
#分支配置,默认master
#spring.cloud.config.label=master
#配置服务端的地址
spring.cloud.config.uri=http://localhost:8888/
management.security.enabled=false
- ClientApplication
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
- controller
新建个controller
@RestController
@RefreshScope//此注解,是在访问/refresh后服务端加载新配置,自动把新配置注入
public class ClientController {
//加载application-test.properties的name属性注入
@Value("${name}")
private String name;
@RequestMapping("/name")
public String name(){
return name;
}
//加载application-test.properties的test属性
//因为没有test属性,所以加载application.properties的test属性注入
@Value("${test}")
private String test;
@RequestMapping("/test")
public String test(){
return test;
}
}
- 启动项目,访问http://localhost:9999/name
default
www
更新配置文件
- 修改 application-test.properties内容
name=creditease
test=creditease
提交svn
-
configServer
不需要更改 -
configClient 添加刷新方法
@RequestMapping("/refresh1")
public String refresh1(){
HttpUtils.refresh();
return "success";
}
public class HttpUtils {
public static void refresh() {
HttpURLConnection connection =null;
try {
URL url = new URL("http://localhost:9999/refresh");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.connect();//链接
InputStream in=connection.getInputStream();//等待响应
in.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(connection!=null){
connection.disconnect();
}
}
}
总结
因为没有搞通自动配置,在配置hook文件时在命令行可以实现刷新,但是在hook文件没有触发,再研究。
网友评论