SpringCloud Config Eureka注册中心动态读取Config配置

目的:通过访问Config微服务工程,动态获取配置文件。

事例:eureka工程访问config工程,获取eureka的动态配置文件。

优点:不需要通过注册中心去获取,直接访问Config工程获取。



一、搭建Config微服务工程

POM文件
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</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-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
</dependencies>
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class PurchasecloudSrmConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(PurchasecloudSrmConfigApplication.class, args);
    }
}


appliaction.yml配置文件**
server:
  port: 3344

spring:
  application:
    name: purchasecloud-srm-config
  cloud:
    config:
      server:
        git:
          username: 
          password: 
          uri: 
management:
  endpoints:
    web:
      exposure:
        include: "*"
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
Username:账号
Password:密码
Uri:仓库连接


二、码云建立仓库

建立仓库

在这里插入图片描述

在这里插入图片描述

运行config项目,访问码云上的资源文件

测试:运行config微服务工程,访问码云上的连接。

访问http://127.0.0.1:3344/purchasecloud-srm-eureka.yml,出现配置文件内容信息即为成功



三、eureka注册中心动态读取配置文件

将appliaction.yml更名成bootstrap.yml
Yml配置
spring:
  cloud:
    config:
      name: purchasecloud-srm-eureka #需要从github或码云上读取的资源名称,即获取哪个配置文件,注意没有yml后缀名
      profile: dev   #本次访问的配置项,决定了当前模块使用配置文件中的哪个开发环境,与Github仓库里的spring.profiles属性对应
      label: master
      uri: http://127.0.0.1:3344  #本微服务启动后先去找3344号Configure配置中心模块,通过          SpringCloudConfig获取GitHub的仓库地址
POM文件
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
    <version>2.1.3.RELEASE</version>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值