文章目录

  • 概述
  • 搭建Config Server
    • Step1: 新建Config Server微服务,添加依赖
    • Step2: 将Config Server 注册到Eureka Server上
    • Step3: 启动@EnableConfigServer,将服务变更为Config Server
    • Step5: 新建远端Git工程,用于存放配置文件
    • Step6: 启动测试
  • 搭建Config Client
    • Step1.添加spring-cloud-config-client依赖
    • Step2. 新建bootstrap.yml
    • Step3. 启动artisan-order
    • Step4. 验证
  • 配置中心的高可用
    • Config Server注册到注册中心上的场景
    • Config Server未注册到注册中心上的场景
  • 修改Eureka Server的默认端口8761的情况
    • Step1. 将Eureka的配置下沉到客户端上
    • Step2. 注释掉默认配置文件的配置
  • 遗留问题
  • 代码

概述

入门文章请看我之前整理的博客: Spring Cloud【Finchley】-19Spring Cloud Config之Config Server和Config Client


搭建Config Server

总结下Spring Cloud的三部曲

1. 增加依赖

2. 启用注解@EnableXXX

3. 配置文件


Step1: 新建Config Server微服务,添加依赖

关键依赖如下:

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

Step2: 将Config Server 注册到Eureka Server上

2.1启动类增加 @EnableEurekaClient 注解 ,作为Eureka Client 注册到注册中心上去

2.2application.yml 新增配置如下

spring:application:name: artisan-config
eureka:client:service-url:defaultZone: http://localhost:8761/eureka/

Step3: 启动@EnableConfigServer,将服务变更为Config Server

2.1启动类增加 @EnableConfigServer注解

2.2application.yml 新增配置如下

又加了个指定端口 9898 和 force-pull 属性 。 完整配置如下

Cannot pull from remote the working tree is not clean. 这种报错可以通过配置强制拉属性force-pull: true 。 由于Spring Cloud配置服务器会复制远程git存储库,如果本地副本变得不干净,那么Spring Cloud配置服务器就不能更新远程存储库中的本地副本。通过设置强制拉属性为true,使Spring Cloud配置服务器从远程存储库中强制pull。

官方指导: https://github.com/spring-cloud/spring-cloud-config/blob/master/docs/src/main/asciidoc/spring-cloud-config.adoc#force-pull-in-git-repositories


Step5: 新建远端Git工程,用于存放配置文件

之前创建了一个远端Git, 地址为: https://github.com/yangshangwei/spring-cloud-config-center 我们就直接拿来用吧

搭建过程: 搭建Config Server的后端存储

为了测试下,我们新建几个order的配置文件 ,放些配置进去


Step6: 启动测试

启动Eureka Server 和 该工程

访问Eureka http://localhost:8761/

访问: http://localhost:9898/artisan-order-dev.yml

basedir的配置看日志


搭建Config Client

上面我们把Order微服务的配置文件放到了远端的Git,自然而然本地的工程直接使用远端存储的配置文件既可以了,本地的配置自然而言就应该不需要了。


Step1.添加spring-cloud-config-client依赖

在Order工程中添加 spring-cloud-config-client 依赖

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

Step2. 新建bootstrap.yml

spring:application:name: artisan-ordercloud:config:profile: dev# 可配置多个,不推荐使用,因为需要设置具体的ip.服务端修改或者新增IP后,要同步修改# uri: http://localhost:9898/,http://localhost:9999/discovery:# 指定Config Server在服务发现中的service Id ,默认为configserverservice-id: ARTISAN-CONFIG# 表示使用服务发现组件中的Config Server,而不自己指定Config Server的uri,默认为falseenabled: true

上述配置信息一定要写到bootstrap.yml中。 如果配到了application.yml中,spring.cloud.config.uri 就会访问默认的8888端口,而非配置的端口了。

bootstrap.yml 是被一个父级的 Spring ApplicationContext 加载的,在加载application.yml 的 ApplicationContext之前。配置在 bootstrap.yml 中的属性优先级更高,默认情况下不能被本地配置覆盖。

当启动不了的时候,比如从远端加载不到配置文件的时候,注意观察日志:

2019-04-04 16:42:41.844  INFO 27888 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:9898/
2019-04-04 16:42:45.426  INFO 27888 --- [  restartedMain] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=artisan-order, profiles=[dev], label=null, version=16152eaaf132cd054c2ac538a11fe4bb7fff6583, state=null
2019-04-04 16:42:45.427  INFO 27888 --- [  restartedMain] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/yangshangwei/spring-cloud-config-center/artisan-order.yml'}]}

Step3. 启动artisan-order

启动后观察是否注册到Eureka上


Step4. 验证

package com.artisan.order.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ConfigController {@Value("${spring.datasource.url}")private String url;@GetMapping("/value")public String getValueFromGit(){return url;}
}

获取 spring.datasource.url的值

访问 http://localhost:8081/value


配置中心的高可用

Config Server注册到注册中心上的场景

这种情况最简单,启动多个Config Server即可。 下面我们来验证下

配置中心再启动另外一个端口

可以看到我们启动了两个进程

去Eureka Server上看也可以。

然后重启下artisan-order微服务,观察日志 中的 server url

Multiple Config Server Urls found listed.
Fetching config from server at : http://localhost:9898/
Located environment: name=artisan-order, profiles=[dev], label=null, version=06a183a4820a618cc1dc53b33d77e22106a9c4e4, state=null
Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/yangshangwei/spring-cloud-config-center/artisan-order-dev.yml'}, MapPropertySource {name='https://github.com/yangshangwei/spring-cloud-config-center/artisan-order.yml'}]}

再次重启下 ,或者多重启几次

Multiple Config Server Urls found listed.
Fetching config from server at : http://localhost:9999/
Located environment: name=artisan-order, profiles=[dev], label=null, version=06a183a4820a618cc1dc53b33d77e22106a9c4e4, state=null
Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/yangshangwei/spring-cloud-config-center/artisan-order-dev.yml'}, MapPropertySource {name='https://github.com/yangshangwei/spring-cloud-config-center/artisan-order.yml'}]}

停掉9898节点 ,再次重启下artisan-order服务 ,观察日志


Config Server未注册到注册中心上的场景

借助负载均衡设备即可 ,比如 Nginx , F5,A10等等


修改Eureka Server的默认端口8761的情况

如果我们不使用Eureka Server默认的端口呢? 比如我们使用8762

先把Eureka Server的端口改为8762

其他eureka client客户端将 defaultZone修改为 8762端口对应的url .


Step1. 将Eureka的配置下沉到客户端上

改完默认的端口,我们启动Eureka Server , artisan-config , 发现都可以起来

访问: http://localhost:8762/

紧接着我们启动 artisan-order ,报错了。。。。

2019-04-08 14:47:29.463 ERROR 30340 --- [  restartedMain] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution errorcom.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connectat com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]........
Caused by: java.net.ConnectException: Connection refused: connectat java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_191]2019-04-08 14:47:29.466  WARN 30340 --- [  restartedMain] c.n.d.s.t.d.RetryableEurekaHttpClient    : Request execution failed with message: java.net.ConnectException: Connection refused: connect
2019-04-08 14:47:29.468 ERROR 30340 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_ARTISAN-ORDER/localhost:artisan-order - was unable to refresh its cache! status = Cannot execute request on any known servercom.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known serverat com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.2.jar:1.9.2]....at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.0.3.RELEASE.jar:2.0.3.RELEASE]2019-04-08 14:47:29.474  WARN 30340 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Using default backup registry implementation which does not do anything.
2019-04-08 14:47:29.476  INFO 30340 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Not registering with Eureka server per configuration
2019-04-08 14:47:29.480  INFO 30340 --- [  restartedMain] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1554706049479 with initial instances count: 0
2019-04-08 14:47:29.590  WARN 30340 --- [  restartedMain] lientConfigServiceBootstrapConfiguration : Could not locate configserver via discoveryjava.lang.IllegalStateException: No instances found of configserver (ARTISAN-CONFIG)at org.springframework.cloud.config.client.ConfigServerInstanceProvider.getConfigServerInstances(ConfigServerInstanceProvider.java:25) ~[spring-cloud-config-client-2.0.0.RELEASE.jar:2.0.0.RELEASE]....

一看就知道是Eureka 的问题,我们想到,统一配置中心上artisan-order-dev.yml的Eureka Server的端口还没改,改下再试试吧

访问下 http://localhost:9898/artisan-order-dev.yml 看下效果

再次启动artisan order ,还是报错

访问了配置中心的默认端口8888 ,

这样的话 我们把Eureka的配置

# Eureka
eureka:client:service-url:defaultZone: http://localhost:8762/eureka/

下沉到artisan order 中


意思就是:启动的时候会优先加载bootstrap中的配置,从该配置文件中找到注册中心的地址,然后再注册中心上去找配置中心的service-id .

同时把配置中心artisan-order-dev.yml 中的eureka的配置注释或者删掉。


Step2. 注释掉默认配置文件的配置

再次访问下 http://localhost:9898/artisan-order-dev.yml 看下

怎么又是8761了。。。。

我们来观察下 artisan cofig的日志

当我们按照这种格式访问 http://localhost:9898/artisan-order-dev.yml 配置文件时,config server同时也会把默认配置文件的artisan-order中的配置合并后返回。

那看下 artisan-order.yml的配置看下吧

把artisan-order.yml的配置注释掉吧

再次访问 http://localhost:9898/artisan-order-dev.yml

可以发现已经没有eureka的相关信息了。

再次启动artisan order ,启动成功。 查看注册中心 http://localhost:8762/


遗留问题

修改配置自动刷新,还是没有实现,仅仅实现了从远端Git读取配置的功能,下一篇我们来实战下如何通过Spring Cloud Bus自动刷新配置


代码

配置文件远端存储Git: https://github.com/yangshangwei/spring-cloud-config-center
artisan order : https://github.com/yangshangwei/springcloud-o2o/tree/master/artisan_order
artisan config: https://github.com/yangshangwei/springcloud-o2o/tree/master/artisan_config
eureka server : https://github.com/yangshangwei/springcloud-o2o/tree/master/eureka-server

Spring Cloud【Finchley】实战-05配置中心的搭建(配合使用Eureka)和Config Server高可用相关推荐

  1. Spring Cloud Alibaba Nacos 分布式配置中心

    文章目录 1 摘要 2 核心 Maven 依赖 3 核心代码 3.1 bootstrap 配置文件 3.2 application 配置文件 3.3 配置测试类 - Controller 层 3.4 ...

  2. Spring Boot + Spring Cloud 实现权限管理系统 配置中心(Config、Bus)

    技术背景 如今微服务架构盛行,在分布式系统中,项目日益庞大,子项目日益增多,每个项目都散落着各种配置文件,且随着服务的增加而不断增多.此时,往往某一个基础服务信息变更,都会导致一系列服务的更新和重启, ...

  3. Spring Cloud 采用Consul做配置中心

    -----------------pom.xml依赖,主要是spring-cloud-starter-consul-config <dependency><groupId>or ...

  4. Spring Cloud Alibaba——Nacos服务配置中心

    Nacos服务配置中心 建Module 改Pom 改yml Nacos端操作 写启动类 写controller层 测试 Tips 简单记录下使用Nacos作为服务配置中心,此篇建立在上篇的Nacos服 ...

  5. Spring Cloud 如何选择分布式配置中心

    ##微服务必备的几样武器有了,才能独闯武林, 有哪几样呢? 注册中心(eureka, consul, zk, etcd) 配置中心 (Spring Cloud Config, disconf ) AP ...

  6. Spring Cloud 系列之 Nacos 配置中心

    目录 一.Nacos简介 二.Nacos安装及配置 1.环境准备 2.安装包下载 (1)源码方式 (2)发行包方式 3.启动Nacos服务 4.Nacos数据库配置 (1)MySQL数据源 (2)初始 ...

  7. Spring Cloud(15)——配置中心

    Spring Cloud Config Spring Cloud Config提供分布式配置功能,它包含Server和Client两部分.Server负责提供统一的配置信息,Client负责从Serv ...

  8. Spring Cloud【Finchley】实战-06使用/actuator/bus-refresh端点手动刷新配置 + 使用Spring Cloud Bus自动更新配置

    文章目录 概述 特别注意版本信息 使用@RefreshScope + /actuator/bus-refresh端点手动刷新配置 Step1. 添加依赖 Step2. 配置RabbitMQ信息 Ste ...

  9. 【Spring Cloud Alibaba 实战 | 总结篇】Spring Cloud Gateway + Spring Security OAuth2 + JWT 实现微服务统一认证授权和鉴权

    一. 前言 hi,大家好~ 好久没更文了,期间主要致力于项目的功能升级和问题修复中,经过一年时间这里只贴出关键部分代码的打磨,[有来]终于迎来v2.0版本,相较于v1.x版本主要完善了OAuth2认证 ...

  10. apollo 配置中心_Spring Cloud 系列之 Apollo 配置中心(三)

    本篇文章为系列文章,未读前几集的同学请猛戳这里: 哈喽沃德先生:Spring Cloud 系列之 Apollo 配置中心(一)​zhuanlan.zhihu.com 哈喽沃德先生:Spring Clo ...

最新文章

  1. 台式您想使用系统还原计算机吗,联想台式机一键恢复,小编教你怎么使用联想电脑一键恢复...
  2. mysql删除数据太多卡死解决办法
  3. 计算机数据库系统考研复试面试题,2016年山西财经大学081203计算机应用技术871数据库系统概论复试笔试最后押题五套卷...
  4. python的8种标准数据类型有哪些_Python的八种数据类型
  5. snipaste安装和使用_snipaste替代品 amp; linux截图解决方案-截图、贴图工具Flameshot...
  6. jQuery之animate自定义动画
  7. Windows10系统的MSDN下载和通过U盘进行安装的步骤(亲测有效)
  8. 软考对程序员的作用,对程序员有多大意义?
  9. eclipse使用配置教程
  10. GIF动态图片分解,多帧动态图分解成多张静态图片
  11. js:常用的3种弹出提示框(alert、confirm、prompt)
  12. Unity3D教程笔记——unity初始02
  13. Hadoop集群之开启kerberos安全认证
  14. python将时间戳转换成北京时间、标准格式
  15. 【清单】边角知识清单
  16. 微信封号被限制的几种原因及解决方法
  17. Centos 7.5 1804安装绿联PL2303串口驱动
  18. springboot 使用mybatis-plus 配置乐观锁。
  19. web页面之弹出窗口
  20. FineReport报表工具最新版本细则

热门文章

  1. 蓦然回首,Java 已经 24 岁了!
  2. 一款开源短视频去水印程序,大爱!
  3. 网页倒计时制作(js)
  4. c艹入门->入土 ( 二 )
  5. 谈谈网络通信中的 ACK、NACK 和 REX
  6. 大龄程序员都去哪了?
  7. 【CSDN雇主招聘】深信服科技带着高薪岗位JD和公司周边来啦
  8. mysql.sock 是什么_mysql.sock到底存了什么信息?
  9. 大唐凌烟阁二十四功臣
  10. DataSet-如何优雅使用DataSet,看完此篇文章完全理解C7N/choerodon/猪齿鱼 UI中的DataSet