您的位置:首页 > 编程语言 > Java开发

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

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端口

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: