SpringCloudAlibaba+Nacos整合OpenFeign

导读:本篇文章讲解 SpringCloudAlibaba+Nacos整合OpenFeign,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

Feign是Spring Cloud提供的一个声明式的伪Http客户端, 它使得调用远程服务就像调用本地服务一样简单, 只需要创建一个接口并添加一个注解即可。

Nacos很好的兼容了Feign, 在Nacos下使用Fegin默认就实现了负载均衡的效果。

1、如何使用

以下是feign的基本使用方式

1、pom依赖

<!--fegin组件-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2、开启注解

@SpringBootApplication
// 注册中心
@EnableDiscoveryClient
// 开启Fegin
@EnableFeignClients 
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication .class, args);
    }
}

3、创建Feign接口

wage服务中,有一个定义的Controller接口如下:

@RestController
public class WageController {
  
    @GetMapping("list")
    public void listUser(){
        System.out.println("工资列表");
    }
}

User服务中,配置Feign如下:

// 声明调用的提供者的name
@FeignClient("wage-service") 
public interface WageServiceFeign {
    //指定调用提供者的哪个方法
    //@FeignClient+@GetMapping 就是一个完整的请求路径 http://wage-service/wage/list

    //此处定义方式和wage服务中controller接口定义方式一致
    //注意:如果是传参的Get请求需要在参数前加入@RequestParam注解
    @GetMapping(value = "/wage/list")
    void findWage();
}

4、调用
在User服务中,调用Wage服务。

@Service
pulic class UserService{
	@Resource
	private WageServiceFeign wageServiceFeign;
	
	public void test(){
		wageServiceFeign.findWage();
	}
}

2、调用超时配置

如果我们觉得默认的feign请求和响应时间,通过以下配置可以进行修改:

feign:
  client:
    config:
      default:
        # 建立链接的超时时长
        connectTimeout: 5000
        # 读取超时时长
        readTimeout: 5000

在上面的配置中,通过default表示对所有的feign请求设置统一的超时时长,如果想要对某个服务请求的单独设置不同的超时时长,采用以下方式:

feign:
  client:
    config:
      # 被调用服务的名称
      wage-service:
        # 建立链接的超时时长
        connectTimeout: 5000
        # 读取超时时长
        readTimeout: 5000

3、日志打印配置

在开发中如果想要看到Feign请求过程的日志记录,默认情况下Feign的日志是没有开启的,通过配置 application.yml开启日志打印:

feign:
  client:
    config:
      default:
        # 设置日志级别
        loggerLevel: basic

logging:
  level:
  	# Feign接口所在的包
    com.test.feign.wage: debug

在上面的配置中,通过default表示对所有的feign请求设置统一的日志级别,如果想要对某个服务请求的单独设置不同的日志级别,采用以下方式:

feign:
  client:
    config:
      # 被调用服务的名称
      wage-service:
        # 设置日志级别
        loggerLevel: basic

logging:
  level:
  	# Feign接口所在的包
    com.test.feign.wage: debug

feign日志记录说明:

  • none:不记录任何日志(默认值)

  • basic:仅记录请求方法、URL、响应状态代码以及执行时间

  • headers:在basic级别的基础上,还记录了请求和响应的header

  • full:记录请求和响应的header、body和元数据。

效果:
在这里插入图片描述

4、指定配置文件

我们可以自定义一个配置类,并且将Feign指向自定义的配置类,比如:我们可以在类中配置header传递,也可以进行其他操作。

配置类如下:

@Configuration
public class FeignConfiguration implements RequestInterceptor {

    /**
     * 设置header传递,将当前服务的header信息传递到下游服务
     *
     * @param template
     */
    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            Enumeration<String> headerNames = request.getHeaderNames();
            if (headerNames != null) {
                while (headerNames.hasMoreElements()) {
                    String name = headerNames.nextElement();
                    String values = request.getHeader(name);
                    // 跳过 content-length
                    if ("content-length".equals(name)) {
                        continue;
                    }
                    template.header(name, values).header("test", "test");
                }
            }
        }
    }
}

修改@FeignClient

@FeignClient(value = "user-service",configuration = {FeignConfiguration.class})
public interface TestFeign {
   ....
}

5、整合Sentinel降级容错

pom

<!--sentinel客户端-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

yaml

# feign启用sentinel
feign:
  sentinel:
	enabled: true

Fallback容错类

@Component
public class TestServiceFallbackFactory implements FallbackFactory<TestServiceFeign> {

    @Override
    public WageServiceFeign create(Throwable throwable) {
        // 可以将异常继续抛出,也可以实现自定义的内容
        throw new RuntimeException(throwable.getMessage());
    }
}

修改@FeignClient

//加入fallbackFactory指向容错类
@FeignClient(value = "user-service",fallbackFactory = TestServiceFallbackFactory .class),configuration = {FeignConfiguration.class})
public interface TestFeign {
   ....
}

6、异常处理器

我们可以实现异常拦截,在feign抛出异常后进行一些自定义处理

定义FeignErrorDecoder:

@Configuration
public class FeignErrorDecoder extends ErrorDecoder.Default {

    private final Logger log = LoggerFactory.getLogger(FeignErrorDecoder.class);

    @Override
    public Exception decode(String methodKey, Response response) {
        Exception exception = super.decode(methodKey, response);

        // 如果是RetryableException,则返回继续重试
        if (exception instanceof RetryableException) {
            return exception;
        } else {
            // 可以自定义一些逻辑,比如抛出一个其他的(统一个异常)
            exception = new RuntimeException("Feign调用出错了");
        }
        return exception;
    }
}

修改@FeignClient
configuration中加入FeignErrorDecoder

@FeignClient(value = "user-service",configuration = {FeignConfiguration.class, FeignErrorDecoder.class})
public interface TestFeign {
   ....
}

与Fallback容错类关系:
如果同时配置FeignErrorDecoderFallbackFactory,那么异常会先进入到FeignErrorDecoder然后再FallbackFactory中。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/18046.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!