Spring Cloud Config Server
2017-07-05 22:04
447 查看
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
resources目录配置信息
启动ConfigServer
https://github.com/spring-cloud-samples/config-repo目录下有如下配置文件,
我们通过8888端口获取下logtest的配置:
http://localhost:8888/configserver/logtest
得到返回如下:
下面我们创建一个新的Config Client程序,看是否可以正常获取到配置信息:
resources目录配置信息,注意,Config Client 我们使用8889端口,uri是上面Config Server默认暴露的接口地址
启动Config Client
我们访问下8889端口
@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 @EnableConfigServer public 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 initialized 2017-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' channel 2017-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.errorLogger 2017-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 8888 2017-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 @RefreshScope public 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: 8889 spring: 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 0 2017-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端口
相关文章推荐
- spring cloud config server 配置中心
- spring-cloud中遇到的坑sidecar与config server client
- spring cloud:config-server中@RefreshScope的"陷阱"
- Spring Cloud实战(一)-Spring Cloud Config Server
- Spring Cloud之config server(配置中心)
- Spring Cloud Config Server的高可用
- spring-cloud-config-server——Environment Repository(File System Backend)
- Spring Cloud Config Server 配置说明
- 【SpringBoot】SpringCloud Config Server实践
- 4. 分布式配置中心 springcloud-config-server
- spring cloud config server使用svn来作为外部配置中心
- spring cloud config-server 高可用配置中心
- 基于mysql的spring cloud config server
- Spring Cloud Config (一. 启动Spring Cloud Config Server)
- Spring Cloud Config Server基于本地git使用SSH clone github repository
- Spring cloud config server 缓存问题
- spring-cloud-config-server
- spring-cloud-config-server——Environment Repository(Git Backend)
- spring cloud config server---No instances found of configserver
- Spring Cloud Config Server 实践