SpringBoot2 Spring Cloud consul 分布式配置中心使用教程

版权声明:原创文章欢迎转载,不过要记得加出处哦 https://blog.csdn.net/wljk506/article/details/82748660

环境说明

java 1.8
idea 2018.x
spring boot 2.0.5

consul 安装与启动

这里使用 docker
https://github.com/foxiswho/docker-nginx-redis-mysql-rocketmq-rabbitmq-zookeeper
下载它.

docker-compose.yml 把不需要用到的给注释掉,不 注释掉也不影响
执行如下,

cd docker

docker-compose up

foxwho.风

手册

Spring Cloud consul 中文手册: https://springcloud.cc/spring-cloud-consul.html

创建测试案例

浏览器访问

http://localhost:8500/ui/dc1/kv

点击create按钮,创建

config/application,dev/data

内容

config:
	title: 这是基础,你知道:后面还有一个空格码

如图
在这里插入图片描述
依葫芦画瓢,添加的参数如下

config/application/data
#内容是>>>
config:
	title: 这是所有应用默认配置
config/server-demo,dev/data
#内容是>>>
config:
	title: 这是dev开发版demo
config/server-demo/data	
#内容是>>>
config:
	title: 这是正式版demo

idea 创建SpringBoot2 Spring Cloud consul 案例

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

pom.xml 完整配置如下

<?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>com.foxwho</groupId>
    <artifactId>server-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>server-demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</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-starter-consul-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

入口如下

package com.foxwho.consul.demo001;

import com.foxwho.consul.demo001.model.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigurationProperties({Config.class})//指定配置类
public class ConsulDemo001Application {

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

Config 类

package com.foxwho.consul.demo001.model;

import org.springframework.boot.context.properties.ConfigurationProperties;

//title要与consul上面的config:下 title写的一样,consul上面可以写多级目录 例如: config: title: 这是标题
//对应bootstrap.yml 中的 config
@ConfigurationProperties(prefix = "config")
public class Config {
    private String title; //name要与consul上面的title写的一样

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "title : ["+title+"] ";
    }
}

控制器

package com.foxwho.consul.demo001.controller;

import com.foxwho.consul.demo001.model.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @Autowired
    private Config config;

    @RequestMapping(value = "/getConfig")
    public String getConfig() {
        return config.toString();
    }

    @RequestMapping(value = "/getTitle")
    public String getTitle() {
        return config.getTitle();
    }
}

浏览器效果

http://localhost:8010/getTitle

在这里插入图片描述

源码

https://github.com/foxiswho/spring-boot2-consul-demo

参考
https://blog.csdn.net/expect521/article/details/77151094
https://blog.csdn.net/xiaonuo717/article/details/73289178/

阅读更多

扫码向博主提问

风.foxwho

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • php
  • java
  • go
  • 架构
  • more
去开通我的Chat快问
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页