资讯专栏INFORMATION COLUMN

Spring Cloud实战(一)-Spring Cloud Config Server

Dionysus_go / 2160人阅读

摘要:概要什么是创建并运行一个建立一个创建并运行一个是什么什么是配置信息一个中不只是代码还需要连接资源和其它应用经常有很多需要外部设置的项去调整行为如切换不同的数据库国际化等应用中的会经常见到的等就是配置信息常见的实现信息配置的方法硬编码缺点需要

概要

</>复制代码

  1. 什么是Spring Cloud Config?

  2. 创建并运行一个Spring Cloud Config Server

  3. 建立一个Repository

  4. 创建并运行一个Spring Cloud Config Client

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信息具体如下

</>复制代码

  1. 4.0.0
  2. org.springframework.cloud
  3. spring-cloud-starter-parent
  4. Brixton.SR4
  5. org.mmb.cloud
  6. mmb-config-server
  7. 1.0-SNAPSHOT
  8. jar
  9. org.springframework.cloud
  10. spring-cloud-config-server

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

</>复制代码

  1. package config;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. /**
  6. * Created by mmb on 2016/7/30.
  7. */
  8. @EnableConfigServer
  9. @SpringBootApplication
  10. public class MMBConfigServerApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(MMBConfigServerApplication.class, args);
  13. }
  14. }

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

</>复制代码

  1. ---
  2. lucky-word: mmb

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

</>复制代码

  1. server:
  2. port: 8001
  3. spring:
  4. cloud:
  5. config:
  6. server:
  7. git:
  8. uri: https://github.com/mumubin/MMBConfigData
  9. searchPaths: data

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

</>复制代码

  1. {
  2. "name": "mmb-config-client",
  3. "profiles": [
  4. "default"
  5. ],
  6. "label": "master",
  7. "version": "4d9240f45fecd34136f81683d44c2e144792af86",
  8. "propertySources": [
  9. {
  10. "name": "https://github.com/mumubin/MMBConfigData/data/mmb-config-client.yml",
  11. "source": {
  12. "lucky-word": "mmb"
  13. }
  14. }
  15. ]
  16. }
创建并运行一个Spring Cloud Config Client

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

</>复制代码

  1. 4.0.0
  2. org.springframework.boot
  3. spring-boot-starter-parent
  4. 1.3.5.RELEASE
  5. org.springframework.cloud
  6. spring-cloud-starter-parent
  7. Brixton.SR4
  8. pom
  9. import
  10. org.springframework.cloud
  11. spring-cloud-starter-config
  12. org.springframework.boot
  13. spring-boot-starter-web
  14. org.springframework.boot
  15. spring-boot-devtools
  16. true
  17. org.mmb.cloud
  18. mmb-config-client
  19. 1.0-SNAPSHOT

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

</>复制代码

  1. spring:
  2. application:
  3. name: mmb-config-client
  4. cloud:
  5. config:
  6. uri: http://localhost:8001
  7. ---
  8. server:
  9. port: 8002

</>复制代码

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

3.创建一个Controller

</>复制代码

  1. @RestController
  2. public class LuckyWordController {
  3. @Value("${lucky-word}") String luckyWord;
  4. @RequestMapping("/lucky-word")
  5. public String showLuckyWord() {
  6. return "The lucky word is: " + luckyWord;
  7. }
  8. }

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

</>复制代码

  1. @SpringBootApplication
  2. public class Application {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Application.class,args);
  5. }
  6. }

</>复制代码

  1. The lucky word is: mmb

</>复制代码

  1. 特别感谢 kennyk65
    Spring Cloud 中文用户组 31777218
    Spring-Cloud-Config 官方文档-中文译本
    Spring Cloud Netflix 官网文档-中文译本
    本文实例github地址 Config Server Config Client

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64990.html

相关文章

  • Spring Cloud实战(二)-Spring Cloud Eureka

    摘要:概要什么是使用获取服务调用整合构建集群什么是模块提供的功能是被动式的服务发现什么是服务发现服务发现就像聊天室一个每个用户来的时候去服务器上注册这样他的好友们就能看到你你同时也将获取好友的上线列表在微服务中服务就相当于聊天室的用户而服务注册中 概要 什么是Spring Cloud Eureka? 使用Eureka获取服务调用 Eureka整合Spring Config Server 构...

    jaysun 评论0 收藏0
  • Spring Cloud 上手实战-架构解析及实作

    摘要:服务器将要监听的端口不要使用服务进行注册不要在本地缓存注册表信息使用一个新的注解,就可以让我们的服务成为一个服务服务发现客户端配置以为例需要做件事情成为服务发现的客户端配置对应来说我们只需要配置如下启动运行查看。 Spring简介 为什么要使用微服务 单体应用: 目前为止绝大部分的web应用软件采用单体应用,所有的应用的用户UI、业务逻辑、数据库访问都打包在一个应用程序上。 showI...

    Godtoy 评论0 收藏0
  • Spring Cloud Config 学习(二)

    摘要:是匹配规则,意思是配置以开头并且以开头的。健康监控集成了。可以通过配置去检查指定的配置文件访问结果如下可以通过设置来禁用健康检查。显示的是类似于用户自己定义的属性的那种黄色背景。意思就是这个不是系统的属性,但是确认是生效的。 《Spring Cloud与Docker 微服务架构实战》学习笔记 Config Client 在上篇文章中,我们已经编写好了 Config Server 那个客...

    Developer 评论0 收藏0

发表评论

0条评论

Dionysus_go

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<