没有网站如何做adsense,摄影网站源码,专业网站开发公司地址,形象墙设计公司1、OpenFeign 1.1 概述 – Feign是一个声明性web服务客户机。它使编写web服务客户机变得更容易。 – 它的使用方法是定义一个服务接口并在上面添加注解。 – Feign支持可插拔编码器和解码器。 – Spring Cloud对Feign进行了封装#xff0c;使其支持SpringMVC标准注解和Httpttp… 1、OpenFeign 1.1 概述 – Feign是一个声明性web服务客户机。它使编写web服务客户机变得更容易。 – 它的使用方法是定义一个服务接口并在上面添加注解。 – Feign支持可插拔编码器和解码器。 – Spring Cloud对Feign进行了封装使其支持SpringMVC标准注解和HttpttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。 1.2 Feign的作用 Feign旨在使编写Java Http客户端变得更容易。 前面在使用RibbonRestTemplate时利用RestTemplate对http请求的封装处理形成了一套模版化的调用方法。但在实际开发中由于对服务依赖的调用可能不止一处往往一个接口可能被多处调用所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以Feign在此基础上做了进一步封装由它来定义和实现依赖服务接口的定义。在Feign的实现下只需要创建一个接口并使用注解的方式来配置它以前是Dao接口上面标注Mapper注解现在是一个微服务接口上标注一个Feign注解即可即可完成对服务提供方的的接口绑定简化了使用Spring Cloud Ribbon时自动封装服务调用客户端的开发量。 1.3 Feign集成了Ribbon 利用Ribbon维护了Payment的服务列表信息并且通过轮询实现了客户端的负载均衡。而与Ribbon不同的是通过Feign只需要定义服务绑定接口且以声明式的方法优雅而简单的实现了服务的调用。 1.4 Feign和OpenFeign的区别 Feign是SpringCloud组件中一个轻量级RESTful的HTTP服务客户端Feign内置了Ribbon,用来做客户端负载均衡去调用服务注册中心的服务。Feign的使用方式是使用Feign的注解定义接口调用这个接口就可以调用服务注册中心的服务。 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-feign/artifactId
/dependencyOpenFeign是SpringCloud在Feign的基础上支持了SpringMVC的注解如RequestMapping等。OpenFeign 的FeignClient可以解析SpringMVC的RequestMapping注解下的接口并通过动态代理的方式产生实现类实现类中做负载均衡并调用其他服务。 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId
/dependency2、Fegin的使用 2.1 EnableFeignClients 在启动类上使用EnableFeignClients注解开启Feign客户端并注入RestTemplate package com.fcant.userservice;import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;/*** UserServiceApplication** encoding:UTF-8* author Fcant 9:54 2019/12/9*/
EnableFeignClients
SpringCloudApplication
public class UserServiceApplication {public static void main(String[] args) {SpringApplication.run(UserServiceApplication.class, args);} BeanLoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}
} 2.2 Fegin的Service层 在接口层使用注解**FeignClient(name area-service)**指定请求的服务名请求中有参数需要通过**PathVariable(name countryId)**注解的name属性指定才能解析反射值 package com.fcant.userservice.feign;
import com.fcant.userservice.bean.Country;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
ICountryService
通过FeignClient注解指定Feign客户端名称为要访问的服务名encoding:UTF-8 *
author Fcant 10:24 2019/12/9 */
FeignClient(name “area-service”) public interface ICountryService {/**queryCountryFeign中没有指定PathVariable(name “countryId”)变量映射关系必须通过name属性指定 *param countryId 城市IDreturn com.fcant.userservice.bean.Countryauthor Fcant 10:53 2019/12/9 */ GetMapping(“/country/{countryId}”) Country queryCountry(PathVariable(name “countryId”) long countryId);
} 2.3 接口层 接口层的调用和使用和平常的Controller没什么区别把Service层注入即可 java
package com.fcant.userservice.controller;import com.fcant.userservice.bean.Country;
import com.fcant.userservice.bean.User;
import com.fcant.userservice.feign.ICountryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;/*** FeignController* p* encoding:UTF-8** author Fcant 10:06 2019/12/9*/
RestController
RequestMapping(/user)
public class FeignController {private static final ListUser USER_LIST;static {ListUser userList new ArrayList();userList.add(new User(3L, User 03, 男, user03fcant.com, 1L, ));userList.add(new User(4L, User 04, 女, user04fcant.com, 2L, ));USER_LIST userList;}private final ICountryService countryService;public FeignController(ICountryService countryService) {this.countryService countryService;} GetMapping(/feign)public ListUser queryUser() {ListUser userList new ArrayList(USER_LIST);userList.forEach(user - {Country country countryService.queryCountry(user.getCountryId());user.setCountryName(country.getCountryName());});return userList;}
} 3、Fegin异常处理的回调 A.在接口的注解上添加fallback属性指定异常回调的类 package com.fcant.userservice.feign;import com.fcant.userservice.bean.Country;
import com.fcant.userservice.feign.impl.CountryServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;/*** ICountryService* 通过FeignClient注解指定Feign客户端名称为要访问的服务名* p* encoding:UTF-8** author Fcant 10:24 2019/12/9*/
FeignClient(name area-service, fallback CountryServiceImpl.class)
public interface ICountryService {/*** queryCountry* Feign中没有指定PathVariable(name countryId)变量映射关系必须通过name属性指定** param countryId 城市ID* return com.fcant.userservice.bean.Country* author Fcant 10:53 2019/12/9*/GetMapping(/country/{countryId})Country queryCountry(PathVariable(name countryId) long countryId);
} B.在指定的处理异常的类中实现接口的方法进行处理 该类实现FeginClients注解指定的接口该类添加Component注解将其作为Bean注入 package com.fcant.userservice.feign.impl;
import com.fcant.userservice.bean.Country; import com.fcant.userservice.feign.ICountryService; import org.springframework.stereotype.Component;
/**
CountryServiceImplencoding:UTF-8 *
author Fcant 14:24 2019/12/9 */ Component public class CountryServiceImpl implements ICountryService {
private static final Country ERROR new Country(1L, “ERROR”); /**
queryCountry
Feign中没有指定PathVariable(name “countryId”)变量映射关系必须通过name属性指定 *
param countryId 城市ID
return com.fcant.userservice.bean.Country
author Fcant 10:53 2019/12/9 */ Override public Country queryCountry(long countryId) { return ERROR; }}Fegin的异常处理属于熔断的范围要在配置文件配置开启熔断-application.yml feign:hystrix:enabled: true4、关于Feign访问超时的配置 如果不配置访问超时则会报如下异常 feign.RetryableException: Read timed out executing POST http://******因为Feign接口调用分两层Ribbon的调用和Hystrix调用理论上设置Ribbon的时间即可但是Ribbon的超时时间和Hystrix的超时时间需要结合起来按照木桶原则最低的就是Feign的超时时间建议最好配置超时时间一致。经过配置一下application设置后完美解决了问题。因为第三方接口中需要3~20秒不等的时间所以这个数值也是根据自己的业务系统情况设置的。 # hystrix的超时时间
hystrix:command:default:execution:timeout:enabled: trueisolation:thread:timeoutInMilliseconds: 30000
# ribbon的超时时间-设置feign客户端超时时间openfeign默认支持ribbon
ribbon:# 指的是建立连接所用的时间适用于网络状况正常的情况下两端连接所用的时间ReadTimeout: 30000# 指的是建立连接后从服务器读取到可用资源所用的时间ConnectTimeout: 30000 5、OpenFeign日志增强 Feign提供了日志打印功能可以通过配置来调整日志级别从而了解Feign中Http请求的细节。对Feign接口的调用情况进行检控和输出。 NONE默认的不显示任何日志。BASIC仅记录请求方法、URL、响应状态吗即执行时间。HEADERS除了BASIC中定义的信息之外还有请求和响应的头信息。FULL除了HEADERS中定义的信息之外还有请求和响应的正文及元数据。 1添加配置类指明日志打印级别 import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Configuration
public class FeignConfig {BeanLogger.Level feignLoggerLevel(){return Logger.Level.FULL;}
} 2application.yml/properties配置文件开启日志的Feign客户端 logging:level:#feign日志以什么级别监控哪个接口com.atguigu.springcloud.service.PaymentFeignService: debug