springCloud config配置中心,暂时没有实现自动刷新)(入门系列)

本文介绍了如何使用SpringCloud Config搭建配置中心。首先,在码云上创建配置文件仓库,然后创建并配置config-service,使其注册到Eureka注册中心。接着,配置客户端,添加相关依赖和配置文件。最后,验证配置中心能否成功读取并应用配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springcloud 版本  Hoxton.RELEASE

springboot 版本 2.2.2.RELEASE

一、

在码云上创建一个仓库 存放配置文件

 

 

二、创建config-service 并将其注册到注册中心

启动类添加 注解

@EnableConfigServer // 注解来开启config 的server功能
@SpringBootApplication
@EnableEurekaClient

pom添加 

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--注册中心客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

application.yml配置文件

server:
  port: 8201
spring:
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri: #上面所创建仓库的路径
          search-paths: '{application}'   #占位符  应用名
          # 每次都从仓库拉取文件,防止本地脏读
          force-pull: true
          #username:  公共开源可不要账号密码
          #password:
          default-label: master #git分支
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8100/eureka/
  instance:
    prefer-ip-address: true

三、查看是否服务是否注册到注册中心

四、客户端配置

添加 

<!--配置中心客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

配置文件为

spring:
  devtools:
    add-properties: false
  application:
    name: ELITE
  profiles:
    active: test
  cloud:
    config:
        profile: ${spring.profiles.active}  # 指定配置文件的环境
        name: ${spring.application.name}    #应用名称
        discovery:
          service-id: config-service
          enabled: true  #是否从eureka上找服务
          #uri: http://127.0.0.1:8201 #不通过注册中心  直接通过config ip地址访问
#注册中心配置
eureka:
  client:
    service-url:
      defaultZone: http://172.18.0.192:8100/eureka/
      instance:
        prefer-ip-address: true

五、查看结果

是我配置文件里指定的服务名

正确

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值