目的:

  1、SpringCloud Config简介

  2、Config Server基本使用

  3、Config Client基本使用

  4、Config整合Eureka

  5、Config配置搜索路径


SpringCloud Config简介

 SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

 SpringCloud Config分为服务端和客户端两部分。
 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
用途:
 集中管理配置文件。
 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release。
 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心。
 统一拉取配置自己的信息。
 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置。
 将配置信息以REST就接口的形式暴露。

Config Server端主要和Git/SVN服务器

通俗点,就是统一管理配置,包括方便切换环境配置,以及修改配置无需动代码,省心省力;

如果用上SpringCloud Bus,能实现无需重启,自动感知配置变化以及应用新配置;


Config Server基本使用

首先第一步,要搞个 configServer来联通远程GIT仓库,来读取远程配置;

这里GIT仓库,我们一般选用GitHub https://github.com/,或者码云  https://gitee.com/

我们这里用GitHub演示github官网:https://github.com/

关于github在window系统中使用可以查看博客https://www.cnblogs.com/huangting/p/11684508.html

建个仓库 microservice-config  然后 Git下载本地

把仓库克隆到本地仓库以后把项目中的application.yml文件复制进去在push到github仓库中,测试yml文件可否上传到github

其中出现的window转义问题解决可看我上篇博客:https://www.cnblogs.com/huangting/p/11945774.html

成功。那么我们来玩一下Springcloudserver和github仓库的配置文件交互

首先在ideaul中创建一个Springboot工程,microservice-config-server-4001

然后在pom中添加一个依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ht</groupId>
<artifactId>htSpringCloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-config-server-4001</artifactId> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

在启动类ConfigServerApplication_4001中添加注释@EnableConfigServer

package com.ht.microserviceconfigserver4001;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer
@SpringBootApplication
public class MicroserviceConfigServer4001Application { public static void main(String[] args) {
SpringApplication.run(MicroserviceConfigServer4001Application.class, args);
} }

然后配置yml文件,给它添加一段集合git的url (记住访问git的http地址而不是ssh地址)

server:
port: spring:
application:
name: microservice-config
cloud:
config:
server:
git:
uri: https://github.com/haungting/htmicroservice-config.git
//从git你的仓库里面复制下来的http地址,url变化因人而异,这样git才可以请求

单单是这样还不行,我们还要配置本地映射:

找到本机C:\Windows\System32\drivers\etc 地址下的host文件

加以下配置:

127.0.0.1  configserver.ht.com

然后我们请求:http://configserver.ht.com:4001/application-xxx.yml

返回结果了正确的文本结果;

请求路径,也有相应的匹配规则:

The HTTP service has resources in the form:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

详细可参考:https://www.cnblogs.com/hellxz/p/9306507.html


Config Client基本使用

  这里我们需要建立Client端去调用server端,然后实现client端获取远程git配置信息

因为测试需要所以我们提交三个配置文件到远程git库

application.yml:

  active: dev 这里设置获取的文件名
---
spring:
profiles:
active: dev
---
spring:
profiles: dev
port:
---
spring:
profiles: test
port:

crm-dev.yml:

port:
  

crm-test.yml:

port:
  

然后我们新建一个springboot工程  microservice-config-client-5001

给项目pom文件中添加依赖并且修改父id:

<parent>
<groupId>com.ht</groupId>
<artifactId>htSpringCloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

因为git在读取配置文件的之后最先读取bootstrap.yml,并且bootstrap.yml中存放的都是项目中通用不改变的配置,

我们项目启动的时候,要调用server config端,获取配置信息

所以这里要bootstrap.yml配置文件

spring:
application:
name: application-dev
cloud:
config:
name: crm //做拼接的,所以必须要和git上面的文件开头名一样
uri: http://configserver.ht.com:4001
profile: test //这里去设置获取的端口文件名,根据上面的文件这里默认获取test那么端口就是888
label: master
fail-fast: true

这里给git配置了请求那么也需要配置本地映射否则无法访问:

找到本机C:\Windows\System32\drivers\etc 地址下的host文件添加配置:

127.0.0.1  client-config.ht.com

我们再来添加一个动态获取git仓库里面存放端口的文件类

ConfigClientController

package com.ht.microserviceconfigclient5001.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; /**
* 这个类可以根据git中yml文件配置更换去动态获取端口
*/
@RestController
public class ConfigClientController { @Value("${port}")
private String port; @GetMapping("/getPort")
public String getPort() {
return "测试你访问的yml文件的端口是:【"+port+"】";
} }

启动类不需要添加注释这里就不贴代码了

先启动4001,在启动5001

然后页面访问:

http://client-config.ht.com:5001/getPort

因为项目中bootstrap.yml文件中获取test文件中的端口所以获取的是888

根据上面push到git仓库的文件进行访问测试调用端口

http://configserver.ht.com:4001/application-dev.yml

通过git仓库中application.yml文件去调用dev文件的端口

http://configserver.ht.com:4001/application-test.yml

通过git仓库中application.yml文件去调用dev文件的端口

我们进去git仓库中去修改active获取的文件为test

然后在去访问

很显然active环境变成test


Config整合Eureka

  eureka整合config以及服务器提供者整合config

  首先是eureka整合config

  我们先搞个配置文件到git

spring:
profiles:
active:
- dev
---
server:
port: 2004
context-path: /
spring:
profiles: dev
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
---
server:
port: 2005
context-path: /
spring:
profiles: test
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

然后在push到仓库中去

新建Springboot工程:microservice-eureka-server-config-2004

添加pom.xml:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

bootstrap.yml

spring:
application:
name: microservice-eureka-server-config
cloud:
config:
name: eureka_config
uri: http://configserver.ht.com:4001 # 配置configserver地址
profile: dev # 级别
label: master # 分支 git中 默认master

application.yml

spring:
application:
name: microservice-eureka-server-config

启动类:

package com.ht.microserviceeurekaserverconfig2004;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
@EnableEurekaServer
public class MicroserviceEurekaServerConfig2004Application { public static void main(String[] args) {
SpringApplication.run(MicroserviceEurekaServerConfig2004Application.class, args);
} }

先启动 microservice-config-server-4001

再启动 microservice-eureka-server-config-2004

测试连接

http://localhost:2004/

http://eureka2001.ht.com:2004/

说明成功读取远程Git配置,然后eureka启动OK

然后我们把服务提供者和config整合,把服务提供者注册到eureka;

我们搞个配置provider_config.yml,push到远程GIT;

spring:
profiles:
active: dev
---
server:
port: 1007
context-path: / # 数据源配置
spring:
profiles: dev
application:
name: microservice-student
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:1007
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2004/eureka info:
groupId: com.ht.htSpringCloud
artifactId: microservice-student-provider-config-1007
version: 1.0-SNAPSHOT
userName: http://ht.com
phone: 123456
---
server:
port: 1008
context-path: / # 数据源配置
spring:
profiles: test
application:
name: microservice-student
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: root
jpa:
hibernate:
ddl-auto: update
show-sql: true eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:1008
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2004/eureka info:
groupId: com.ht.htSpringCloud
artifactId: microservice-student-provider-config-1008
version: 1.0-SNAPSHOT
userName: http://ht.com
phone: 123456

新建module:microservice-student-provider-config-1004

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ht</groupId>
<artifactId>htSpringCloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-student-provider-config</artifactId> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>com.ht</groupId>
<artifactId>microservice-common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency> <!--添加注册中心Eureka相关配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency> <!-- actuator监控引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

bootstrap.yml:

spring:
application:
name: microservice-student-provider-config
cloud:
config:
name: provider_config
uri: http://configserver.ht.com:4001 # 配置configserver地址
profile: dev # 级别
label: master # 分支 git中 默认master

application.yml

spring:
application:
name: microservice-student-provider-config

其他文件和原先的服务提供者一样

说明成功注册到服务注册中心了


Config配置搜索路径

我们所有的GIT远程端配置文件都是跟目录的,所有请求默认都是根目录,但是有时候,项目很多,配置文件需要根据子目录来划分,这时候,就需要来配置搜索路径了;比如aaa项目的配置文件放aaa目录下,bbb项目的配置文件放bbb目录下,不配置的话 是找不到的那些配置文件的,我们需要配置search-paths属性实现;

microservice-config-server-4001 configserver端 加个配置

server:
port: 4001 spring:
application:
name: microservice-config
cloud:
config:
server:
git:
uri: https://github.com/haungting/htmicroservice-config.git
search-paths: aaa,bbb

搞2个目录aaa,bbb 里面分别放3个配置文件 nns1.yml,nns2.yml

根据github的application.yml文件去写就行啦,差不多就这样

spring:
profiles:
active: dev
---
spring:
profiles: dev
name: aaadev
---
spring:
profiles: test
name: aaatest

然后push到远程仓库git

启动:microservice-config-server-4001

在浏览器中输入:http://configserver.ht.com:4001/nns2-test.yml或者http://localhost:4001/nns-test.yml

谢谢观看!!

SpringCloudConfig相关配置简介、使用、整合Eureka的更多相关文章

  1. 【AndroidStudio】关于SVN的相关配置简介

    AndroidStudio 的SVN 安装和使用方法与我以前用的其他IDE 都有很大差别,感觉特麻烦,网上相关资料很少,貌似现在 Git 比较流行,之前有用过 github 但是他只能是开源项目免费, ...

  2. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

  3. Spring学习笔记(二)——Spring相关配置&属性注入&Junit整合

    一.Spring的相关配置 1.1 Bean元素 class属性:被管理对象的完整类名 name属性:给Bean起个名字,能重复,能使用特殊字符.后来属性 id属性:给Bean起个名字,不能重复,不能 ...

  4. SpringCloud系列十:SpringCloudConfig 高级配置(密钥加密处理(JCE)、KeyStore 加密处理、SpringCloudConfig 高可用机制、SpringCloudBus 服务总线)

    1.概念:SpringCloudConfig 高级配置 2.具体内容 在 SpringCloudConfig 之中考虑到所有配置文件都暴露在远程仓库之中的安全性问题,所以提供有安全访问的处理机制,这样 ...

  5. SpringCloud系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客户端使用 SpringCloudConfig 进行配置、单仓库目录匹配、应用仓库自动选择、仓库匹配模式)

    1.概念:SpringCloudConfig 基础配置 2.具体内容 通过名词就可以发现,SpringCloudConfig 核心作用一定就在于进行配置文件的管理上.也就是说为了更好的进行所有微服务的 ...

  6. Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇

    Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...

  7. 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移

    不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...

  8. Ribbon整合Eureka组件,以实现负载均衡

    1整体框架的说明 在本案例的框架里,我们将配置一个Eureka服务器,搭建三个提供相同服务的Eureka服务提供者,同时在Eureka服务调用者里引入Ribbon组件,这样,当有多个url向服务调用者 ...

  9. Centos7中网络及设备相关配置

    centos7中,不再赞成使用ifconfig工具,取而代之的是nmcli工具,服务管理也是以systemctl工具取代了service,这些之前版本的工具虽然在centos7中还可以继续使用,只是出 ...

随机推荐

  1. 使用singer tap-postgres 同步数据到pg

    singer 是一个很不错的开源etl 解决方案,以下演示一个简单的数据从pg 同步到pg 很简单就是使用tap-postgres + target-postgres 环境准备 对于测试的环境的数据库 ...

  2. lyft amundsen简单试用

    昨天有说过amundsen 官方为我们提供了dockerc-compose 运行的参考配置,以下是一个来自官方的 quick start clone amundsen 代码 amundsen 使用了g ...

  3. prisma mongodb 试用

    prisma 已经支持mongodb了,我们需要做的就是安装新版本的prisma cli,后然初始化项目使用 环境准备 安装cli 注意使用新版本(prisma/1.32.2) 低版本有坑 npm i ...

  4. Switch ……case语句

    Switch(变量){ case 1: 如果变量和1的值相同,执行该处代码 break; case 2: 如果变量和2的值相同,执行该处代码 break; case 3: 如果变量和3的值相同,执行该 ...

  5. 洛谷P2029跳舞

    题目 DP, 用的\(dp[i][j]\)表示\(i\)之前的数选了\(j\)个得到的最大结果,然后状态转移方程应该是 \[if (j \% t == 0)~~dp[i][j] = max(dp[i] ...

  6. mysql 创建分组

    mysql> select * from table1; +----------+------------+-----+---------------------+ | name_new | t ...

  7. Java连接excel实现:通过姓名查找id和通过id查找姓名

    注意每个方法结束都要关闭workbook: 还有getIdbyname()方法中字符串flag与name的比较,一定要用equals()方法!!!: 剩下的不多解释,注释都在代码中: import j ...

  8. MySQL事务隔离级别(一)

    本文实验的测试环境:Windows 10+cmd+MySQL5.6.36+InnoDB 一.事务的基本要素(ACID) 1.原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做 ...

  9. beyond compare解决特殊字符无法输出、多sheet页无法对比以及文件太大超出系统内存问题的Excel转txt脚本

    beyond compare解决特殊字符无法输出.多sheet页无法对比以及文件太大超出系统内存问题的Excel转txt脚本 ' XLS_to_CSV.vbs ' ' Converts an Exce ...

  10. SQLServer string_split函数,撕裂函数,撕开函数

    declare @name char(1000) --注意:char(10)为10位,要是位数小了会让数据出错 set @name='s{sss}fc{fggh}dghdf{cccs}x' selec ...