码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Cloud Config 入门

时间:2017-06-17 20:40:50      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:spring cloud config

1.    简介

Spring Cloud Config 是用来为分布式系统中为微服务应用提供集中化的外部配置支持,主要分为Spring Cloud Config Server(服务器端)和Spring Cloud Config Client(客户端)。

2.    Spring Cloud Config Server

Spring Cloud Config Server为服务器端,它是一个单独的微服务应用,用来连接配置仓库(本文使用的是git仓库)并为客户端获取配置信息。

1.     首先,创建config server工程

打开http://start.spring.io/

技术分享

填写好GroupArtifact。选择依赖的包有Config Server

对应的pom.xml

<dependencies>

      <dependency>

          <groupId>org.springframework.cloud</groupId>

          <artifactId>spring-cloud-config-server</artifactId>

      </dependency>

      <dependency>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-web</artifactId>

      </dependency>

 

      <dependency>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-test</artifactId>

          <scope>test</scope>

      </dependency>

</dependencies>

2.     将下载下来的项目导入Eclipse

目录结构如下,我这里面新增了bootstrap.yml

技术分享

3.     修改配置

application.yml中添加

       server:

      port: 8080

bootstrap.yml中添加

spring:

  cloud:

    config:

      server:

        git:

         uri: https://github.com/DevinXin/config-repo

 

注意:ConfigServerApplicationSpring Boot 启动类上需要添加@EnableConfigServer注解

技术分享

4.     启动configServer

通过访问http://localhost:8080/master/foobar-dev.properties可以读到git上的配置文件。

技术分享

3.    Spring Cloud Config Client

Spring Cloud Config Client为客户端,客户端通过配置连接服务器端,从服务器端加载配置信息。

1.     创建config client工程

config server工程创建一样,依赖需要webConfig Client

对应的pom.xml为:

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

      <dependency>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-test</artifactId>

          <scope>test</scope>

      </dependency>

   </dependencies>

2.     修改配置

application.yml配置为

     server:

    port: 8081

bootstrap.yml配置为

    spring:

      cloud:

        config:

          uri: http://localhost:8080/

          profile: dev

          label: master

      application:

       name: foobar

3.     写一个Controller

技术分享

4.     启动config Client

访问http://localhost:8081/configServer

可以从config Server中获取到配置文件中的值。

技术分享


本文出自 “辛立光博客” 博客,请务必保留此出处http://devinxin.blog.51cto.com/2325562/1939372

Spring Cloud Config 入门

标签:spring cloud config

原文地址:http://devinxin.blog.51cto.com/2325562/1939372

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!