SpringCloud-Finchley系列10-配置获取Spring Cloud Config搭建

  • 创建项目
  • 配置文件
  • 启动类
  • 服务
  • pom文件
  • 验证

一.创建项目
选取Spring Cloud Config作为获取配置组件
Spring Cloud Bus(rabbitmq)作为消息总线组件

创建maven项目flow-config-client01(8751)、flow-config-client02(8752)作为子模块

二.配置文件
1.application-config-client01-dev.yml, 内容如下:

# rabbitmq_config\bus_config
spring:
  rabbitmq:
    host: *.*.*.*
    port: 5672
    username: ***
    password: ***
  cloud:
    bus:
      enabled: true
      trace:
        enabled: true

management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

info:
  company.name: org.flowframework
  app.name: config-client01
  app.desc: springcloud-config-client01
  author: robin

2.bootstrap.yml, 内容如下:

server:
  port: 8751

eureka:
  instance:
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      # 单个配置
#      defaultZone: http://localhost:8761/eureka/
      # 集群配置
      defaultZone: http://127.0.0.1:8761/eureka/, http://127.0.0.1:8762/eureka/

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      uri: http://localhost:8741
      discovery:
        enable: true
        service-id: config-server

注意:
git相关配置必须写在bootstrap.yml中

三.启动类
Application.java

@EnableEurekaClient
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

四.服务
1.RefreshConfig.java

@Component
@RefreshScope
public class RefreshConfig {

    @Value("${data.env}")
    private String env;
    @Value("${data.user.username}")
    private String username;
    @Value("${data.user.password}")
    private String password;

    public Map<String, Object> getConfig() {
        Map<String, Object> paramMap = new HashMap<>(3);
        paramMap.put("env", env);
        paramMap.put("username", username);
        paramMap.put("password", password);
        return paramMap;
    }

    public String getEnv() {
        return env;
    }

    public void setEnv(String env) {
        this.env = env;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

2.ConfigController.java

@RestController
public class ConfigController {

    @Autowired
    RefreshConfig refreshConfig;

    @RequestMapping("/config")
    public Object getConfig() {
        return refreshConfig.getConfig();
    }
}

五.pom文件

<?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
                                   http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.flowframework</groupId>
    <artifactId>flow-config-client01</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.flowframework</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <!-- eureka-client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!-- config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- bus -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>true</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>flow-config-client01</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

六.验证
http://localhost:8751/config
{“env”:“config-client-dev”,“password”:“520025”,“username”:“client-user”}

更改配置文件参数后, postman执行(post请求)
单个刷新
http://localhost:8751/actuator/refresh
bus刷新
http://localhost:8751/actuator/bus-refresh

http://localhost:8761/
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值