java 1.8
idea 2018.x
spring boot 2.0.5
这里使用 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
<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.0modelVersion>
<groupId>com.foxwhogroupId>
<artifactId>server-demoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>server-demoname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.5.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<spring-cloud.version>Finchley.SR1spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-consul-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
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);
}
}
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/