springCloud08—分布式配置中心:Spring Cloud Config

创建一个新的springBoot web 应用命名为config-client,添加以下依赖

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.5.4

<java.version>11</java.version>

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-starter-config

3.0.4

org.springframework.cloud

spring-cloud-starter-bootstrap

3.0.3

org.springframework.cloud

spring-cloud-dependencies

2020.0.2

pom

import

添加bootstrap.properties

spring.application.name=didispace

注册服务的时候使用服务的ip地址

eureka.instance.prefer-ip-address=true

eureka.client.service-url.defaultZone=http://peer2:1112/eureka/

#指定的环境

spring.cloud.config.profile=dev

#指定分支,当使用git的时候,默认是master

spring.cloud.config.label=master

#Config server的uri

spring.cloud.config.uri=http://localhost:7001/

配置项目启动端口号

server.port=7002

注意:spring.application.name要和你git仓库中的配置前缀相对应

在这里插入图片描述

这里需要格外注意,上面这些属性必须配置在 bootstrap.properties 中,这样config-server中的配置信息才能被正确加载

修改主配置类

@SpringBootApplication

@EnableDiscoveryClient

public class ConfigClientApplication {

public static void main(String[] args) {

SpringApplication.run(ConfigClientApplication.class, args);

}

}

创建一个RESTful接口来返回配置中心的from 属性

通过@value ("${from} ")绑定配置服务中配置的from属性,具体实现如下:

//@RefreshScope是为了可以动态刷新这个Controller的Bean

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值