Spring Cloud微服务(7)之Feign服务之间调用

1.简介

微服务架构服务实例众多,服务与服务之间如何调用,Spring Cloud提供了解决方案:伪装者 Feign。

Feign 是 Spring Cloud 的一个组件,也是一个WebService客户端,用于服务之间的调动。


2. 如何使用

第一步:服务之间调用

本例需要创建三个工程:

eureka-server 注册中心服务(项目创建参照第三节网关)

product-service 产品服务(最基础的客户端服务,提供一个rest接口即可。前面第三节已创建,可参考,不需要做任何修改)

order-service-feignclient  订单服务(测试服务调用)

order-service-feignclient 工程目录如下图:


(1)pom.xml增加依赖

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>

(2)增加接口和 @FeignClient 注解

package com.hole.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by helei on 2017/7/16.
 */
@FeignClient(value = "product-service",fallback = ProductServiceFeignDaoHystrix.class)
public interface ProductServiceFeignDao {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    String hello();
}

@FeignClient(value = "product-service"
value设置要调用服务的服务名。Feign会根据服务名去注册中心查找服务url

(3)启动类增加 @EnableFeignClient开启Feign功能,新增Rest接口用于调用产品服务。

package com.hole;

import com.hole.feign.ProductServiceFeignDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@EnableDiscoveryClient
@SpringBootApplication
@RestController
@EnableFeignClients
public class OrderServiceFeignclientApplication {
	@Autowired
	private ProductServiceFeignDao productServiceFeignDao;

	public static void main(String[] args) {
		SpringApplication.run(OrderServiceFeignclientApplication.class, args);
	}

	@RequestMapping(value = "/product/hello",method = RequestMethod.GET)
	public String getHelloFromProductService(){
		return productServiceFeignDao.hello();
	}
}

(4)启动服务验证

启动成功后请求 /product/hello,结果成功调用产品服务并返回结果。



第二步:增加 Hystrix 断路熔断机制

(1)增加 Feign接口实现类,实现方法作为断路器的回调方法。

package com.hole.feign;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * Created by helei on 2017/7/16.
 */
@Component
public class ProductServiceFeignDaoHystrix implements ProductServiceFeignDao {

    @Override
    public String hello() {
        return "服务不见鸟,稍后再试!";
    }
}
(2)Feign接口的 @FeignClient注解增加 fallback参数,指向接口的实现类。

package com.hole.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by helei on 2017/7/16.
 */
@FeignClient(value = "product-service",fallback = ProductServiceFeignDaoHystrix.class)
public interface ProductServiceFeignDao {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    String hello();
}

(3)启动服务验证。

启动成功后将 product-sevice 服务关闭,再次请求 /product/hello,结果发现报异常。


发现断路器并没有起作用,而是直接抛异常。

带着疑惑查了下官方API,官方文档中大致意思是feign默认是启用hystrix的,然而测试的结果是并没有。但发现了一段配置

feign.hystrix.enabled=false

这个配置意思很明确了吧,将false改为true,试试又不犯法,结果。。。

对,就是你认为的那样,成功了!



还有一个地方要补充下:

FigenClient已经包含了Hystrix,所有启动类不需要再开启 @EnableCircuitBreaker。


猜你喜欢

转载自blog.csdn.net/u013084910/article/details/76539201