前面两篇介绍了Spring Cloud Config服务端客户端的简单配置,本篇介绍Spring Cloud Config与Eureka配合使用

前言

默认情况下,配置客户端启动时,都是通过配置属性 spring.cloud.config.uri 绑定到配置服务器,并使用远程属性初始化 Spring Environment。这样做的最终结果是所有想要使用Config Server的客户端必须在bootstrap.yml中配置 spring.cloud.config.uri (默认是"http://localhost:8888")。

如果您正在使用DiscoveryClient实现,可将ConfigServer与Eureka等注册中心联合使用(目前Spring Cloud只支持与Eureka及Consul联合使用,不支持与Zookeeper联合使用)。但是如果配置了 spring.cloud.config.uri ,客户端将无法利用注册。

使用服务发现的坏处是启动时额外的网络往返,以定位服务注册。好处是配置服务器可以更改其坐标,只要发现服务是一个固定点(如项目名称不变)。

准备工作

1、启动Eureka服务器(很简单,这里就不演示了)。启动成功后访问http://localhost:8761,如下图所示:

配置服务器代码示例

在pom文件中增加依赖:

        <dependency>
<!-- 配置中心服务端 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<!-- eureka客户端 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

启动类开启服务发现:

@SpringBootApplication
@EnableConfigServer // 通过@EnableConfigServer注解激活配置服务
@EnableDiscoveryClient // 开启服务发现
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
} }

配置文件,application.yml

server:
port: spring:
application:
name: config-server #应用程序名称
cloud:
config:
server:
git:
uri: https://github.com/xuwenjin/config-repo-xwj #git上配置中心地址 eureka:
client:
serviceUrl:
defaultZone: http://admin:admin@localhost:8761/eureka
instance:
prefer-ip-address: true #当猜测主机名时,服务器的IP地址应该在操作系统报告的主机名中使用

配置客户端代码示例

在pom中增加依赖:

       <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!-- 配置中心客户端 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<!-- 监控管理(如:状态页和健康指标) -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<!-- eureka客户端 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

启动类增加服务发现:

@SpringBootApplication
@EnableDiscoveryClient // 开启服务发现
public class ConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
} }

增加一个Controller,用于测试请求:

@RestController
@RefreshScope
public class IndexController { @Value("${profile}")
private String profile; @RequestMapping("/profile")
public String getProfile() {
return profile;
} }

配置文件,bootstrap.yml

spring:
application:
name: config-client
cloud:
config:
profile: test #对应spring.profiles.active
label: master #分支名。当使用配置服务器是git时,默认是master
username: user #配置服务器的用户名密码,此配置会覆盖uri中的配置
password: password123
discovery:
enabled: true #默认false,设为true表示使用注册中心中的configserver配置,而不是自己配置configserver的uri
service-id: CONFIG-SERVER #指定config server在服务发现中的serviceId,默认为:configserver eureka:
client:
serviceUrl:
defaultZone: http://admin:admin@localhost:8761/eureka
instance:
prefer-ip-address: true #当猜测主机名时,服务器的IP地址应该在操作系统报告的主机名中使用

配置文件,application.yml

server:
port:

从示例代码可以看到,想要将Config Server与注册中心联合使用,只需要在客户端配置 spring.cloud.config.discovery.enabled:true 和 spring.cloud.config.discovery.serviceId 两个配置项即可(serviceId是注册到Eureka中的Application)。

测试工作

1、启动配置服务器,会发现Eureka中增加了该示例

2、启动配置客户端,在日志中可以看到客户端发现了服务器的地址:

3、访问http://localhost:18084/profile,返回配置信息。至此配置完成~

spring cloud config与eureka配合使用的更多相关文章

  1. Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务

    上一篇文章,讲了SpringCloudConfig 集成Git仓库,这一篇我们讲一下SpringCloudConfig 配和 Eureka 注册中心一起使用 在分布式系统中,由于服务数量巨多,为了方便 ...

  2. Spring Cloud Config 配置中心高可用

    详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...

  3. Spring Cloud Config 配置高可用集群

    详细参考:<Sprin Cloud 与 Docker 微服务架构实战>p163-9.10节 spring cloud config 与 eureka 配合使用 我就不写了,请参见本书章节.

  4. 9.Spring Cloud Config统一管理微服务配置

    Spring Cloud Config统一管理微服务配置 9.1. 为什么要统一管理微服务配置 9.2. Spring Cloud Config简介 Spring Cloud Config为分布式系统 ...

  5. Spring Cloud Config 实现配置中心,看这一篇就够了

    Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Clou ...

  6. spring cloud config搭建说明例子(二)-添加eureka

    添加注册eureka 服务端 ConfigServer pom.xml <dependency> <groupId>org.springframework.cloud</ ...

  7. Spring Cloud Config中文文档

    https://springcloud.cc/spring-cloud-config.html 目录 快速开始 客户端使用 Spring Cloud Config服务器 环境库 健康指标 安全 加密和 ...

  8. spring cloud config 入门

    简介 Spring cloud config 分为两部分 server client config-server 配置服务端,服务管理配置信息 config-client 客户端,客户端调用serve ...

  9. Spring Cloud Config

    Spring Cloud Config provides server and client-side support for externalized configuration in a dist ...

随机推荐

  1. Repository 简化实现多条件查询

    Repository 在做查询的时候,如果查询条件多的话,linq查询表达式会写的很复杂,比如: public IQueryable<Student> Get(int id, string ...

  2. 使用PostgreSQL进行中文全文检索

    code[class*="language-"], pre[class*="language-"] { background-color: #fdfdfd; - ...

  3. react中使用阿里Viser图表

    参考demo的codesandbox:https://codesandbox.io/s/kxxxx3w5kv 使用步骤:   1. 安装依赖  viser-react和@antv/data-set 2 ...

  4. [CocoaPods]终端方式加载第三方库

    终端方式集成第三方库 1.打开终端,转到当前工程所在的文件夹. 方式一: [访达]->[服务]->[系统偏好设置] ->勾选[新建位于文件夹位置的终端标签 ]和[新建位于文件夹位置的 ...

  5. LabVIEW(十四):VI属性

    1.VI的属性在项目研究中是很常用的,但是会经常忽略VI的属性设置,建议在编程事常常运用这些属性的设置,这样就可以使你的程序更加完善,易懂性也会提高. 属性快捷键:Ctrl+I. 2.常规 (1).编 ...

  6. 多核CPU配合负载均衡可以这样用,为老板省点钱

    负载均衡作为一个处理高并发,大流量的访问的业务场景,已经几乎是常识性的知识了. 而本文的意义在于需求:由于大流量请求,导致服务无法正常响应,在不增加购买机器成本的场景下,如何提高服务器的业务处理能力? ...

  7. 机器学习基石笔记:09 Linear Regression

    线性回归假设: 代价函数------均方误差: 最小化样本内代价函数: 只有满秩方阵才有逆矩阵. 线性回归算法流程: 线性回归算法是隐式迭代的. 线性回归算法泛化可能的保证: 根据矩阵的迹的性质:tr ...

  8. HttpClient和HttpURLConnection的使用和区别(下)

    转自来自点击打开链接 接着上一篇,我们继续来分析HttpURLConnection的使用,以及两者的共同点和区别. 目录 用法 HttpURLConnection 区别 引用资料 用法 HttpURL ...

  9. 30-socketserver类

    SocketServer模块简化了编写网络服务程序的任务.同时SocketServer模块也是Python标准库中很多服务器框架的基础. socketserver模块可以简化网络服务器的编写,Pyth ...

  10. c++编程之内存的分配

    当我们在进行编程时,特别是使用c++语言进行编程时,需要知道内存有几个内存区可供我们使用,因为c++可以直接操作内存.接下让我们来看看内存中的几大内存区. 1.栈区 栈区(stack)是速度最快的一个 ...