Spring Cloud Config的目标是在在大量的微服务中,将服务配置信息和和服务的实际物理部署分离,且服务配置服务不应与服务实例一起部署。配置信息应该作为环境变量传递给正在启动的服务,或者在服务启动时从存储库(文件系统,Git)中读取。

下面,分别从个方面来讲Config:Config Server,Config Client,High availability Config Server,使用Spring Security保护Config Server,配置自动刷新。

(1)搭建Config Server

首先,在pom.xml中添加依赖spring-cloud-config-server。

<!-- Spring cloud: config server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

其次,在启动类ServerConfigApplication中加入@EnableConfigServer注解。

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

我们知道,Config Server是用来集中管理配置文件的地方,那么在哪存放这些配置文件呢?Config Server提供了两种方法,分别是Git Solution和Classpath and file-based solution。

(1.1)Git Solution

Git Solution就是把配置文件放到Git Repository中,下面代码使用HTTPS跳过SSL验证,使用username和password来连接Git Repository:

bootstrap.yml

# bootstrap.yml用来程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等
# bootstrap.yml先于application.yml加载
spring:
application:
name: server-config # 使用对称加密设置secret key(https://blog.csdn.net/u012702547/article/details/78499458)
# curl http://{confgit server host}:{port}/server-config/encrypt -d {pass} (need remove security)
encrypt:
key: my-secret-key

application.yml

# Git solution
spring:
cloud:
config:
server:
git:
uri: https://github.com/lyz170/spring-cloud-demo.git
# https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_skipping_ssl_certificate_validation
skipSslValidation: true
# https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_placeholders_in_git_search_paths
searchPaths: 'config/{application}'
username: xxxxxxxx
password: '{cipher}xxxxxxxxxxxxx'

我们可以看到,我们使用了一个叫spring-cloud-demo的repository,因为用了HTTPS访问,所以我们跳过SSL验证,通过查找仓库中config目录下的所有applications,使用username和password来连接Git Repository。这里的password使用了JCE加密,需要从Oracle官网上下载额外的jar包,如果不了解的话可以查阅相关资料。

关于更多的Git配置,可以查阅https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_git_backend

(1.2)Classpath and file-based solution

就是把配置文件全部放在项目中(这里是src/main/resources/config),通过classpath去读取:

bootstrap.yml

spring:
application:
name: server-config

application.yml

# Classpath and file-based solution
spring:
profiles:
active: native
cloud:
config:
server:
native:
searchLocations: classpath:config,classpath:config/app1,classpath:config/app2

无论是用(1.1)还是(1.2),目录和文件的命名都是有要求的。目录是spring.application.name的值,文件为{application}-{profile}.yml。假设有2个application(app1,app2),和2个环境(dev,prod),目录和文件的命名如下:

config
|--app1
|--app1.yml
|--app1-dev.yml
|--app1-prod.yml
|--app2
|--app2.yml
|--app2-dev.yml
|--app2-prod.yml

[注] 启动时无论环境参数是dev还是prod,都会先读取默认的{app}.yml,然后用dev或prod中的参数覆盖默认的{app}.yml。所以,可以把一些共通的配置配到{app}.yml中,把需要改变或增加的配置配到相应的{app}-{profile}.yml中。

配置完成后,我们启动该服务,可以通过下面的格式看到配置文件的内容:

http://{hostname}:{port}/{应用名}/{环境名}[/{分支名}]
http://{hostname}:{port}/{应用名}-{环境名}.yml
http://{hostname}:{port}/{分支名}/{应用名}-{环境名}.yml

例如:http://127.0.0.1:10010/server-config/app1/prod,http://127.0.0.1:10010/server-config/app1-dev.yml

(2) 搭建Config Client

先引入依赖包spring-cloud-starter-config。

<!-- Spring cloud starter: config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

在配置文件(bootstrap.yml)中添加配置,指定config server服务的位置。

spring:
application:
name: app-db
cloud:
config:
uri: http://localhost:10010/server-config

这样,在服务启动时,会根据参数[--spring.profiles.active={env}],去Config Server拿到相应环境的配置文件后,再启动项目。

(3) 搭建High availability Config Server

既然是高可用的,所以首先需要创建多个Config Server实例。然后这里有两种方式:Eureka Service Discovery和Multiple Urls。

(3.1)Eureka Service Discovery

这里使用了Spring Cloud Netflix and Eureka Service Discovery,即把所有Config Server作为Eureka Client注册Eureka Server中,再通过Eureka的Discovery,只需配置一个service-id即可连接多个Config Server。

如何把Config Server注册Eureka Server中不再赘述,可以去看Spring Cloud(3):服务发现(Eureka)的第2部分。配置好Eureka后,需要分别配置Config Server和Config Client。

Config Server - bootstrap.yml

# If the Config Server is secured with HTTP Basic, you can configure the credentials as user and password.
# Also, if the Config Server has a context path, you can set configPath.
# http://cloud.spring.io/spring-cloud-static/Greenwich.RELEASE/single/spring-cloud.html#discovery-first-bootstrap
eureka:
instance:
metadataMap:
# user: xxxxxxxx
# password: '{cipher}xxxxxxxx'
configPath: /server-config

[注] 如果配置了server.servlet.ocntext-path,则需要配置configPath;如果使用了HTTP Basic,则要在这里配置认证信息。

Config Client - bootstrap.yml

spring:
application:
name: app-db
cloud:
config:
# 使用service-id代替url实现config高可用
discovery:
enabled: true
serviceId: server-config

[注] 这里的service-id就是Config Server的application name,也是在Eureka Server中注册的application name。

在配置Config Client时,我们发现,配置Config Server的优先级很高,在bootstrap.yml中。然而,使用Discovery的方式必须在配置Config Server前配置好Eureka。这也就导致了bootstrap.yml中的配置过多,并且不能把这些配置配到Config Server中。在多环境中,我们还要在本地创建多个bootstrap-{profile}.yml。这个问题目前我还没有解决方法。

本部分参考:https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#discovery-first-bootstrap

(3.2)Multiple Urls

其实就是(2)中配置多个URL即可。

spring:
application:
name: app-db
cloud:
config:
uri: "http://localhost:10010/server-config,\
http://127.0.0.1:10011/server-config,\
http://127.0.0.1:10012/server-config"

需要说明的是,Config Client会逐个连接,只有在Config Server未运行时(即应用程序已退出时)或发生连接超时时,才能确保高可用性(即跳过当前url连接下一个url)。但是,如果Config Server返回500(内部服务器错误)响应或Config Client从Config Server收到401(由于凭据错误或其他原因),表示用户问题而不是可用性问题,则Config Client不会尝试去连接下一个url。

本部分参考:https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_specifying_multiple_urls_for_the_config_server

(4)使用Spring Security保护Config Server

首先,需要添加spring-cloud-starter-security依赖。

<!-- Spring cloud starter: security -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>

然后,添加一个user和password用于登录即可。

@EnableWebSecurity
public class ServerConfigWebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
} @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//@formatter:off
PasswordEncoder encoder = new BCryptPasswordEncoder();
auth.inMemoryAuthentication()
.withUser("config-user").password("{bcrypt}" + encoder.encode("config-user")).roles("USER");
//@formatter:on
}
}

当Config Client连接时,如果使用了Eureka Service Discovery方式,只需在Config Server中添加如下配置:

# 使用对称加密设置secret key(https://blog.csdn.net/u012702547/article/details/78499458)
# curl http://{confgit server host}:{port}/server-config/encrypt -d {pass} (need remove security)
encrypt:
key: my-secret-key # If the Config Server is secured with HTTP Basic, you can configure the credentials as user and password.
# Also, if the Config Server has a context path, you can set configPath.
# https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#discovery-first-bootstrap
# 该配置只用于Eureka Service Discovery(spring.cloud.config.discovery.serviceId) 如果使用了Multiple Urls则不需要配置
eureka:
instance:
metadataMap:
user: xxxxxxxx
password: '{cipher}xxxxxxxx'
configPath: /server-config

如果使用了Url或Multiple Urls,可以在Config Client中这样写:

encrypt:
key: my-secret-key config:
username: xxxxxxxx
password: '{cipher}xxxxxxxx' spring:
application:
name: app-db
cloud:
config:
uri: "http://${config.username}:${config.password}@localhost:10010/server-config,\
http://${config.username}:${config.password}@127.0.0.1:10011/server-config,\
http://${config.username}:${config.password}@127.0.0.1:10012/server-config"

(5)配置自动刷新

简要来说,就是使用@RefreshScope注解,然后客户端执行/refresh端点即可。这里省略。

Spring Cloud(3):配置服务(Config)的更多相关文章

  1. spring cloud 2.x版本 Config配置中心教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的文章eureka-server的实现. 参考 eureka-server ...

  2. Spring Cloud构建微服务架构:服务网关(路由配置)【Dalston版】

    转载:http://blog.didispace.com/spring-cloud-starter-dalston-6-2/ 原创  2017-08-26  翟永超  Spring Cloud 被围观 ...

  3. Spring Cloud构建微服务架构(一)服务注册与发现

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...

  4. Spring Cloud Consul 实现服务注册和发现

    Spring Cloud 是一个基于 Spring Boot 实现的云应用开发工具,它为基于 JVM 的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布 ...

  5. Spring Cloud构建微服务架构(二)服务消费者

    Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides clie ...

  6. 使用Spring Cloud连接不同服务

    http://www.infoq.com/cn/articles/spring-cloud-service-wiring 主要结论 Spring Cloud为微服务系统中相互依赖的服务提供了丰富的连接 ...

  7. Spring Cloud构建微服务架构(五)服务网关

    通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: 我们使用Spring Cloud Netflix中的Eureka实现了服务 ...

  8. Spring Cloud构建微服务架构 - 服务网关

    通过之前几篇Spring Cloud中几个核心组件的介绍,我们已经可以构建一个简略的(不够完善)微服务架构了.比如下图所示: alt 我们使用Spring Cloud Netflix中的Eureka实 ...

  9. 基于Spring Cloud的微服务入门教程

    (本教程的原地址发布在本人的简书上:http://www.jianshu.com/p/947d57d042e7,若各位看官有什么问题或不同看法请在这里或简书留言,谢谢!) 本人也是前段时间才开始接触S ...

  10. 干货|基于 Spring Cloud 的微服务落地

    转自 微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的 ...

随机推荐

  1. jenkins"控制台输出"乱码问题解决

    今天在搭建Jenkins环境时,安装完Tomcat.Jenkins.创建项目进行构建后,在查看控制台输出时,结果中文全部显示乱码.然后呢,就是漫长的解决历程,最终呢,解决乱码问题的时间终于超过了环境搭 ...

  2. ak-2

    1.人大金仓数据 jdbc 安装完成之后有个jdbc的文件夹  里边就是 jdbc包,相应的window前端工具有jdbc示例,因为太多没看全呢,有一个“”模式“”的概念  jdbc连接时如果要指定模 ...

  3. shell 跟$有关的各种怪命令集锦

    $ 这个程式的执行名字 $n 这个程式的第n个参数值,n=.. $* 这个程式的所有参数,此选项参数可超过9个. $# 这个程式的参数个数 $$ 这个程式的PID(脚本运行的当前进程ID号) $! 执 ...

  4. Codeforces Round #452 (Div. 2) 899E E. Segments Removal

    题 OvO http://codeforces.com/contest/899/problem/E Codeforces Round #452 (Div. 2) - e 899E 解 用两个并查集(记 ...

  5. JDBC (Java DataBase Connectivity)数据库连接池原理解析与实现

    一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...

  6. 【转】pe结构详解

    (一)基本概念 PE(Portable Execute)文件是Windows下可执行文件的总称,常见的有DLL,EXE,OCX,SYS等, 事实上,一个文件是否是PE文件与其扩展名无关,PE文件可以是 ...

  7. Python GUI编程(Tkinter)(一)

    tk官网的教程学习: https://tkdocs.com/tutorial/firstexample.html 学习blog: https://www.cnblogs.com/aland-1415/ ...

  8. vue中用watch监听当前路由

    之前做项目时,特别是后台项目,左边都有侧边栏,我们需要做到点击某个侧边栏的项让这个项高亮,之前采用的是给每个项绑定一个值,点击某个项时,就将这个值付给一个变量,在每一项上判断这个变量是否与每项上的值相 ...

  9. Linux长格式文件属性介绍

    长格式文件属性 查看长格式文件命令:ll (或ls -l) (1)-:文件类型 -:普通文件 d:目录 b:块设备文件(随机读取) c:字符设备文件(顺序读取) p:管道文件 s:Socket套接字文 ...

  10. 关于centos6版本执行程序报错:libc.so.6: version GLIBC_2.14 not found的解决

    执行后程序报错: libc.so.6: version GLIBC_2.14 not found 这种情况是因为当前服务器glibc的版本比较低造成的(不出意外是glibc_2.12是最高版本): 1 ...