【Spring Cloud学习之五】配置中心
环境
eclipse 4.7
jdk 1.8
Spring Boot 1.5.2
Spring Cloud 1.2
一、什么是配置中心
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库(或者SVN仓库)中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
二、远程配置仓库
配置文件可以选择Git、Svn、本地目录,GitHub、GitLab、gitee都是基于web的Git仓库,Spring Cloud默认使用Git,如果使用Svn需要引入额外依赖org.tmatesoft.svnkit。
这里使用GitHub:
新建仓库:config-repo
上传配置文件:application-dev.yml 内容:version:0.0.1-SNAPSHOT

三、搭建config-server
1、新建maven工程



2、pom.xml
<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>
<groupId>com.wjy</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> </project>
3、application.yml
server:
port: 8889
spring:
application:
name: config-server
cloud:
config:
label: master
server:
git:
password:
searchPaths: config-repo
uri: https://github.com/cac2020/config-repo.git
username:
4、启动类
package com.wjy; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication
@EnableConfigServer
public class ConfigServerApp { public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class, args);
} }
5、测试验证
查询配置中心:http://localhost:8889/config-repo/dev

查询配置文件:http://localhost:8889/application-dev.yml

四、搭建config-client
1、新建maven工程



2、pom.xml
<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>
<groupId>com.wjy</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <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> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> </project>
3、bootstrap.yml
应用启动先读取bootstrap.yml ,然后读取远程配置中心配置,然后再读取application.yml配置。
server:
port: 8881
#spring.cloud.config.label 指明远程仓库的分支
#spring.cloud.config.profile指明配置文件类型 dev-开发环境 test-测试环境 pro-正式环境
#spring.cloud.config.uri 指明配置服务中心的网址
spring:
application:
name: config-client
cloud:
config:
label: master
profile: dev
uri: http://134.32.80.196:8889/
注意点:yml配置文件冒号后面要跟一个空格。
4、controller
package com.wjy.controller; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class ConfigController { @Value("${version}")
String versionstr; @RequestMapping(value = "/getVersion")
public String getVersion () {
return versionstr;
} }
启动类
package com.wjy; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ConfigClientApp { public static void main(String[] args) {
SpringApplication.run(ConfigClientApp.class, args);
} }
5、测试验证
http://localhost:8881/getVersion

五、配置免重启自动刷新
1、使用spring-boot-starter-actuator的refresh
2、与github的webhook、svn的hook进行配合刷新;
六、配置中心高可用
将config-server、config-client都可以注册到注册中心作为微服务管理。
参考:
Config 配置中心与客户端的使用与详细
Spring Cloud 配置中心的基本用法
配置自动更新
【Spring Cloud学习之五】配置中心的更多相关文章
- spring cloud学习(六) 配置中心-自动更新
上一篇学习了spring cloud config的基本使用,但发现有个问题,就是每次更改配置后,都需要重启服务才能更新配置,这样肯定是不行的.在上网查资料了解后,spring cloud支持通过AM ...
- spring cloud学习(五) 配置中心
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务中心采用Git的方式存储配置文件,因此我们很容易部署修改,有助于对环境配置进行版本管理. 一.配置中心 ...
- Spring Cloud Config的配置中心获取不到最新配置信息的问题
Spring Cloud Config的配置中心获取不到最新配置信息的问题 http://blog.didispace.com/spring-cloud-tips-config-tmp-clear/
- 跟我学SpringCloud | 第六篇:Spring Cloud Config Github配置中心
SpringCloud系列教程 | 第六篇:Spring Cloud Config Github配置中心 Springboot: 2.1.6.RELEASE SpringCloud: Greenwic ...
- Spring Cloud Config 实现配置中心,看这一篇就够了
Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Clou ...
- Spring Cloud Config(配置中心)
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code 一.简介 Spring Cloud Config为分布式系统中的外部配置提供服务器和客 ...
- Spring Cloud Config 分布式配置中心使用教程
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...
- Spring Cloud 2-Config 分布式配置中心(七)
Spring Cloud Config 1.github配置 2.服务端配置 pom.xml application.xml Application.java 3.配置和命名 1. 配置加载顺序 ...
- Spring Cloud Config 分布式配置中心【Finchley 版】
一. 介绍 1,为什么需要配置中心? 当服务部署的越来越多,规模越来越大,对应的机器数量也越来越庞大,靠人工来管理和维护服务的配置信息,变得困难,容易出错. 因此,需要一个能够动态注册和获取服务信息的 ...
随机推荐
- git 学习笔记 —— 切换和恢复提交版本( git reset/reflog/tag 命令)
记录一下关于 git 不同提交版本间切换的操作以及如何恢复至切换之前的版本. 切换到之前提交的版本 —— git reset --hard 笔者在使用 git 时,首先接触到了一个"黑魔法& ...
- Intellij IDEA运行报Command line is too long解法
修改项目下 .idea\workspace.xml,找到标签 <component name="PropertiesComponent"> , 在标签里加一行 < ...
- react的优点:兼容了dsl语法与UI的组件化管理
react的优点:兼容了dsl语法与UI的组件化管理. 组件化管理的dsl描述 UI: 虚拟dom:
- What is a Servlet?
Servlet 工作原理解析 https://www.ibm.com/developerworks/cn/java/j-lo-servlet/index.html 你可以理解为,Spring MVC是 ...
- Web安全之CSRF基本原理与实践
阅读目录 一:CSRF是什么?及它的作用? 二:CSRF 如何实现攻击 三:csrf 防范措施 回到顶部 一:CSRF是什么?及它的作用? CSRF(Cross-site Request Forger ...
- ACM数据结构-树状数组
模板: int n; int tree[LEN]; int lowbit(int x){ return x&-x; } void update(int i,int d){//index,del ...
- window.showModelessDialog传值
参数传递:1. 要想对话框传递参数,是通过vArguments来进行传递的.类型不限制,对于字符串类型,最大为4096个字符.也可以传递对象,例如:------------------------ ...
- mybatis-generator 模板
generatorConfig.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ge ...
- 线程:Java中wait、notify、notifyAll使用详解
基础知识 首先我们需要知道,这几个都是Object对象的方法.换言之,Java中所有的对象都有这些方法. public final native void notify(); public final ...
- pymysql装饰器封装
pymysql装饰器封装 def openClose(fun): def run(sql=None): coon =pymysql.connect(host='localhost' ,port=330 ...