在传统的应用中,我们的配置文件都是放在项目中,这个影响不大。但是在一个微服务架构的系统中,我们的微服务可能存在几十上百个,并且每个小的微服务可能又部署在多台机器上,那么这个时候如果我们的配置文件在都放在具体的微服务中,那么就不好进行维护了,因此我们需要将服务中的配置文件单独抽取出来,集中管理。spring cloud config 可以为我们做到配置的集中管理,动态刷新配置等。当然除了spring cloud config 还有其它的也可以实现配置的集中管理,此处简单的来看一下 spring cloud config 的配置。

实现功能

  1、config server 端的编写并注册到eureka上
    2、config server 端增加 basic 认证
    3、config server 路径查找,searchPaths 参数
    4、配置项进行对称加密处理
    5、其它的一些配置见具体的配置文件上
    6、客户端的快速失败
    7、config server 的一些加密和解密端点

注意:

     1、config server 和 config client 的配置文件都需要写在 bootstrap.yml 配置文件中

     2、启用配置加密时,需要从 oracle 官网下载 JCE ,并覆盖在 jdk/jre/lib/security 目录中

|- Java 6 JCE

|- Java 7 JCE

|- Java 8 JCE

代码结构

配置文件的访问路径

config server端访问路径

   例如:http://configer_server:port/{application}-{profile}.properties

http://localhost:8301/product-provider-config-client-8302-dev.properties

{application}   --->  product-provider-config-client-8302

  {profile}           --->  dev

  {label}              --->  master 没有指定默认就是 master 分支(此处指的是git的分支)。

即最终的配置的值product-provider-config-client-8302.propertiesproduct-provider-config-client-8302-dev.properties合并的值。

在 github 上创建一个仓库,作为配置存放的地方

配置服务器服务端编写

1、config server端增加basic认证,需要引入 spring security

2、由于引入了spring security 所以需要放行 /encrypt/status,/encrypt/**,/decrypt/** 这些端点,便于测试

3、配置服务器的配置放置在 git 服务器上,如果是在私有仓库,那么需要配置 username和password的值

4、默认情况下是第一次访问配置的时候从 git 服务器上 clone 下配置,修改成程序一启动就拉取配置

5、指定 git 配置存在在本地的路径

6、启用配置的对称加密

|- encrypt.key 这个值必须配置bootstrap.yml 配置文件中,否则不生效

|- 使用 post 访问 http://config_server_host:port/encrypt 请求body中放入 需要加密的字符串,访问得到加密的值。  server.port={cipher}459c0c93ceab39c1924bb5addf72f03dc5eedfed7edac44851ecb0a1a1e888c9

1、依赖的引入,主要引入 config server 和 spring security 的依赖

2、启动类上增加 @EnableConfigServer 注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ApplicationConfigServer8301 {
public static void main(String[] args) {
SpringApplication.run(ApplicationConfigServer8301.class, args);
}
}

3、配置文件 bootstrap.yml 的编写


   此处启用加密,需要将上方注意事项中的 JCE 下载下来,并覆盖在 JDK/jre/lib/security 目录中

加密后里面的配置的写法:

server:
port: '{cipher}459c0c93ceab39c1924bb5addf72f03dc5eedfed7edac44851ecb0a1a1e888c9'

配置客户端的编写,即具体的微服务配置

1、依赖的引入,主要是 config

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

2、bootstrap/yml 配置文件的编写

配置及运行结果

完整代码

config server 和 config client 演示代码: https://gitee.com/huan1993/spring-cloud-parent/tree/master/config

spring cloud config的使用的更多相关文章

  1. spring cloud config 入门

    简介 Spring cloud config 分为两部分 server client config-server 配置服务端,服务管理配置信息 config-client 客户端,客户端调用serve ...

  2. Spring Cloud Config

    Spring Cloud Config provides server and client-side support for externalized configuration in a dist ...

  3. Spring Cloud官方文档中文版-Spring Cloud Config(上)

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http ...

  4. Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...

  5. SpringCloud的配置管理:Spring Cloud Config

    演示如何使用ConfigServer提供统一的参数配置服务 ###################################################################一.概 ...

  6. 搭建spring cloud config

    很久没更新了,因为不是专职研究spring cloud,因此更新速度得看工作强度大不大,每天能抽出的时间不多,如果更新太慢了,并且有小伙伴看的话,请见谅了. Spring Cloud简介 Spring ...

  7. Spring Cloud Config - RSA简介以及使用RSA加密配置文件

    简介 RSA非对称加密有着非常强大的安全性,HTTPS的SSL加密就是使用这种方法进行HTTPS请求加密传输的.因为RSA算法会涉及Private Key和Public Key分别用来加密和解密,所以 ...

  8. Spring Cloud Config 分布式配置中心使用教程

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...

  9. 【spring实战第五版遇到的坑】第14章spring.cloud.config.uri和token配置项无效

    本文使用的Spring Boot版本为:2.1.4.RELEASE Spring Cloud版本为:Greenwich.SR1 按照书上的做法,在application.yml中配置配置服务器的地址和 ...

  10. .NET Core微服务之基于Steeltoe使用Spring Cloud Config统一管理配置

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 =>  Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...

随机推荐

  1. c# 递归树形菜单

    首先创建模型类Menus public class Menus { //菜单Id public int Id { get; set; } //菜单名 public string MenuName { ...

  2. el-table回显遇到的坑

    使用element ui 的el-table在做到复选框回显勾中的问题时,整整困惑了我一天,当时百度了一下,好多人都说是  this.$nextTick(() => {})的问提,在组件中监听w ...

  3. ansible 批量安装yum包

    1.首先安装一下ansible yum install ansible 2.修改一下ansible的参数以防ssh过去的时候需要首次判断yes  或者no sed -i 's/#host_key_ch ...

  4. FastAPI(5)- get 请求 - 查询参数 Query Parameters

    什么是查询参数? http://127.0.0.1:8000/get?name=xxx&age=18 http://127.0.0.1:8000/get?age=18&name=xxx ...

  5. 328 day07线程池、Lambda表达式

    day07[线程池.Lambda表达式] 主要内容 等待与唤醒案例 线程池 Lambda表达式 教学目标 -[ ] 能够理解线程通信概念 -[ ] 能够理解等待唤醒机制 -[ ] 能够描述Java中线 ...

  6. Java中short和int的转换

    例子[1]: 第一种情况: short a = 1; a = a + 1; // 这一步会报错 System.out.print(a); 编译器会报错,原因如下: 第二种情况: short a = 1 ...

  7. windows环境下永久修改pip镜像源的方法

    在windows环境下修改pip镜像源的方法 (1)在windows文件管理器中,输入 %APPDATA% (2)会定位到一个新的目录下,在该目录下新建pip文件夹,然后到pip文件夹里面去新建个pi ...

  8. PHP中命名空间是怎样的存在(一)?

    命名空间其实早在PHP5.3就已经出现了.不过大部分同学可能在各种框架的使用中才会接触到命名空间的内容,当然,现代化的开发也都离不开这些能够快速产出的框架.这次我们不从框架的角度,仅从简单的代码角度来 ...

  9. 3gcms-Flash幻灯片上传后图片模糊解决办法

    很简单,不用纠结,直接修改admin/lib/action/FileAction.class.php 将 $upload->thumbMaxWidth='300'; //以字串格式来传,如果你希 ...

  10. 哪5种IO模型?什么是select/poll/epoll?同步异步阻塞非阻塞有啥区别?全在这讲明白了!

    系统中有哪5种IO模型?什么是 select/poll/epoll?同步异步阻塞非阻塞有啥区别? 本文地址http://yangjianyong.cn/?p=84转载无需经过作者本人授权 先解开第一个 ...