轉(zhuǎn)載請(qǐng)標(biāo)明出處:
http://blog.csdn.net/forezp/article/details/70245644
本文出自方志朋的博客
這篇文章主要介紹 spring cloud consul 組件玛荞,它是一個(gè)提供服務(wù)發(fā)現(xiàn)和配置的工具勋眯。consul具有分布式、高可用塞蹭、高擴(kuò)展性讶坯。
一、consul 簡(jiǎn)介
consul 具有以下性質(zhì):
- 服務(wù)發(fā)現(xiàn):consul通過(guò)http 方式注冊(cè)服務(wù)漱办,并且服務(wù)與服務(wù)之間相互感應(yīng)娩井。
- 服務(wù)健康監(jiān)測(cè)
- key/value 存儲(chǔ)
- 多數(shù)據(jù)中心
consul可運(yùn)行在mac windows linux 等機(jī)器上似袁。
二、consul安裝
linux
$ mkdir -p $GOPATH/src/github.com/hashicorp && cd $!
$ git clone https://github.com/hashicorp/consul.git
$ cd consul
$ make bootstrap
$ make bootstrap
windows下安裝:
見(jiàn)consul怎么在windows下安裝
三扬霜、構(gòu)建工程
構(gòu)建一個(gè)consul-miya的springboot工程绒尊,導(dǎo)入依賴pring-cloud-starter-consul-discovery仔粥,其依賴文件:
<?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.forezp</groupId>
<artifactId>consul-miya</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consul-miya</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RELEASE</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>
在其入口文件ConsulMiyaApplication加入注解@EnableDiscoveryClient谭羔,開(kāi)啟服務(wù)發(fā)現(xiàn):
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulMiyaApplication {
@RequestMapping("/hi")
public String home() {
return "hi ,i'm miya";
}
public static void main(String[] args) {
new SpringApplicationBuilder(ConsulMiyaApplication.class).web(true).run(args);
}
}
在其配置文件application.yml指定consul服務(wù)的端口為8500:
spring:
cloud:
consul:
host: localhost
port: 8500
discovery:
healthCheckPath: ${management.contextPath}/health
healthCheckInterval: 15s
instance-id: consul-miya
application:
name: consul-miya
server:
port: 8502
啟動(dòng)工程瘟裸,訪問(wèn)localhost:8500,可以發(fā)現(xiàn)consul-miya被注冊(cè)了话告。
源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter14