上一篇文章,讲了SpringCloudConfig 集成Git仓库,这一篇我们讲一下SpringCloudConfig 配和 Eureka 注册中心一起使用

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client,业界也有些知名的同类开源产品,比如百度的disconf

相比较同类产品,SpringCloudConfig最大的优势是和Spring无缝集成,支持Spring里面Environment和PropertySource的接口,对于已有的pring应用程序的迁移成本非常低,在配置获取的接口上是完全一致,结合SpringBoot可使你的项目有更加统一的标准(包括依赖版本和约束规范),避免了应为集成不同开软件源造成的依赖版本冲突。

准备工作

我们先拿之前的代码为基础,进行下面的操作

Spring Cloud(四) 服务提供者 Eureka + 服务消费者 Feign

http://www.ymq.io/2017/12/06/spring-cloud-feign/

Eureka Service

导入第四篇文章中的项目:作为服务注册中心

spring-cloud-eureka-service

Eureka Provider

导入第四篇文章中的项目:作为服务的提供者

spring-cloud-eureka-provider-1
spring-cloud-eureka-provider-2
spring-cloud-eureka-provider-3

Eureka Consumer

导入第四篇文章中的项目:作为服务的消费者

spring-cloud-feign-consumer

服务端配置

Config Server

复制上一篇的项目 spring-cloud-config-server,添加 eureka依赖

https://github.com/souyunku/spring-cloud-examples/tree/master/spring-cloud-config/

添加依赖

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

开启服务注册

在程序的启动类 ConfigServerApplication.java 通过 @EnableEurekaClient 开启 Eureka 提供者服务

package io.ymq.example.config.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}

修改配置

修改配置文件 application.properties ,添加 eureka 注册中心地址 http://localhost:8761/eureka/

spring.application.name=config-server
server.port=8888
spring.cloud.config.label=master
spring.cloud.config.server.git.uri=https://github.com/souyunku/spring-cloud-config.git
spring.cloud.config.server.git.search-paths=spring-cloud-config
#spring.cloud.config.server.git.username=your username
#spring.cloud.config.server.git.password=your password
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.server.git.uri:配置git仓库地址
spring.cloud.config.server.git.searchPaths:配置仓库路径
spring.cloud.config.label:配置仓库的分支
spring.cloud.config.server.git.username:访问git仓库的用户名
spring.cloud.config.server.git.password:访问git仓库的用户密码 eureka.client.serviceUrl.defaultZone:eureka注册中心地址

Git仓库如果是私有仓库需要填写用户名密码,示例是公开仓库,所以不配置密码。

远程Git仓库

spring-cloud-config 文件夹下有 application-dev.properties,application-test.properties 三个文件,内容依次是:content=hello dev,content=hello test,content=hello pre

测试服务

启动程序 ConfigServerApplication 类

访问 Config Server 服务:http://localhost:8888/springCloudConfig/dev/master

{
"name": "springCloudConfig",
"profiles": [
"dev"
],
"label": "master",
"version": "b6fbc2f77d1ead41d5668450e2601a03195eaf16",
"state": null,
"propertySources": [
{
"name": "https://github.com/souyunku/spring-cloud-config.git/application-dev.properties",
"source": {
"content": "hello dev"
}
}
]
}

证明配置服务中心可以从远程程序获取配置信息。

http请求地址和资源文件映射如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

客户端端配置

config Client Eureka

修改已经导入的,第四篇文章中的项目:配置客户端的一些配置

spring-cloud-eureka-provider-1
spring-cloud-eureka-provider-2
spring-cloud-eureka-provider-3

添加依赖

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

开启服务注册

在程序的启动类 EurekaProviderApplication ,通过 @Value 获取服务端的 content 值的内容

package io.ymq.example.eureka.provider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaProviderApplication {
@Value("${content}")
String content;
@Value("${server.port}")
String port;
@RequestMapping("/")
public String home() {
return "Hello world ,port:" + port+",content="+content;
}
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApplication.class, args);
}
}

添加配置

修改配置文件 application.properties 添加 Eureka 注册中心,配置从springCloudConfig 配置中心读取配置,指定springCloudConfigService 服务名称

spring.application.name=eureka-provider
server.port=8081
spring.cloud.config.label=master
spring.cloud.config.profile=dev
#spring.cloud.config.uri=http://localhost:8888/
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.label 指明远程仓库的分支
spring.cloud.config.profile
dev开发环境配置文件
test测试环境
pro正式环境
#spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址(注释掉) spring.cloud.config.discovery.enabled=true 是从配置中心读取文件。
spring.cloud.config.discovery.serviceId=config-server 配置中心的servieId,服务名称,通过服务名称去 Eureka注册中心找服务

测试服务

依次启动项目:

spring-cloud-eureka-service
spring-cloud-config-server
spring-cloud-eureka-provider-1
spring-cloud-eureka-provider-2
spring-cloud-eureka-provider-3
spring-cloud-feign-consumer

启动该工程后,访问服务注册中心,查看服务是否都已注册成功:http://localhost:8761/

在浏览器访问http://127.0.0.1:9000/hello

修改了Git仓库的配置后,需要重启服务,才可以得到最新的配置,下一篇讲怎么解决配置的更新

Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务的更多相关文章

  1. springCloud学习-高可用的分布式配置中心(Spring Cloud Config)

    1.简介 高可用的分布式配置中心,即将配置中心做成一个微服务,将其集群化,从而达到高可用.config-server和config-client向eureka-server注册,且将config-se ...

  2. Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config

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

  3. 【SpringCloud】第七篇: 高可用的分布式配置中心(Spring Cloud Config)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  4. 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)

    上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如 ...

  5. SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)

    上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如 ...

  6. SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)

    版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...

  7. Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ)

    详见:https://www.w3cschool.cn/spring_cloud/spring_cloud-jl8a2ixp.html 上一篇文章,留了一个悬念,Config Client 实现配置的 ...

  8. SpringCloud学习(七)高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)

    上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用 准备工作 ...

  9. Spring Cloud(十)高可用的分布式配置中心 Spring Cloud Config 中使用 Refresh

    上一篇文章讲了SpringCloudConfig 集成Git仓库,配和 Eureka 注册中心一起使用,但是我们会发现,修改了Git仓库的配置后,需要重启服务,才可以得到最新的配置,这一篇我们尝试使用 ...

随机推荐

  1. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_01-页面静态化需求分析

    上半部分就是静态化 业务流程如下: 1.获取模型数据 2.制作模板 3.对页面进行静态化 4.将静态化生成的html页面存放文件系统中 5.将存放在文件系统的html文件发布到服务器

  2. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_14-异常处理-异常处理的问题分析

    这块代码没有异常处理.如果在Service出现了异常代码,在哪里捕获?要么在Servive内捕获,要么在调用service的地方也就是controller内捕获 每个调用service的地方都要去捕获 ...

  3. Spring事务管理2----编程式事务管理

    编程式事务管理 通过使用将Spring框架提供的TransactionTemplate模板注入到业务层来进行事务管理,这样对业务层原来的代码修改过多.不利于项目的后期维护. 以下是声明式事务管理的具体 ...

  4. 共享打印机,错误0x80070035和错误0x00000709的解决办法

    这两个错误可以说是共享打印机里经常出现的错误了. 首先,要确认客户机可以ping通打印机的直连电脑的IP,如果这一步不通,那别玩了. 其次,很多人会忽略的一点儿,两个电脑的dns最好设置为相同的,经测 ...

  5. PAT 甲级 1027 Colors in Mars (20 分)(简单,进制转换)

    1027 Colors in Mars (20 分)   People in Mars represent the colors in their computers in a similar way ...

  6. 详解用python实现简单的遗传算法

    详解用python实现简单的遗传算法 今天整理之前写的代码,发现在做数模期间写的用python实现的遗传算法,感觉还是挺有意思的,就拿出来分享一下. 首先遗传算法是一种优化算法,通过模拟基因的优胜劣汰 ...

  7. 使用linux flock文件锁实现任务锁定避免计划任务程序冲突

    格式:flock [-sxun][-w #] fd#flock [-sxon][-w #] file [-c] command选项-s, --shared: 获得一个共享锁 -x, --exclusi ...

  8. CockroachDB学习笔记——[译]Cgo的成本与复杂性

    原文链接:https://www.cockroachlabs.com/blog/the-cost-and-complexity-of-cgo/ 原作者:Tobias Schottdorf 原文日期:D ...

  9. ubuntu 18.04安装ftp服务器

    首先安装vsftpd: sudo apt-get install vsftpd 可以通过命令vsftpd -version查看vsftpd版本. 为ftp服务器新建一个用户(比如我这里新建一个用户名和 ...

  10. QFramework 使用指南 2020(五):脚本生成(3)生成 Prefab

    在上一篇,我们简单介绍了 ViewController 的嵌套实现. 在这一篇,我们学习 ViewController 脚本生成的最后一个功能,即:生成 Prefab. 实际上这个功能非常简单. 我们 ...