Spring Cloud Config Server

来源:互联网 发布:单片机程序实验流程图 编辑:程序博客网 时间:2024/05/06 12:15

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.

@EnableConfigServer

package demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.config.server.EnableConfigServer;import org.springframework.context.annotation.Configuration;@Configuration@EnableAutoConfiguration@EnableDiscoveryClient@EnableConfigServerpublic class ConfigServerApplication {    public static void main(String[] args) {        SpringApplication.run(ConfigServerApplication.class, args);    }}

resources目录配置信息

这里写图片描述

启动ConfigServer

2017-07-05 21:48:25.212 DEBUG 1680 --- [           main] s.c.c.d.h.DiscoveryClientHealthIndicator : Discovery Client has been initialized2017-07-05 21:48:25.286  INFO 1680 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel2017-07-05 21:48:25.286  INFO 1680 --- [           main] o.s.i.channel.PublishSubscribeChannel    : Channel 'configserver:8888.errorChannel' has 1 subscriber(s).2017-07-05 21:48:25.287  INFO 1680 --- [           main] o.s.i.endpoint.EventDrivenConsumer       : started _org.springframework.integration.errorLogger2017-07-05 21:48:25.458  INFO 1680 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8888 (http)2017-07-05 21:48:25.460  INFO 1680 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 88882017-07-05 21:48:25.467  INFO 1680 --- [           main] demo.ConfigServerApplication             : Started ConfigServerApplication in 12.164 seconds (JVM running for 12.807)

https://github.com/spring-cloud-samples/config-repo目录下有如下配置文件,
这里写图片描述

我们通过8888端口获取下logtest的配置:
http://localhost:8888/configserver/logtest
得到返回如下:

{    "name":"configserver",    "profiles":[        "logtest"    ],    "label":"master",    "version":"a611374438e75aa1b9808908c57833480944e1a8",    "state":null,    "propertySources":[        {            "name":"https://github.com/spring-cloud-samples/config-repo/configserver.yml",            "source":{                "info.description":"configserver"            }        },        {            "name":"https://github.com/spring-cloud-samples/config-repo/application.yml",            "source":{                "info.description":"Spring Cloud Samples",                "info.url":"https://github.com/spring-cloud-samples",                "eureka.client.serviceUrl.defaultZone":"http://localhost:8761/eureka/",                "foo":"baz"            }        }    ]}

下面我们创建一个新的Config Client程序,看是否可以正常获取到配置信息:

package hello;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestController@RefreshScopepublic class Application {    @Value("${logging.level.com.example.foo}")    String bar;    @RequestMapping("/")    String hello() {        return "Hello " + bar + "!";    }    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

resources目录配置信息,注意,Config Client 我们使用8889端口,uri是上面Config Server默认暴露的接口地址

server:  port: 8889spring:  application:    name: logtest  cloud:    config:      label:master      uri:http//localhost:8888

启动Config Client

2017-07-05 22:00:48.633  INFO 10828 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'refreshScope': registering with JMX server as MBean [org.springframework.cloud.context.scope.refresh:name=refreshScope,type=RefreshScope]2017-07-05 22:00:48.658  INFO 10828 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'configurationPropertiesRebinder': registering with JMX server as MBean [org.springframework.cloud.context.properties:name=configurationPropertiesRebinder,context=bf1ec20,type=ConfigurationPropertiesRebinder]2017-07-05 22:00:48.668  INFO 10828 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Located managed bean 'refreshEndpoint': registering with JMX server as MBean [org.springframework.cloud.endpoint:name=refreshEndpoint,type=RefreshEndpoint]2017-07-05 22:00:48.845  INFO 10828 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 02017-07-05 22:00:49.166  INFO 10828 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8889 (http)2017-07-05 22:00:49.170  INFO 10828 --- [           main] hello.Application                        : Started Application in 9.573 seconds (JVM running for 10.034)

我们访问下8889端口
这里写图片描述

原创粉丝点击