(八)SpringBoot+SpringCloud —— 使用Feign消费服务

Feign简介

Spring Cloud Feign是基于Netflix Feign实现,整合了Spring Cloud Riggon与Spring Cloud Hystrix,出了提供这二者的强大功能外,他还提供了一种声明式的Web服务客户端定义方式。java

在使用Spring Cloud Ribbon时,一般都会利用它对RestTemplate的请求拦截来实现对服务的调用,而RestTemplate已经实现了对HTTP请求的封装处理,造成了一套模板化的调用方法。git

快速使用

添加对feign的依赖:github

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

在主类使用注解开启Feign的功能:web

package cn.net.bysoft.owl.bookstore.web.api;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OwlBookstoreWebApiApplication {

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

接着,建立调用接口,使用注解来消费服务:spring

package cn.net.bysoft.owl.bookstore.web.api.service;

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

import cn.net.bysoft.owl.bookstore.facade.user.entity.User;
import cn.net.bysoft.owl.bookstore.web.api.fallback.UserServiceFallbackFactory;

@FeignClient(value = "SERVICE-USER", fallbackFactory = UserServiceFallbackFactory.class)
public interface UserService {

	@RequestMapping(value = "/users/{email}/", method = RequestMethod.GET)
	Boolean isExistsByEmail(@PathVariable("email") String email);

	@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
	User findById(@PathVariable("id") Long id);

}

接口中声明的方法的参数与返回值,要与服务提供的方法一致。api

Feign自动开启了Ribbon中的负载均衡和hystrix断路器,要使用断路器先须要开启断路器:bash

feign:
  hystrix:
    enabled: true
  compression:
    request:
      enabled: true
    response:
      enabled: true

那么就能够使用服务降级来处理消费失败的服务,能够选择建立一个UserServiceFallback类:app

package cn.net.bysoft.owl.bookstore.web.api.fallback;

import org.springframework.stereotype.Component;

import cn.net.bysoft.owl.bookstore.facade.user.entity.User;
import cn.net.bysoft.owl.bookstore.web.api.service.UserService;

@Component
public class UserServiceFallback implements UserService {

	@Override
	public Boolean isExistsByEmail(String email) {
		return false;
	}

	@Override
	public User findById(Long id) {
		return null;
	}

}

我在Feign中使用的是fallbackFactory来降级服务,使用该方式能够同时处理服务抛出的异常,建立一个UserServiceFallbackFactory类:负载均衡

package cn.net.bysoft.owl.bookstore.web.api.fallback;

import org.springframework.stereotype.Component;

import cn.net.bysoft.owl.bookstore.facade.user.entity.User;
import cn.net.bysoft.owl.bookstore.web.api.service.UserService;
import feign.hystrix.FallbackFactory;

@Component
public class UserServiceFallbackFactory implements FallbackFactory<UserService> {

	@Override
	public UserService create(Throwable cause) {
		return new UserService() {
			
			@Override
			public Boolean isExistsByEmail(String email) {
				System.out.println(cause.getMessage());
				return false;
			}
			
			@Override
			public User findById(Long id) {
				cause.printStackTrace();
				System.out.println(cause.getMessage());
				return null;
			}
			
		};
	}

}

Feign配置

Ribbon配置

全局配置的方法很简单:ide

ribbon.ConnectTimeout=500
ribbon.ReadTimeout=5000

指定服务配置:

[server-name].ribbon.ConnectTime=500
[server-name].ribbon.ReadTimeout=5000

Feign中默认实现了请求的重试机制,这里要注意,Ribbon的超时与Hystrix的超时彻底不一样。

Hystrix配置

对于Hystrix的配置与以前同样,默认配置也是用前缀hystrix.command.default就能够进行设置。

Github

https://github.com/XuePeng87/owl-bookstore

相关文章
相关标签/搜索