上一篇文章,讲了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. jQuery的html()、text()和val()的使用和区别

    .html():读取和修改一个元素的HTML内容 .text():读取和修改一个元素的文本内容 .val():读取和修改一个表单元素的value字段值

  2. Linux命令之iptables

    从CentOS7开始,系统自带的防火墙更改为firewalld,但同样支持iptables,不过只有iptables命令,如果想要使用iptables服务需要自行安装iptables-server. ...

  3. 删除badi及badi interface

    转自:https://blog.csdn.net/zeewjj/article/details/7963147 Use Function Module OO_INTERFACE_DELETE and ...

  4. Java8 根据对象某个属性值去重

    list中的对象某个属性存在重复时将重复的对象去重 //根据skuAttrValueName值去重 List<SkuValue> uniqueSkuValues = skuValues.s ...

  5. 给mysql创建用户

    给mysql 创建用户过程: 1.进入mysql cmd下 2.输入密码:123 3.选择使用的数据库:use myeshop 4.创建新用户grant usage on *.* to 'cctvse ...

  6. swift 第十一课 结构体定义model类

    结构体是可以作为 model 类使用的不过也要 写下的创建方法 import UIKit/***创建一个model 结构,重写init 方法,结构体的属性不能出现可选类型**/ struct Mode ...

  7. ABC技术落地_成功带动lot物联网行业、金融科技行业、智能人才教育。

    ABC技术:AI:Python神经网络和自然语言处理(NLP):C ++ 机器学习和神经网络:Java自然语言处理.搜索算法.神经网络:Lisp归纳逻辑项目和机器学习.Big Date:R.Pytho ...

  8. Java学习笔记-内部类

    内部类在Android中有着大量的运用 内部类 内部类提供了更好的封装:内部类可以直接访问外部类的私有数据:匿名内部类适合那些只需要使用一次的类.非静态内部类不能拥有静态成员.内部类比外部类可以多使用 ...

  9. Memcache分布式锁

    在分布式缓存的应用中,会遇到多个客户端同时争用的问题.这个时候,需要用到分布式锁,得到锁的客户端才有操作权限 下面通过一个简单例子介绍: 这里引用的是Memcached.ClientLibrary.d ...

  10. 用elasticsearchdump备份恢复数据

    1.安装elastic searchdump mkdir /data/nodejs cd /data/nodejs wget https://nodejs.org/dist/v10.16.2/node ...