(八)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

相關文章
相關標籤/搜索