频道栏目
首页 > 资讯 > 其他 > 正文

Spring cloud记录 - config 外部配置

17-05-12        来源:[db:作者]  
收藏   我要投稿
Spring cloud记录 - config 外部配置。

Spring cloud config介绍

原文:Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production you can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate. The default implementation of the server storage backend uses git so it easily supports labelled versions of configuration environments, as well as being accessible to a wide range of tooling for managing the content. It is easy to add alternative implementations and plug them in with Spring configuration.

译文:Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器映射的概念与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可用于管理内容的各种工具。可以轻松添加替代实现,并使用Spring配置将其插入。

简单说两句

对于官网给的第一个Demo也是煞费苦心,刚看到第一句话的时候我是崩溃的,具体如下:

Start the server:


        org.springframework.boot
        spring-boot-starter-parent
        1.5.3.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Dalston.RELEASE
    

    
        
            org.springframework.cloud
            spring-cloud-config-server
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

SpringCloudConfigServerApplication.java

server端可以使用@EnableConfigServer注释轻松嵌入到Spring boot应用程序中。

@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigServerApplication {

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

application.yml

跟所有Spring Boot应用程序一样,它默认在端口8080上运行,而默认客户端请求的是8888端口,我们可以通过各种方式将其切换到常规端口8888。关于端口的几种方式可参加我前面写的文章spring boot自定义端口
然后我们需要在server端配置加载外部配置的git路径.具体如下所示.


        org.springframework.boot
        spring-boot-starter-parent
        1.5.3.RELEASE
         
    


    
        UTF-8
        UTF-8
        1.8
        Dalston.RELEASE
    

    
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

SpringCloudConfigClientApplication.java

public class SpringCloudConfigClientApplication {

    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

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

application.yml

这里主要是指明要加载的外部文件和环境.

spring:
  application:
    name: foo
  profiles:
    active: development

运行服务

1.启动server服务

2.启动client服务

结果

在开启client之后,可以在log中国看到如下类似的信息

Fetching config from server at: http://localhost:8888
2017-04-30 10:53:42.637  INFO 12948 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=foo, profiles=[development], label=null, version=null, state=null
2017-04-30 10:53:42.638  INFO 12948 --- [  restartedMain] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='https://github.com/spring-cloud-samples/config-repo/foo-development.properties']]

或者直接在浏览器输入 localhost:8888/foo/development

此处的地址和我们client的配置是有关联的,他决定了server去加载什么外部配置.具体介绍如下

#请求路径 label是可选的
/ {application} / {profile} [/ {label}]
#资源路径
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

结语

我相信经过上面的例子,再去看官网的文档,思路应该清晰很多了.

相关TAG标签
上一篇:Spring使用策略模式对访问资源的封装
下一篇:搭建keepalived+lvs 模式,简单笔记
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站