Spring cloud笔记

来源:互联网 发布:cf卡数据恢复软件 编辑:程序博客网 时间:2024/06/05 21:50

仅限于学习用途的转载,转载请注明出处http://eumji025.com

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:

$ cd spring-cloud-config-server$ ../mvnw spring-boot:run
The server is a Spring Boot application so you can run it from your IDE instead if you prefer (the main class is ConfigServerApplication). Then try out a client:
$ curl localhost:8888/foo/development{"name":"development","label":"master","propertySources":[  {"name":"https://github.com/scratches/config-repo/foo-development.properties","source":{"bar":"spam"}},  {"name":"https://github.com/scratches/config-repo/foo.properties","source":{"foo":"bar"}}]}
demo上来就直接就是运行步骤,很容易让人翻车,下面我就按照自己的流程简单介绍一下spring cloud config的第一个demo

config案例流程

从上面官网的介绍也可以看处,spring cloud config是为分布式系统提供外部配置,通过配置一个config server作为中间载体,去加载外部属性,这些外部属性可以放在git,svn等服务器上,spring cloud默认是使用git作为属性存储.

外部属性配置

首先将我们的外部属性配置到git服务器上,具体操作流程如下,相信使用过git的朋友都很熟悉.

mkdir config-repositorycd config-repositorygit initvim foo-development.propertiesgit add foo-development.propertiesgit commit -m "add foo-development.properties"git push origin master
注意:使用vim foo-development.properties命令后添加如下内容,或者也可以不用命令直接自己新建文件,然后在把下面的内容放在文件中.
bar: spamfoo: from foo development
外部配置已经创建完毕.如果你你只是自己测试一下,也可以不使用git的方式,设置这个属性使用 file:前缀,Config Server 是从本地配置库中取数据,这种方式可以不使用Git的情况下,快速和简单的运行起来

config-server配置

首先使用idea或者eclipse创建一个名为config-server的maven项目,使用idea的spring initializr可以配一步到位.配置时候勾选中cloud config 的config server即可.eclipse则可以使用spring STS插件.

pom.xml

在pom文件中追加如下配置.说明一下,本文中使用的都是最新版的spring boot和spring cloud

<parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.3.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-config-server</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>${spring-cloud.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>

SpringCloudConfigServerApplication.java

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

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

application.yml

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

#访问外部配置的git路径spring:  cloud:    config:      server:        git:          uri: https://github.com/spring-cloud-samples/config-repo#自定义端口server:  port: 8888

config-client配置

基本类似这里不做过多的描述,直接上代码.

pom文件

<parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.3.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-config</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <dependencyManagement>        <dependencies>            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>${spring-cloud.version}</version>                <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build>

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:88882017-04-30 10:53:42.637  INFO 12948 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=foo, profiles=[development], label=null, version=null, state=null2017-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

结语

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

与君共勉!!!!

参考资料

spring-cloud文档

源码地址

config-client
config-server

#BUG
0 0
原创粉丝点击