摘要:概要什么是创建并运行一个建立一个创建并运行一个是什么什么是配置信息一个中不只是代码还需要连接资源和其它应用经常有很多需要外部设置的项去调整行为如切换不同的数据库国际化等应用中的会经常见到的等就是配置信息常见的实现信息配置的方法硬编码缺点需要
概要
Spring Cloud Config是什么?</>复制代码
什么是Spring Cloud Config?
创建并运行一个Spring Cloud Config Server
建立一个Repository
创建并运行一个Spring Cloud Config Client
什么是配置信息?
一个Application中不只是代码,还需要连接资源和其它应用,经常有很多需要外部设置的项去调整Application行为,如切换不同的数据库,i18n国际化 等.应用中的会经常见到的xml,properties,yaml等就是配置信息.
常见的实现信息配置的方法:
硬编码(缺点:需要修改代码,风险大)
放在xml等配置文件中,和应用一起打包(缺点:需要重新打包和重启)
文件系统中(缺点:依赖操作系统等)
环境变量(缺点:有大量的配置需要人工设置到环境变量中,不便于管理,且依赖平台)
云端存储(缺点:与其他应用耦合)
Spring Cloud Config 就是云端存储配置信息的,它具有中心化,版本控制,支持动态更新,平台独立,语言独立等特性.
Spring Cloud Config的原理如图所示,真正的数据存在Git等repository中,Config Server去获取相应的信息,然后开发给Client Application,相互间的通信基于HTTP,TCP,UDP等协议.
1.创建一个名为my-config-server的应用,并添加spring-cloud-starter-parent,spring-cloud-config-server依赖,pom信息具体如下
</>复制代码
4.0.0
org.springframework.cloud
spring-cloud-starter-parent
Brixton.SR4
org.mmb.cloud
mmb-config-server
1.0-SNAPSHOT
jar
org.springframework.cloud
spring-cloud-config-server
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
@SpringBootApplication
public 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:
port: 8001
spring:
cloud:
config:
server:
git:
uri: https://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信息具体如下
</>复制代码
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.3.5.RELEASE
org.springframework.cloud
spring-cloud-starter-parent
Brixton.SR4
pom
import
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
true
org.mmb.cloud
mmb-config-client
1.0-SNAPSHOT
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
</>复制代码
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
</>复制代码
The lucky word is: mmb
</>复制代码
特别感谢 kennyk65
Spring Cloud 中文用户组 31777218
Spring-Cloud-Config 官方文档-中文译本
Spring Cloud Netflix 官网文档-中文译本
本文实例github地址 Config Server Config Client
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/64990.html
摘要:概要什么是使用获取服务调用整合构建集群什么是模块提供的功能是被动式的服务发现什么是服务发现服务发现就像聊天室一个每个用户来的时候去服务器上注册这样他的好友们就能看到你你同时也将获取好友的上线列表在微服务中服务就相当于聊天室的用户而服务注册中 概要 什么是Spring Cloud Eureka? 使用Eureka获取服务调用 Eureka整合Spring Config Server 构...
摘要:服务器将要监听的端口不要使用服务进行注册不要在本地缓存注册表信息使用一个新的注解,就可以让我们的服务成为一个服务服务发现客户端配置以为例需要做件事情成为服务发现的客户端配置对应来说我们只需要配置如下启动运行查看。 Spring简介 为什么要使用微服务 单体应用: 目前为止绝大部分的web应用软件采用单体应用,所有的应用的用户UI、业务逻辑、数据库访问都打包在一个应用程序上。 showI...
摘要:是匹配规则,意思是配置以开头并且以开头的。健康监控集成了。可以通过配置去检查指定的配置文件访问结果如下可以通过设置来禁用健康检查。显示的是类似于用户自己定义的属性的那种黄色背景。意思就是这个不是系统的属性,但是确认是生效的。 《Spring Cloud与Docker 微服务架构实战》学习笔记 Config Client 在上篇文章中,我们已经编写好了 Config Server 那个客...
阅读 3676·2021-11-24 11:17
阅读 2469·2021-11-15 11:38
阅读 3541·2021-10-14 09:42
阅读 3079·2019-08-30 15:54
阅读 2157·2019-08-28 18:09
阅读 681·2019-08-26 11:48
阅读 1762·2019-08-26 10:48
阅读 2298·2019-08-26 10:45