Spring cloud整合consul、feign、hystrix

服务端:

pom.xml文件:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.5.RELEASE
		 
	
	com.snd
	consul
	0.0.1-SNAPSHOT
	consul
	Demo project for Spring Boot

	
		1.8
		Greenwich.SR1
	

	
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
			org.springframework.cloud
			spring-cloud-starter-consul-discovery
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


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文件:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.5.RELEASE
		 
	
	com.snd
	consul-consumer
	0.0.1-SNAPSHOT
	consul-consumer
	Demo project for Spring Boot

	
		1.8
		Greenwich.SR1
	

	
		
			org.springframework.boot
			spring-boot-starter-actuator
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.springframework.cloud
			spring-cloud-starter-consul-discovery
		
		
		
		  org.springframework.cloud
		  spring-cloud-starter-openfeign
		
		
		
            org.springframework.cloud
            spring-cloud-starter-hystrix
            1.4.0.RELEASE
        
        
            org.springframework.cloud
            spring-cloud-starter-hystrix-dashboard
            1.4.0.RELEASE
        
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.cloud
				spring-cloud-dependencies
				${spring-cloud.version}
				pom
				import
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


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发现熔断机制已生效

你可能感兴趣的:(Java技巧,spring,cloud,consul,hystrix,feign,spring,boot)