上一篇文章,讲了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. 绿色版plsql安装和安装包

    转: 绿色版plsql安装和安装包 2018年08月07日 19:53:15 YoungLee16 阅读数 4890   最近在使用数据库的时候,或多或少的会出现一些问题,下面总结下如何安装ORACL ...

  2. mac下的夜神模拟器链接vscode

    1.找到夜神模拟器,点击右键,查看包内容,找到文件夹下面的macos在点击右键打开终端.输入: adb connect 127.0.0.1:62001 dart和flutter交流群:45289287 ...

  3. haproxy报错解决

    .有一次访问出现 错误 http://192.168.0.200:10080 haproxy service unavailable no server is avaible to handle th ...

  4. 解决报错Fatal error in launcher

    换电脑重装python,打算安装第三方库的时候出现错误: Fatal error in launcher 然而在网上搜到的大多数是解决 —— Fatal error in launcher: Unab ...

  5. Mysql按照设计顺序获得某个表的字段名称,字段类型,字段描述!!!!!

    编写sql语句 select column_name,data_type ,column_comment from information_schema.columns where table_nam ...

  6. Java工程师学习指南第5部分:Java网络编程与NIO

    本文整理了微信公众号[Java技术江湖]发表和转载过的Java网络编程相关优质文章,想看到更多Java技术文章,就赶紧关注本公众号吧. 深度解读 Tomcat 中的 NIO 模型 [Java基本功]浅 ...

  7. [非原创]Office 2019 增强版 批处理激活 亲测成功

    忘了原创网址了,在 https://www.52pojie.cn/ 上看到的,这里我备忘一下,希望知道原创网址的朋友告诉我一下,谢谢! 将下面批处理脚本存成.bat文件后,以管理员方式运行: @ech ...

  8. Redis安装与配置问题

    Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  9. centOS 安装 npm

    下载 cd /usr/local/node wget https://npm.taobao.org/mirrors/node/v10.14.1/node-v10.14.1-linux-x64.tar. ...

  10. centos7服务搭建常用服务配置之一:SSH

    目录 1 SSH服务协议 1.1 ssh服务协议说明 1.2 ssh服务工作机制 1.3 ssh加密技术说明 1.3.1 ssh实现安全链接建立,利用要是和锁头 1.3.2 ssh加密算法 1.4 s ...