SpringBoot集成Consul

导入maven坐标

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

编写代码

package com.edu.www;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
@EnableDiscoveryClient
public class DemoConsulApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoConsulApplication.class, args);
        String username = context.getEnvironment().getProperty("username");
        System.out.println(username);
    }

}

Consul编写配置文件

配置文件根目录

在这里插入图片描述

各环境配置文件夹

在这里插入图片描述

各环境配置文件

在这里插入图片描述

编写bootstrap.yml配置文件

server:
  port: 6666


spring:
  application:
    name: consu-config-demo # 应用名称
#  profiles:
#    active: dev # 指定环境,默认加载default环境
  cloud:
    consul:
      # Consul 服务器地址
      host: localhost
      port: 8500
      # 配置中心相关配置
      config:
        # 是否启用配置中心,默认值 true 开启
        enabled: true
        # 设置配置的基本文件夹,默认值config 可以理解为配置文件所在的最外层文件夹
        prefix: config
        # 设置应用的文件夹名称,默认值application 一般建议设置为微服务应用名称
        default-context: orderService
        # 配置环境分隔符,默认值","和default-context配置项搭配
        # 例如应用orderService 分别有环境 default, dev, test, prod
        # 只需 config 文件下创建 orderService, orderService-dev, orderService-test, orderService-prod文件夹
        profile-separator: '-'
        # 指定配置格式为yamL
        format: yaml
        # Consul Key/Values 中的Key, Value 对应整个配置文件
        data-key: orderServiceConfig
        # 以上配置可以理解为:加载 config/orderService/ 文件夹下Key 为orderServiceConfig 的Value 对应的配置
        watch:
          # 是否开启自动刷新,默认值true 开启
          enabled: true
          # 刷新频率,单位:毫秒,默认值1000
          delay: 1000
      discovery:
        register: true                             # 是否需要注册
        instance-id: ${spring.application.name}-01 # 注册实例id(必须唯一)
        service-name: ${spring.application.name}   # 服务名称
        port: ${server.port}                       # 服务端口(默认就是当前服务端口)
        prefer-ip-address: true                    # 是否使用ip地址注册(开启后可以在web界面看到实例ip)
        ip-address: ${spring.cloud.client.ip-address} # 服务请求ip(默认就是当前服务ip)

编写properties文件配置类

package com.edu.www.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "mysql")
@Data
public class MysqlConfigProperties {
    private String host;
}

测试代码

package com.edu.www.controller;

import com.edu.www.config.MysqlConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigController {
    @Autowired
    MysqlConfigProperties configurationProperties;


    @GetMapping("config")
    public MysqlConfigProperties config() {
        return configurationProperties;
    }

    @GetMapping("/actuator/health")
    public String health() {
        return "ok";
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值