springcloud系列28——Spring Cloud Config与Eureka配合使用

前言

在之前说到的Spring Cloud Config Client程序中都是通过spring.cloud.config.uri 指定Config Server的地址。这种方式Spring Cloud称之为“Config First Bootstrap”。除此之外,还支持一种服务发现的方式,Spring Cloud称之为“Discovery First Bootstrap”。本文将说明如何通过Eureka发现Config Server的服务。

Spring Cloud Config与Eureka配合使用

1.Config Server改造

既然是通过Eureka服务发现来帮助Config Client定位到Config Server,那么必须先将Config Server注册到Eureka上。

a.添加Eureka的依赖

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

b.在配置文件指定eureka的serviceUri

spring:
  application:
    name: cloud-config-server
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true

注意需要指定Config Server的serviceId(spring.application.name),后面客户端从Eureka是根据serviceId查找Config Server的。

3.在Spring Boot主类增加@EnableEurekaClient

2.Config Client改造

a.添加Eureka的依赖

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

b.配置文件增加Config Server的服务发现配置

spring:
  application:
    name: cloud-config-client
  cloud:
    config:
      name: cloud-config
      profile: ${config.profile:dev}
      discovery:
        enabled: true
        service-id: cloud-config-server
      username: user
      password: password123

上面的配置中,discovery.enabled指定是否启用服务发现,service-id指定服务id。

在上一节中说到有2种方式配置会用户名和密码,但如果使用服务发现的话,就只能使用这种单独指定username和password的形式了。

3.在Spring Boot主类增加@EnableEurekaClient

然后依次启动Eureka Server,Config Server和Config Client即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值