Spring 项目 注册Config Client 到 Eureka 拉取配置中心配置文件

简述:首先默认你的 Config Server 服务已经在 Eureka 中注册并运行,这里对实现 Config Server 不做说明。

第一步:添加config client 和 eureka client 依赖

 		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- bus 是用于热刷新的 具体使用方式请自行百度 这里不做说明-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

第二步:添加applicanion.yml 配置文件

server:
  port: 8080

spring:
  config:
    import: optional:configserver:http://username:password@localhost:8080  #config 服务器的访问地址和访问账号、密码(密码账号没有可以不加)

  cloud:
    config:
      label: master	#访问的git分支
      name: profile,datasource,redis,rabbitmq,elasticsearch #访问的文件名,可自行定义
      profile: ${spring.profiles.active} #获取的运行环境(文件后缀)
      discovery:			#此属性用于开始通过eureka查找config服务器来获取配置,如果不想通过 eureka 去拿配置可以不用配置下面的 discovery.enabled 和 discovery.service-id
        enabled: true		#开启eureka的服务发现
        service-id: CONFIG-SERVER	#注册在eureka上的config server服务id

    bus:	# id的创建规则请自行百度
      id: ${vcap.application.name:${spring.application.name:application}}:${vcap.application.instance_index:${spring.cloud.config.profile:${local.server.port:${server.port:0}}}}:${vcap.application.instance_id:${random.value}}

#将 config client 注册到eureka, 如果不通过 eureka 去找config server的时候,可以不用将 client 注册到 eureka ,也就不需要下面的 eureka 配置
eureka:
  client:
    serviceUrl:
      #注册地址
      defaultZone: http://username:password@localhost:8080/eureka/
  instance:
    #开启IP优先
    prefer-ip-address: true


--- #不同的运行环境
spring:
  application:
    name: @project.artifactId@
  profiles:
    active: dev
---
spring:
  application:
    name: @project.artifactId@
  profiles:
    active: pro

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Config Server注册Eureka注册中心的步骤如下: 1. 首先需要在pom.xml文件中添加Eureka Client的依赖,如下所示: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.1.RELEASE</version> </dependency> ``` 2. 在应用程序的配置文件中添加以下配置: ```yaml eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ ``` 其中,`defaultZone`指定了Eureka Server的地址。 3. 在Spring Boot应用程序类上添加@EnableEurekaClient注解,以将Config Server注册Eureka Server上: ```java @EnableConfigServer @EnableDiscoveryClient @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } ``` 4. 运行Config Server应用程序,查看Eureka Server的管理页面(http://localhost:8761)上是否已经注册成功。 5. 客户端应用程序可以通过以下方式从Config Server获取配置: ```yaml spring: application: name: client-service cloud: config: uri: http://localhost:8888 fail-fast: true retry: maxAttempts: 20 multiplier: 1.5 maxInterval: 5000 label: master ``` 其中,`cloud.config.uri`指定了Config Server的地址,`spring.application.name`指定了客户端应用程序的名称,`label`指定了Git仓库的分支名称。 6. 运行客户端应用程序,即可从Config Server获取配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值