开始Spring Cloud Config

4 篇文章 0 订阅
1 篇文章 0 订阅

什么是Spring Cloud Config

Spring Cloud Config项目提供了一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分。

Spring Cloud Config Sever的管理git或svn的外部配置,集中配置到所有客户端。

Spring Cloud Config Client根据Spring框架的EnvironmentPropertySource从Spring Cloud Config Sever获取配置。

所有要开始Spring Cloud Config,一定要先了解Spring BootEnvironmentPropertySourceProfile等一些技术

Spring Cloud官网上提供了默认的基于git的配置,下面例子基于svn, svn地址用www.xxx.com代替了,另行修改下。

@EnableConfigServer

构建Spring Cloud Config Server,只需要一个@EnableConfigServer

@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class App {

    public static void main(String... args) {
        SpringApplication.run(App.class, args).getEnvironment();
    }

}

在到resource下面,添加application.yml,加上配置

server:
  port: 8888

spring:
  profiles:
    active: subversion
  cloud:
    config:
      server:
        svn:
          uri: https://www.xxx.com/svn/demo/demo-config-repo

添加pom.xml配置, 需要带入spring boot、spring cloud 和svn的jar

<!--spring cloud parent version-->
    <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>Angel.SR3</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>com.xxx.App</start-class>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--spring cloud config server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--config server need read svn-->
        <dependency>
            <groupId>org.tmatesoft.svnkit</groupId>
            <artifactId>svnkit</artifactId>
            <version>1.8.10</version>
        </dependency>

        <!--spring boot test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <!--mvn spring-boot:run 命令-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

https://www.xxx.com/svn/demo/demo-config-repo下面提交一个文件,比如demo-development.properties

运行App.class, 访问 http://localhost:8888/{application}/{profile}/{label},比如:http://localhost:8888/dmeo/development/trunk
成功了。

{
    name: "demo",
    profiles: [
        "developement"
    ],
    label: "trunk",
    propertySources: [
        {
            name: "https://www.xxx.com/svn/demo/demo-config-repo/trunk/demo.properties",
            source: {
                demo.env: "default"
            }
        }
    ]
}
  • {application} 匹配客户端的 “spring.application.name”

  • {profile} 匹配客户端的”spring.active.profiles”

  • {label} 如果是svn匹配trunk/branchs等.

尝试下提供svn的属性,在访问,发现配置信息以及发生变化了。

Spring Cloud Config Client

客户端,仍然新建立一Spring boot的项目。加入spring-cloud-config-client

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>

添加bootstrap.yml到resources下面。加入配置

spring:
  cloud:
    config:
      name: loupan
      profile: development
      label: trunk
      uri: http://localhost:8888

这里的http://localhost:8888是刚刚启动的Spring Cloud Config Server的应用。

2015-08-27 18:01:03.751  INFO 11520 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='https://www.xxx.com/svn/demo/demo-config-repo/trunk/demo-developement.properties'], MapPropertySource [name='https://www.xxx.com/svn/demo/demo-config-repo/trunk/demo.properties']]]

启动信息里面找到这样的日志,就成功了。它会自动加载的项目里面,你可以使用Spring的自动配置方便的使用外部配置。
例如直接在application.properties里面使用

spring.profiles.active = ${demo.env}

或者

@Configuration
public class DemoConfig {

    @Value("${demo.env}")
    public String env;

   ...

另外,他还提供了很多方式来满足需求。比如,修改了配置后,可以

$ curl -X POST http://localhost:8080/refresh

来刷新配置。

$ curl -X POST http://localhost:8080/restart

。。。

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值