SpringCloud Config 配置

spring cloud config 大家都知道是配置中心,是用于根据不同环境加载不同配置文件,话不多说,直接上干货!

以下是基于springboot V2.4.2版本,springcloud (V3.0.0)版本,注册中心使用consul

spring config server

  1. config server 的pom文件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tongxue</groupId>
<artifactId>spring-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-config-server</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
​
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
</dependencies>
 
  1. 启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class SpringConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringConfigServerApplication.class, args);
    }
}
  1. application.properties

server.port=9999
spring.application.name=config-server
​
# 注册到consul中心
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.prefer-ip-address=false
#注册到consul的服务名称
spring.cloud.consul.discovery.service-name=${spring.application.name}
​
#配置文件地址
spring.cloud.config.server.git.uri=https://github.com/leozhang136/spring-config.git

spring config client

  1. config client 配置pom文件

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tongxue</groupId>
<artifactId>spring-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-config-client</name>
<description>Demo project for Spring Boot</description>
​
<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
​
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    <!--<dependency>-->
    <!--<groupId>org.springframework.cloud</groupId>-->
    <!--<artifactId>spring-cloud-starter-config</artifactId>-->
    <!--</dependency>-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <!--actuator自动装载-->
    <!--<dependency>-->
    <!--<groupId>org.springframework.boot</groupId>-->
    <!--<artifactId>spring-boot-starter-actuator</artifactId>-->
    <!--</dependency>-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
  1. 启动类

@Configuration
@SpringBootApplication
@EnableDiscoveryClient
public class SpringConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringConfigClientApplication.class, args);
    }
}
  1. 测试读取配置参数

@RestController
public class UserController {
    @Value("${user.userName}")
    private String userName;
    @Value("${user.sex}")
    private String sex;
    @Value("${user.age}")
    private Integer age;
​
    @RequestMapping(value = "/user")
    public String getUserInfo(){
        return "userName:"+userName+" sex:"+sex+" age:"+age;
    }
}
  1. application.properties

server.port=8088
spring.application.name=config-client
spring.profiles.active=test
# consul
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.prefer-ip-address=false
#注册到consul的服务名称
spring.cloud.consul.discovery.service-name=${spring.application.name}
​
# 通过uri进行拉取
#spring.cloud.bootstrap.enabled=true
#spring.config.import=optional:configserver:http://localhost:9999
#spring.cloud.config.fail-fast=true
##spring.cloud.config.name=study
#spring.cloud.config.profile=${spring.profiles.active}
#spring.cloud.config.label=master
​
# 通过服务进行拉取
spring.cloud.config.discovery.enabled=true
spring.config.import=optional:configserver:
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.fail-fast=true
spring.cloud.config.profile=${spring.profiles.active}
spring.cloud.config.label=master

看到Fetching config from server at : http://LAPTOP-TE97R99D:9999/ 表示去拉取远程配置信息

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值