开始Spring Cloud Config

来源:互联网 发布:淘宝网严重违规节点 编辑:程序博客网 时间:2024/04/30 22:12

什么是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 BootEnvironmentPropertySource 、Profile等一些技术 

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

@EnableConfigServer

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

@Configuration@EnableAutoConfiguration@EnableConfigServerpublic class App {    public static void main(String... args) {        SpringApplication.run(App.class, args).getEnvironment();    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

server:  port: 8888spring:  profiles:    active: subversion  cloud:    config:      server:        svn:          uri: https://www.xxx.com/svn/demo/demo-config-repo
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

添加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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

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"            }        }    ]}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • {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>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

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

spring:  cloud:    config:      name: loupan      profile: development      label: trunk      uri: http://localhost:8888
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这里的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']]]
  • 1
  • 2
  • 1
  • 2

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

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

或者

@Configurationpublic class DemoConfig {    @Value("${demo.env}")    public String env;   ...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

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

来刷新配置。

$ curl -X POST http://localhost:8080/restart
0 0
原创粉丝点击