spring cloud config 之svn仓库配置与动态刷新(服务端)

来源:互联网 发布:CPDA数据分析师证书 编辑:程序博客网 时间:2024/05/01 03:47
最近由于公司新项目要用到spring cloud所以利用空闲时间学习了一下,记录下自己的学习经历与收获,此类文章可能有很多也可能就这一篇。好了接下来是正文。
spring cloud 默认使用git作为配置中心仓库,由于公司用的是svn所以这里特意留意了下,引入svn支持与配置比较简单,动态刷新比较蛋疼,不过最后还是解决了,可能方法不是很好但是也是实现了。
由于本人是java开发,设计其他语言与脚本不涉及深入。
前提:svn仓库,在仓库目录下新建config作为配置仓库,新建三个properties文件
application.properties 写上 name=default和test=test
application-dev.properties 写上 name=dev
application-test.properties 写上 name=test
新建springboot项目config-svn-server

pom.xml配置
<dependencies>
  <!-- 服务端包 -->
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
     </dependency>
  <!-- svn依赖包 -->
     <dependency>
         <groupId>org.tmatesoft.svnkit</groupId>
         <artifactId>svnkit</artifactId>
     </dependency>
</dependencies>

application.properties配置
server.port=8888
spring.application.name=config-server
#
#这项必须要写表示使用svn
spring.profiles.active=subversion
#开启配置中心支持
spring.cloud.config.enabled=true
#仓库服务器地址
spring.cloud.config.server.svn.uri=https://DESKTOP-P4TIRFS/svn/cgj/
spring.cloud.config.server.svn.username=cgj
spring.cloud.config.server.svn.password=cgj
#仓库中配置文件目录
spring.cloud.config.server.svn.default-label=config

springboot启动类
@SpringBootApplication
//开启spring cloud config 服务端功能
@EnableConfigServer
public class ConfigServerApplication {
 
 public static void main(String[] args) {
  SpringApplication.run(ConfigServerApplication.class, args);
 }
}

以上为config-svn-server的全部,spring cloud config 的服务端完成
运行启动类,访问
http://localhost:8888/application/test
注:这个不知道是不是坑,我也没看官方文档,访问地址形式为http://localhost:8888/{application}/{profile},对应配置文件application-profile.properties,但是其实访问地址中的{application}随便是什么,只要后缀{profile}对应就能加载到,如果无法匹配默认访问application.properties
eg:
http://localhost:8888/1/test和上面结果一样;
http://localhost:8888/1/test1访问的是application.properties

返回信息
{"name":"application","profiles":["test"],"label":null,"version":"83","state":null,
"propertySources":[{"name":"https://DESKTOP-P4TIRFS/svn/cgj/config/application-test.properties","source":{"name":"test"}},
      {"name":"https://DESKTOP-P4TIRFS/svn/cgj/config/application.properties","source":{"name":"default","test":"test"}}]}

控制台打印
2017-12-19 11:46:38.327  INFO 7772 --- [nio-8888-exec-6] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/pactera/AppData/Local/Temp/config-repo-985817115184069681/config/application-test.properties
2017-12-19 11:46:38.329  INFO 7772 --- [nio-8888-exec-6] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/pactera/AppData/Local/Temp/config-repo-985817115184069681/config/application.properties
启动成功,发现实际上加载了两个properties一个是我们指定的test另一个是默认的application.properties,所以在配置的时候application.properties放公共配置,application-{profile}.properties放独有配置。
结束了,完了,拜拜


阅读全文
0 0
原创粉丝点击