服务端:
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.snd</groupId>
<artifactId>consul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consul</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
properties文件:
#项目名称
spring.application.name=spring-cloud-consul-producer-one
#项目启动端口
server.port=8501
#consul~IP
spring.cloud.consul.host=localhost
#consul~端口
spring.cloud.consul.port=8500
#注册到consul的服务名称
spring.cloud.consul.discovery.serviceName=consul-service-producer
controller测试:
package com.snd.consul.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/hello")
public String test() {
System.out.println("TestController==>hello consul one");
return "hello consul one";
}
}
启动类:
package com.snd.consul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* consul 服务端
* @author Jobs
*
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulApplication.class, args);
}
}
客户端:
pom文件:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.snd</groupId>
<artifactId>consul-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consul-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.0.RELEASE</version>
</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>
properties文件:
spring.application.name=spring-cloud-consul-consumer
server.port=8503
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
#设置不需要注册到 consul 中
spring.cloud.consul.discovery.register=false
#注册到consul的服务名称
#spring.cloud.consul.discovery.serviceName=consul-service-producer
#开启熔断机制
feign.hystrix.enabled=true
启动类:
package com.snd.consul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class ConsulApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulApplication.class, args);
}
}
controller层:
package com.snd.consul.controller;
import org.springframework.beans.factory.annotation.Autowired;
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;
@RestController
public class ServiceController {
@Autowired
private LoadBalancerClient loadBalancer;
@Autowired
private DiscoveryClient discoveryClient;
/**
* 获取所有服务
*/
@RequestMapping("/services")
public Object services() {
return discoveryClient.getInstances("consul-service-producer");
}
/**
* 从所有服务中选择一个服务(轮询)
*/
@RequestMapping("/discover")
public Object discover() {
return loadBalancer.choose("consul-service-producer").getUri().toString();
}
}
package com.snd.consul.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.snd.consul.service.FeignClientService;
@RestController
public class TestController {
@Autowired
private LoadBalancerClient loadBalancer;
@Autowired
private FeignClientService feignClientService;
@RequestMapping("/call")
public String call() {
ServiceInstance serviceInstance = loadBalancer.choose("consul-service-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;
}
@GetMapping("/feign/test")
public String feign() {
return feignClientService.testHello();
}
@HystrixCommand(fallbackMethod="hystrixTest")
@GetMapping("/hystrix/test")
public String hystrix() {
System.out.println("/hystrix/test");
ServiceInstance serviceInstance = loadBalancer.choose("consul-service-producer");
String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
return callServiceResult;
}
public String hystrixTest() {
return "调用服务发生错误,使用hystrix方法,非接口返回的值!";
}
}
service层:
package com.snd.consul.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import com.snd.consul.service.impl.FallbackServiceImpl;
//@FeignClient("spring-cloud-consul-producer-two")
@FeignClient(value="consul-service-producer", fallback=FallbackServiceImpl.class)
public interface FeignClientService {
@GetMapping("/hello")
String testHello();
}
package com.snd.consul.service.impl;
import org.springframework.stereotype.Component;
import com.snd.consul.service.FeignClientService;
/**
* 熔断机制实现类
* @author Jobs
*
*/
@Component
public class FallbackServiceImpl implements FeignClientService {
@Override
public String testHello() {
return "服务器熔断-接口实现--返回值====1111!";
}
}
步骤:
1、先在【https://www.consul.io/downloads.html】下载consul服务,然后解压启动。(window启动:cd到consul解压的目录下执行:consul agent -dev)
2、启动后打开网页【http://localhost:8500/ui/dc1/services】
3、更改服务端端口,启动两个服务端(为了演示负载均衡【修改controller一些返回值和打印值,以方便知道是调用了哪个服务端】)
4、启动客户端服务执行:http://localhost:8503/services、http://localhost:8503/call、http://localhost:8503/feign/test、http://localhost:8503/hystrix/test
5、断开两个服务端服务执行:http://localhost:8503/feign/test、http://localhost:8503/hystrix/test发现熔断机制已生效