一、介绍

1、场景:

微服务系统中,系统多、实例多,如果每个系统都有自己一套配置加载、维护的机制,会导致在生产过程中因为配置问题引发的不必要的沟通成本、故障风险。需要采用分布式配置中心统一管理、统一实现方式。

2、Spring cloud config特性

服务端:存储方式(git、svn、本地文件)、配置读取方式(环境化、多项目仓库)、安全性(访问密码、加密存储)、自动刷新。
客户端:加载远程配置、刷新配置、@refreshscope作用域刷新、集成消息总线。

二、docker中运行gitlab、rabbitmq的方式

1、下载安装: docker toolbox
windows下载:https://www.docker.com/products/docker-toolbox
下载后控制台执行命令:docker-machine create –engine-registry-mirror=”https://s0iielsh.mirror.aliyuncs.com” –engine-insecure-registry=”0.0.0.0/0” -d virtualbox default
这时可能提示没有boot2docker镜像,这样默认就会通过git下载,但是速度实在受不了,根本跑不动,但是可以直接通过安装的docker文件夹中找到

直接复制到C:\Users\Administrator.docker\machine\cache

2、 查看虚拟机IP等信息:docker-machine env default
注:如果想直接在windows命令窗口内使用docker命令,将上一条命令输出的内容,复制粘贴到控制台执行一次即可
3、 创建docker中的网络
docker network create dongnao_net

4、 运行gitlab
docker run -d –net=dongnao_net –publish 1443:443 –publish 18080:80 –name gitlab –restart always gitlab/gitlab-ce:latest
端口18080,通过你的虚拟机IP取访问就可以看到页面了

5、 运行rabbitmq
docker run -d –net=dongnao_net –name rabbitmq –publish 5671:5671 –publish 5672:5672 –publish 4369:4369 –publish 25672:25672 –publish 15671:15671 –publish 15672:15672 rabbitmq:management
连接的端口是 5672
web控制台是 15672
6、说明
Oracle VM VirtualBox是一个管理虚拟机的工具
Docker Quickstart Terminal是用于连接操作虚拟机的

由于虚拟机经常启动失败,我遇到失败的原因经常是vboxdrv服务没有安装或没有成功启动;
解决办法:开始后第一件事就尝试打开虚拟机,如果不成功就找到安装目录下的vboxdrv文件夹,
如C:\Program Files\Oracle\VirtualBox\drivers\vboxdrv,右击VBoxDrv.inf,选安装,然后重启电脑。
再在virturalBox里启动虚拟机,然后打开docker,运行命令 docker start rabbitmq来启动rabbitmq,服务器启动就正常了,这时也可以在浏览器上访问rabbitmq和gitlab了。

三、注册中心eureka

说明:以下代码注释说明得非常清楚,就不多做解释,有疑问的欢迎留言提问!
1、pom文件

<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><!-- spring boot 封装spring  starter封装、自动配置autoconfiguration--><parent><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-parent</artifactId><version>Dalston.SR1</version><relativePath /></parent><groupId>com.dongnaoedu.springcloud</groupId><artifactId>lession-2-eureka</artifactId><version>0.0.1-SNAPSHOT</version><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!-- spring-boot-starter-web web项目,集成容器tomcat --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- spring-boot-starter-actuator 管理工具/web 查看堆栈,动态刷新配置 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- cloud eureka组件 注册中心 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

2、application.yml:

# 上下文初始化加载
info:name: Eureka servercontact: 动脑科技VIPspring:profiles:active: dev
---
spring:profiles: dev
server:port: 8761
eureka:client:# 是否注册到eurekaserverregisterWithEureka: true# 是否拉取信息fetchRegistry: false# eureka server地址serviceUrl:defaultZone: http://127.0.0.1:8761/eureka/server:waitTimeInMsWhenSyncEmpty: 0# false 关闭自我保护,不管如何都要剔除心跳检测异常的服务enableSelfPreservation: trueinstance:hostname: eureka1

3、bootstrap.yml

# 比application context先加载
# 应用名称
spring:application:name: eureka-server

4、启动类

package com.dongnaoedu.springcloud;import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer
public class EurekaApp {public static void main(String[] args) {new SpringApplicationBuilder(EurekaApp.class).web(true).run(args);}
}

四、server

1、application.yml:

info:name: 配置中心contact: 动脑VIPserver:port: 8888
# 使得/refresh不需要验权
management:security:enabled: false
# 访问时需要提供用户和密码
security:# 这个接口不做用户名密码校验, /monitor接收git刷新通知ignored: /monitoruser:name: userpassword: 12345678
encrypt:# 加解密用的秘钥key: 12345678# rabbitmq的地址 用户名 密码
spring:rabbitmq:host: 192.168.99.100username: guestpassword: guestprofiles:active: dev# 不同环境的配置
---
spring:profiles: dev
eureka:client:registerWithEureka: truefetchRegistry: trueserviceUrl:defaultZone: http://127.0.0.1:8761/eureka/instance:preferIpAddress: true
---
spring:profiles: prod
eureka:client:registerWithEureka: truefetchRegistry: trueserviceUrl:defaultZone: http://eureka1:8761/eureka/,http://eureka2:8762/eureka/,http://eureka3:8763/eureka/instance:preferIpAddress: true

2、bootstrap.yml:

spring:application:# 配置文件就是用这个来做文件名的,要对应的哦。name: lession-2-config-serverprofiles:active: dev,gitcloud:config:server:# 本地文件native:# 用本地文件夹存储配置,仅作配置示例,没起作用。要想起作用,将上面的 active中git 改为 nativesearchLocations: file:D:\\cloud\\dongnao\\configrepo# git仓库 gitlab地址git:# 记得在先gitlab上创建一个对应的projecturi: http://192.168.99.100:18080/root/project1.gitsearch-paths: /username: rootpassword: 12345678repos: # 不同环境不同的库,这里的话,只有当应用中的spring.profiles.active=staging的时候才生效lession-2-sms-sys-staging:pattern: '*/staging'# 记得在先gitlab上创建一个对应的projecturi: http://192.168.99.100:18080/root/lession-2-config-repo-staging.git# 不同项目不同库lession-2-sms-webmvc: pattern: # 这里是根据服务名称匹配的spring.application.name- lession-2-sms-webmvc/**- lession-2-sms-webmvc*# 这里面的是本地git仓库的,不知道配置本地git仓库的也可以像上面一样配置成远程git地址uri: file:D:\cloud\dongnao\config-repo# 加解密encrypt:enabled: true
# svn环境
# spring.profiles.active=subversion
# spring.cloud.config.server.svn.uri=http:#127.0.0.1:1234/sms-sys/development/trunk
# spring.cloud.config.server.svn.username=xxx
# spring.cloud.config.server.svn.password=xxx

3、启动类

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {public static void main(String[] args) {new SpringApplicationBuilder(ConfigServerApplication.class).web(true).run(args);}
}

4、pom文件

<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><!-- spring boot 封装spring  starter封装、自动配置autoconfiguration--><parent><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-parent</artifactId><version>Dalston.SR1</version> <relativePath /> </parent><groupId>com.dongnaoedu.springcloud</groupId><artifactId>lession-2-config-server</artifactId><version>0.0.1-SNAPSHOT</version><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!-- spring-boot-starter-web web项目,集成容器tomcat --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- spring-boot-starter-actuator 管理工具/web 查看堆栈,动态刷新配置 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><!-- spring-boot-starter-security --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- cloud config组件 配置中心 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-monitor</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

五、客户端

1、pom文件

<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><!-- spring boot 封装spring  starter封装、自动配置autoconfiguration--><parent><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-parent</artifactId><version>Dalston.SR1</version><relativePath /></parent><groupId>com.tony</groupId><artifactId>lession-2-sms-sys</artifactId><version>0.0.1-SNAPSHOT</version><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!-- spring-boot-starter-web web项目,集成容器tomcat --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- spring-boot-starter-actuator 管理工具/web 查看堆栈,动态刷新配置 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><!-- spring bus 事件通知实现自动刷新 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

2、application.yml

# eureka是必须的spring:profiles:# 这里的意思是启用dev环境active: dev---
spring:profiles: dev
eureka:client:registerWithEureka: truefetchRegistry: trueserviceUrl:defaultZone: http://127.0.0.1:8761/eurekainstance:preferIpAddress: true---
spring:profiles: prod
eureka:client:registerWithEureka: truefetchRegistry: trueserviceUrl:# 这里写成eureka1,是因为我修改系统的hosts, 127.0.0.1 eureka1defaultZone: http://eureka1:8761/eurekainstance:preferIpAddress: true

3、bootst.yml

spring:application:name: lession-2-sms-syscloud:config:discovery:# 使用eureka发现配置中心服务enabled: true# 配置中心服务名称/IDserviceId: lession-2-config-server# 登录用户名和密码username: userpassword: 12345678# 覆盖本地配置overrideNone: falsefailFast: truename: ${spring.application.name}profile: ${spring.profiles.active}# git仓库中,可以使用label来做不同版本的配置管理,默认是master,可以用来做版本管理。比如“2.0”label: 'master'

4、启动类

package com.dongnaoedu.springcloud.service;import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication
@ComponentScan("com.dongnaoedu")
@EnableEurekaClient
public class SmsServiceApplication {public static void main(String[] args) {new SpringApplicationBuilder(SmsServiceApplication.class).web(true).run(args);}
}

5、controller

package com.dongnaoedu.springcloud.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RefreshScope // 此处很重要 +++
public class SmsController {@Value("${tony.configString}")private String configString;// 测试注入tony.configString配置@RequestMapping("/test")public String sendSms() {return "configString的值:" + configString;}@AutowiredEnvironment env;@RequestMapping("/get/{configName}")public String test(@PathVariable String configName) {return configName + "的值为:" + env.getProperty("tony." + configName);}
}

6、redis

package com.dongnaoedu.springcloud.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import redis.clients.jedis.JedisPool;@Configuration
@RestController
@RequestMapping("/redis")
public class RedisController {@AutowiredEnvironment env;@Bean@RefreshScope// 在refresh之后,这个实例会被刷新public JedisPool jedisPool() {String host = env.getProperty("redis.host");int port = env.getProperty("redis.port", Integer.class);return new JedisPool(host, port);}@Autowiredprivate JedisPool jedisPool;// 获取一个key的值 并打印出来@RequestMapping("/get/{key}")public String sendSms(@PathVariable String key) {String value = jedisPool.getResource().get(key);return "    redis中" + key + "的值为:" + value;}
}

当gitlab上的配置改变了,直接调用接口获取到的值会是改变之前的值,得先调用refresh(post接口)接口刷新后再调用目标接口,这样才能获取到最新的值。

六、MVC

1、springmvc中的配置加载 和 环境化
profile激活不同环境,加载不同的配置
普通mvc程序通过 xml中 <beans profile="dev">...</beans> 方式可以进行配置

  <beans profile="test">  <bean id="testDatasource" class="xxxxx.datasource"/>  </beans>  <beans profile="prod">  <bean id="prodDatasource" class="xxxxx.datasource"/></beans>  

2、在xml配置文件中配置<context:property-placeholder location="classpath:sms.properties"/>就可以设置${}里取到的变量值就是sms.properties里配置的值。
3、spring cloud config 工作模式
配置项怎么配置? 通过配置文件进行配置并存储在git中
谁去读配置文件? configserver配置中心
应用系统中配置信息从哪来? 从configserver远程获取
3、全局刷新怎么实现的?
通过rabbitmq发送消息, 订阅的客户端(应用系统)收到消息后,判断是否需要刷新
具体实现后面spring cloud stream/bus会讲到

4、自动刷新是什么一个机制?
gitlab提交配置更新的时候,触发一个事件(项目-设置-集成-webhook)

发起http请求到config-server monitor组件
http://192.168.99.1:8888/monitor 这是配置中心地址

configserver解析请求,通过spring cloud bus消息总线发送通知给各服务(和/bus/refresh一样)
对应的webapi类:PropertyPathEndpoint#notifyByPath
5、安全机制:如何保证配置的保密性?
项目中的密码 不应该明文存储,应该是密文
configserver访问控制:用户名密码
文件内容加密:configserver中设置spring.cloud.config.server.encrypt.enabled=true
再配置一个秘钥:encrypt.key=12345678
配置文件中密文需要以{cipher}做为标识:如spring.rabbitmq.password= ‘{cipher}31010f99731d4bd8aa7e3ce76152b5686265e1160043aac7cf769c3c8e1bb7ef’

对应的web api:EncryptionController#encrypt 加密
EncryptionController#decrypt 解密

springcloud config 分布式配置中心相关推荐

  1. SpringCloud Config分布式配置中心

    目录 一.概述 二.Config服务端配置与测试 配置读取规则 三.Config客户端配置与测试 bootstrasp.yml 四.Config客户端之动态刷新 一.概述 官网:Spring Clou ...

  2. SpringCloud版本Hoxton SR5 --- 第七讲:SpringCloud Config 分布式配置中心+整合bus、rabbitmq、actuator

    传送门:SpringCloud版本Hoxton SR5 --- 第一讲:认识 先看SpringCloud Config 可以完成的功能,或者说他在项目中的定位和作用. SpringCloud conf ...

  3. idea 链接github 无法成功登陆, SpringCloud Config 分布式配置中心配置

    Server前面加上https://  前缀 uri要写成链接地址 ,写成[git@github.com:godbar/hello-world.git] 不知道为啥总是报错,Auth fail

  4. SpringCloud Config 分布式配置

    SpringCloud Config 分布式配置 Dalston.RELEASE Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持.使用Config Server, ...

  5. SpringCloud微服务架构,Config 分布式配置中心,Bus 消息总线, Stream 消息驱动,Sleuth+Zipkin 链路追踪

    Config分布式配置中心 Config 概述 概述 • Spring Cloud Config 解决了在分布式场景下多环境配置文件的管理和维护. • 好处: • 集中管理配置文件 • 不同环境不同配 ...

  6. SpringCloud教程-分布式配置中心Config (SpringCloud版本Greenwich.SR4)

    文章目录 Config(分布式配置中心)简介 创建服务端ConfigServer 创建客户端config-client 代码地址: github-spring-cloud地址 Config(分布式配置 ...

  7. Spring Cloud入门-Config分布式配置中心(Hoxton版本)

    文章目录 Spring Cloud入门系列汇总 摘要 Spring Cloud Config 简介 在Git仓库中准备配置信息 配置仓库目录结构 master分支下的配置信息 dev分支下的配置信息 ...

  8. SpringCloud学习(十八):Config分布式配置中心的介绍与搭建

    目录 一.概述 1.分布式系统面临的配置问题 2. Config配置中心是什么 3.Spring Config能做什么 二.Config总控中心配置与测试 1.在Gitee上新建仓库 2.本地硬盘目录 ...

  9. SrpingCloud 之SrpingCloud config分布式配置中心实时刷新

    默认情况下是不能及时获取变更的配置文件信息 Spring Cloud分布式配置中心可以采用手动或者自动刷新 1.手动需要人工调用接口   监控中心 2.消息总线实时通知  springbus 动态刷新 ...

最新文章

  1. Erlang 数据类型。。
  2. MP3 编码解码 附完整c代码
  3. python_atp框架
  4. 三星note4 N9100刷回4.4.4系统后无法usb连接电脑
  5. GC Blocks Lost等待事件
  6. Android开发笔记(三十三)文本文件和图片文件的读写
  7. webpack配置_webpack的配置
  8. 程序包管理之编译安装
  9. 宽度学习(Broad Learning System)
  10. 造DPU芯片,如梦幻泡影?丨虚构短篇小说
  11. RAIN - SEKAI NO OWARI - 歌词翻译及罗马音译整理
  12. 139显示无法imap服务器,139邮箱客户端 imap服务器
  13. gradient()函数的理解
  14. android内存dump分析,闭眼能敲,Android内存分析command
  15. WordPress 前端投稿/编辑插件 DJD Site Post(支持游客和已注册用户)
  16. 【转】2009最受欢迎中国技术博客评选(PB50)结果公布
  17. JC-6、OpenCV+Tensorflow入门人工智能图像处理
  18. stm32外部中断问题(每次stm32进行系统复位按键控制NRST=0,程序立马进入中断服务函数)
  19. 2022-04-05 学习记录--React-React UI组件库-ant-design(蚂蚁金服)—— 按需引入样式 + 自定义主题
  20. Java面试——缓存

热门文章

  1. JVM-01Java内存区域与内存溢出异常(上)【运行时区域数据】
  2. linux7 security,SECURITY-centos7下NFS使用与配置
  3. 解析远程服务器响应错误,远程服务器返回一个意外的响应:(400)错误的请求,WCF...
  4. java在mac下开发环境_Java开发环境安装(MacOS、Windows)
  5. pythonhtml内容比较_Python使用difflib模块比较两个文件内容异同,同时输出html易浏览...
  6. 微信小程序-WXML转换类型
  7. 微信小程序开发之scroll-view上拉加载数据实现
  8. TypeScript学习笔记1:变量赋值及书写方式
  9. 2020-12-09 深度学习 卷积神经网络中感受野的详细介绍
  10. python 图片base64 编解码,转换成Opencv,PIL.Image图片格式