Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

7.2. Spring Cloud Consol

7.2.1. Spring Cloud Consul 配置

核心參數

		
配置項						預設值
spring.cloud.consul.enabled	true
spring.cloud.consul.host	localhost
spring.cloud.consul.port	8500		
		
		

服務發現參數

		
配置項															預設值
spring.cloud.consul.discovery.acl-token	 
spring.cloud.consul.discovery.catalog-services-watch-delay		10
spring.cloud.consul.discovery.catalog-services-watch-timeout	2
spring.cloud.consul.discovery.datacenters	 
spring.cloud.consul.discovery.default-query-tag	 
spring.cloud.consul.discovery.default-zone-metadata-name		zone
spring.cloud.consul.discovery.deregister						true
spring.cloud.consul.discovery.enabled							true
spring.cloud.consul.discovery.fail-fast							true
spring.cloud.consul.discovery.health-check-critical-timeout	 
spring.cloud.consul.discovery.health-check-interval				10s
spring.cloud.consul.discovery.health-check-path					/actuator/health
spring.cloud.consul.discovery.health-check-timeout	 
spring.cloud.consul.discovery.health-check-tls-skip-verify	 
spring.cloud.consul.discovery.health-check-url	 
spring.cloud.consul.discovery.heartbeat.enabled					false
spring.cloud.consul.discovery.heartbeat.interval-ratio	 
spring.cloud.consul.discovery.heartbeat.ttl-unit				s
spring.cloud.consul.discovery.heartbeat.ttl-value				30
spring.cloud.consul.discovery.hostname	 
spring.cloud.consul.discovery.instance-group	 
spring.cloud.consul.discovery.instance-id						預設為服務名+環境+連接埠號
spring.cloud.consul.discovery.instance-zone	 
spring.cloud.consul.discovery.ip-address	 
spring.cloud.consul.discovery.lifecycle.enabled					true
spring.cloud.consul.discovery.management-port	 
spring.cloud.consul.discovery.management-suffix					management
spring.cloud.consul.discovery.management-tags	 
spring.cloud.consul.discovery.port	 
spring.cloud.consul.discovery.prefer-agent-address				false
spring.cloud.consul.discovery.prefer-ip-address					false
spring.cloud.consul.discovery.query-passing						false
spring.cloud.consul.discovery.register	true
spring.cloud.consul.discovery.register-health-check				true
spring.cloud.consul.discovery.scheme							http
spring.cloud.consul.discovery.server-list-query-tags
spring.cloud.consul.discovery.service-name
spring.cloud.consul.discovery.tags
spring.cloud.consul.discovery.serviceName 						是指註冊到 Consul 的服務名稱,後期客戶端會根據這個名稱來進行服務調用。
		
		

配置服務參數

		
配置項											預設值
spring.cloud.consul.config.enabled				true
spring.cloud.consul.config.prefix				config
spring.cloud.consul.config.default-context		application
spring.cloud.consul.config.profile-separator	,
spring.cloud.consul.config.data-key				data
spring.cloud.consul.config.format				KEY_VALUE, PROPERTIES, YAML, FILES
spring.cloud.consul.config.name					${spring.application.name}
spring.cloud.consul.config.acl-token	 
spring.cloud.consul.config.fail-fast			false
spring.cloud.consul.config.watch.enabled		true
spring.cloud.consul.config.watch.wait-time		55
spring.cloud.consul.config.watch.delay			1000			
		
		

7.2.2. Maven 父項目

		
<?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>cn.netkiller</groupId>
	<artifactId>parent</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<url>http://www.netkiller.cn</url>

	<organization>
		<name>Netkiller Spring Cloud 手札</name>
		<url>http://www.netkiller.cn</url>
	</organization>

	<developers>
		<developer>
			<name>Neo</name>
			<email>netkiller@msn.com</email>
			<organization>Netkiller Spring Cloud 手札</organization>
			<organizationUrl>http://www.netkiller.cn</organizationUrl>
			<roles>
				<role>Author</role>
			</roles>
		</developer>
	</developers>

	<!--使用aliyun鏡像 -->
	<repositories>
		<repository>
			<id>alimaven</id>
			<name>Maven Aliyun Mirror</name>
			<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

	<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>Greenwich.SR1</spring-cloud.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath />
	</parent>


	<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>

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

	<modules>
		<module>consol-producer</module>
		<module>consol-consumer</module>
		<module>consol-config</module>
		<module>openfeign</module>
	</modules>

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

</project>		
		
		

7.2.3. Consul 服務生產者

7.2.3.1. Maven

			
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.netkiller</groupId>
		<artifactId>parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<!-- <groupId>cn.netkiller</groupId> -->
	<artifactId>consul-producer</artifactId>
	<!-- <version>0.0.1-SNAPSHOT</version> -->
	<name>consul-producer</name>
	<url>http://www.netkiller.cn</url>
	<description>Spring Cloud Consul Sample</description>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>11</java.version>
	</properties>
	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</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-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<!-- Only needed at compile time -->
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<!-- <version>3.8.1</version> -->
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<!-- <configuration> <mainClass>cn.netkiller.config.Application</mainClass> </configuration> -->
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
			
			
			

7.2.3.2. application.properties

			
server.port=8080
spring.application.name=spring-cloud-consul-producer

spring.cloud.consul.host=192.168.4.217
spring.cloud.consul.port=8500

logging.level.org.springframework.cloud.consul=DEBUG	
			
			

7.2.3.3. SpringApplication

			
package cn.netkiller.consul;

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

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
	public static void main(String[] args) {
		System.out.println("Hello World!");
		SpringApplication.run(Application.class, args);
	}
}			
			
			

7.2.3.4. TestController

			
package cn.netkiller.consul.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	public TestController() {
		// TODO Auto-generated constructor stub
	}

	@GetMapping("/hello")
	public String provider() {
		return "Helloworld!!!";
	}

	@RequestMapping("/hi")
	public String hi(@RequestParam(name = "name") String name) {
		return "hi " + name + "!";
	}

}
			
			
			

7.2.4. Consul 服務消費者

7.2.4.1. Maven

			
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.netkiller</groupId>
		<artifactId>parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>cn.netkiller</groupId>
	<artifactId>consul-consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>consul-consumer</name>
	<url>http://www.netkiller.cn</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>11</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>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<!-- <configuration> <mainClass>cn.netkiller.config.Application</mainClass> </configuration> -->
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
			
			
			

7.2.4.2. application.properties

			
server.port=8082
spring.application.name=spring-cloud-consul-consumer

spring.cloud.consul.host=192.168.4.217
spring.cloud.consul.port=8500
#設置不需要註冊到 consul 中
spring.cloud.consul.discovery.register=false
			
			
			

7.2.4.3. SpringApplication

			
package cn.netkiller.consul.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		System.out.println("Consol Consumer!");
		SpringApplication.run(Application.class, args);
	}
}
			
			
			

7.2.4.4. TestController

			
package cn.netkiller.consul.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestController {

	public ConsumerController() {
		// TODO Auto-generated constructor stub
	}

	@Autowired
	private LoadBalancerClient loadBalancerClient;
	@Autowired
	private DiscoveryClient discoveryClient;

	/**
	 * 獲取所有服務
	 */
	@RequestMapping("/services")
	public Object services() {
		return discoveryClient.getInstances("spring-cloud-consul-producer");
	}

	/**
	 * 從所有服務中選擇一個服務(輪詢)
	 */
	@RequestMapping("/discover")
	public Object discover() {
		return loadBalancerClient.choose("spring-cloud-consul-producer").getUri().toString();
	}

	@RequestMapping("/call")
	public String call() {
		ServiceInstance serviceInstance = loadBalancerClient.choose("spring-cloud-consul-producer");
		System.out.println("服務地址:" + serviceInstance.getUri());
		System.out.println("服務名稱:" + serviceInstance.getServiceId());

		String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
		System.out.println(callServiceResult);
		return callServiceResult;
	}
}
			
			
			

7.2.5. Openfeign

7.2.5.1. Maven

			
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.netkiller</groupId>
		<artifactId>parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>cn.netkiller</groupId>
	<artifactId>openfeign</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>openfeign</name>
	<url>http://www.netkiller.cn</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-consul-discovery</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<!-- <configuration> <mainClass>cn.netkiller.config.Application</mainClass> </configuration> -->
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
			
			
			

7.2.5.2. application.properties

			
server.port=8083
spring.application.name=spring-cloud-consul-openfeign

spring.cloud.consul.host=192.168.4.217
spring.cloud.consul.port=8500

spring.cloud.consul.discovery.register=false
			
			
			

7.2.5.3. SpringApplication

			
package cn.netkiller.openfeign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients

public class Application {
	public static void main(String[] args) {
		System.out.println("openfeign!");
		SpringApplication.run(Application.class, args);
	}
}
			
			
			

7.2.5.4. Feign 介面

			
package cn.netkiller.openfeign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "spring-cloud-consul-producer", fallback = FeignFallback.class)
public interface TestFeign {
	@RequestMapping(value = "/hi", method = RequestMethod.GET)
	String hi(@RequestParam(value = "name") String name);
}
			
			
			
			
package cn.netkiller.openfeign;

public class FeignFallback implements TestFeign {
	@Override
	public String hi(String name) {
		return "sorry,熔斷介入";
	}

}
			
			
			

7.2.5.5. TestController

			
package cn.netkiller.openfeign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	@Autowired
	TestFeign testFeign;

	@RequestMapping("/feign")
	public String testFeign(@RequestParam(name = "name") String name) {
		return testFeign.hi(name);
	}
}