发表于: 2017-06-08 22:45:59

2 1083


一、今天完成的事情:

     继续撸了一波spring-cloud

Spring Cloud Config是什么?

什么是配置信息?
一个Application中不只是代码,还需要连接资源和其它应用,经常有很多需要外部设置的项去调整Application行为,如切换不同的数据库,i18n国际化 等.应用中的会经常见到的xml,properties,yaml等就是配置信息.

常见的实现信息配置的方法:

  • 硬编码(缺点:需要修改代码,风险大)

  • 放在xml等配置文件中,和应用一起打包(缺点:需要重新打包和重启)

  • 文件系统中(缺点:依赖操作系统等)

  • 环境变量(缺点:有大量的配置需要人工设置到环境变量中,不便于管理,且依赖平台)

  • 云端存储(缺点:与其他应用耦合)Spring Cloud Config 就是云端存储配置信息的,它具有中心化,版本控制,支持动态更新,平台独立,语言独立等特性.


Spring Cloud Config的原理如图所示,真正的数据存在Git等repository中,Config Server去获取相应的信息,然后开发给Client Application,相互间的通信基于HTTP,TCP,UDP等协议.

创建并运行一个Spring Cloud Config Server

1.创建一个名为my-config-server的应用,并添加spring-cloud-starter-parent,spring-cloud-config-server依赖,pom信息具体如下

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>  

  <parent>       

 <groupId>org.springframework.cloud</groupId>         <artifactId>spring-cloud-starter-parent</artifactId>        <version>Brixton.SR4</version>        <relativePath/>    </parent>    <groupId>org.mmb.cloud</groupId>    <artifactId>mmb-config-server</artifactId>    <version>1.0-SNAPSHOT</version>    <packaging>jar</packaging>    <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-config-server</artifactId>        </dependency></dependencies></project>

2.在Application主类上添加@EnableConfigServer注解,具体如下

package config;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;/** * Created by mmb on 2016/7/30. */@EnableConfigServer @SpringBootApplicationpublic class MMBConfigServerApplication {    public static void main(String[] args) {        SpringApplication.run(MMBConfigServerApplication.class, args);    } }

3.去自己的GitHub上创建一个repository命名=MMBConfigData,并创建一个mmb-config-client.yml的配置文件,并添加一个key为luck-word,value mmb,或者其它任何值.具体如下

---lucky-word: mmb

4.回自己的工程,设置应用application.yml,配置spring.cloud.config.server.git.uri为"https://github.com/"YOUR-GITHUB-ID"/"YOUR-REPOSITORY-NAME"",并设置端口server.port为8001

server:  port8001spring:  cloud:    config:      server:        git:          urihttps://github.com/mumubin/MMBConfigData     searchPaths: data

5.启动应用并打开地址http://localhost:8001/mmb-con...将会看到配置好的信息,我的如下

{    "name""mmb-config-client",    "profiles": [        "default"    ],    "label""master",    "version""4d9240f45fecd34136f81683d44c2e144792af86",    "propertySources": [        {            "name""https://github.com/mumubin/MMBConfigData/data/mmb-config-client.yml",            "source": {   "lucky-word""mmb"            }        }   ] }

创建并运行一个Spring Cloud Config Client

1.创建一个名为my-config-client的应用,并添加spring-cloud-starter-parent,spring-cloud-starter-config,spring-boot-starter-web依赖,pom信息具体如下

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.3.5.RELEASE</version>        <relativePath/> <!-- lookup parent from repository --></parent>    <dependencyManagement>        <dependencies>            <dependency>      <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-starter-parent</artifactId>                <version>Brixton.SR4</version>               <type>pom</type>                <scope>import</scope>            </dependency>        </dependencies>    </dependencyManagement><dependencies>        <dependency>          <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-config</artifactId>        </dependency><dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>   </dependency>        <!-- Allow for automatic restarts when classpath contents change. -->        <dependency>            <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-devtools</artifactId>            <optional>true</optional>     </dependency>    </dependencies>    <groupId>org.mmb.cloud</groupId>    <artifactId>mmb-config-client</artifactId>    <version>1.0-SNAPSHOT</version></project>

2.创建bootstrap.yml在resource下,并设置spring.application.name,spring.cloud.config.uri,server.port信息,具体如下

spring:  application:    name: mmb-config-client  cloud:    config:      uri:  http://localhost:8001---server:  port: 8002

注意这里是bootstrap.yml而不是appliction.yml,因为bootstrap.yml会在应用启动之前读取,而spring.cloud.config.uri会影响应用启动

3.创建一个Controller

@RestController    public class LuckyWordController {      @Value("${lucky-word}") String luckyWord;        @RequestMapping("/lucky-word")      public String showLuckyWord() {        return "The lucky word is: " + luckyWord;      }    }

4.启动Application,打开http://localhost:8002/lucky-word

@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class,args);    } }
The lucky word is: mmb

二、遇到的问题:无

三、明天计划的事情

四、收获:武汉太热了………妈个鸡,(・Д・)ノ这要一个胖子怎么活


返回列表 返回列表
评论

    分享到