美文网首页
spring cloud config 整合svn

spring cloud config 整合svn

作者: 米_8d62 | 来源:发表于2018-05-03 18:34 被阅读0次

配置文件

新建一个文件夹,创建application-test.properties文件,内容是:

name=default
test=www

提交到svn库

server端

  1. 新建spring boot项目, 命名为configServer
  2. 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>
  1. 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
  1. ServerApplication
@SpringBootApplication
@EnableConfigServer
public class ServerApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(ServerApplication.class).web(true).run(args);
    }
}
  1. 启动项目,访问 http://localhost:8888/application/test, 注意命名,application是application-test.properties的前缀,test是后缀

  2. 请求结果

{"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端

  1. pom.xml 添加依赖包
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  1. 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
  1. ClientApplication
@SpringBootApplication
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

}
  1. 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;
    }

}
  1. 启动项目,访问http://localhost:9999/name
default

访问http://localhost:9999/test

www

更新配置文件

  1. 修改 application-test.properties内容
name=creditease
test=creditease

提交svn

  1. configServer
    不需要更改

  2. 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();
            }
        }
    }
  1. 重启后调用http://localhost:9999/refresh1
  2. 重新调用http://localhost:9999/namehttp://localhost:9999/test

总结

因为没有搞通自动配置,在配置hook文件时在命令行可以实现刷新,但是在hook文件没有触发,再研究。

相关文章

网友评论

      本文标题:spring cloud config 整合svn

      本文链接:https://www.haomeiwen.com/subject/amelrftx.html