OpenFeign服务接口绑定

作用

Feign旨在使编写java Http客户端更容易,使用Ribbon—+RestTemplate,利用RestTemplate对http请求进行封装,形成了一套模板化调用方法。但是在实际开发中,由于服务依赖调用可能不止一处,往往一个接口会被多出调用,所以通常都会针对每个微服务自行封装一些可续断来包装,我们只需要创建一个接口并使用注解的方式来配置,即可往常对服务提供放的接口绑定,简化了Spring cloud Ribbon,自动封装服务调用客户端的开发量,Feign继承了Ribbon,优雅简单的通过接口实现服务调用

Feign和OpenFeign

Feign是Spring Cloud组件中的一个轻量级的Rest风格的http服务客户端,Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务,调用使用Feign注解的接口,即可调用服务注册中心的服务

OpenFign是在Feign基础上支持了SpringMvc的注解,如@RequestMapping等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务

简单实现

controller

package com.jia.learn.controller;

import com.jia.learn.entities.CommonResult;
import com.jia.learn.entities.Payment;
import com.jia.learn.service.PaymentFeignService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @program: SpringCloudLearn
 * @description
 * @author: LIANG
 * @create: 2021-12-12 15:32
 **/
@RestController
@RequestMapping("/consumer")
public class OpenFeignOrderController {
    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        return paymentFeignService.getPaymentById(id);
    }
}

service Feign接口

package com.jia.learn.service;

import com.jia.learn.entities.CommonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @program: SpringCloudLearn
 * @description
 * @author: LIANG
 * @create: 2021-12-12 15:23
 **/
@Component
//@FeignClient注解配置对应服务
@FeignClient(value = "cloud-payment-service")
public interface PaymentFeignService {
    @GetMapping(value = "payment/get/{id}")
    CommonResult getPaymentById (@PathVariable("id") Long id);
}

启动类

package com.jia.learn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @program: SpringCloudLearn
 * @description
 * @author: LIANG
 * @create: 2021-12-12 15:12
 **/
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
//开启Feign
@EnableFeignClients
public class OpenFeienOrder8000 {
    public static void main(String[] args) {
        SpringApplication.run(OpenFeienOrder8000.class,args);
    }
}

配置

Ribbon负载均衡,超时时间等

# 服务名
cloud-payment-service:
  ribbon:
    #配置规则
    #    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #配置规则 随机
    #    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #配置规则 轮询
    #    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RetryRule #配置规则 重试
    #    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule #配置规则 响应时间权重
    #    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule #配置规则
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
    #请求连接超时时间
    ConnectTimeout: 5000
    #请求处理的超时时间
    ReadTimeout: 1000
    #对所有请求都进行重试
    OkToRetryOnAllOperations: true
    #切换实例的重试次数
    MaxAutoRetriesNextServer: 2
    #对当前实例的重试次数
    MaxAutoRetries: 1

增强日志

  • NONE:(默认)不显示任何日志

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

  • HEADERS:BASIC+请求头和响应头

  • FULL:HEADERS+请求和相应的正文和数据

    package com.jia.learn.config;
    
    import feign.Logger;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * @program: SpringCloudLearn
     * @description
     * @author: LIANG
     * @create: 2021-12-12 17:08
     **/
    @Configuration
    public class FeignConfig {
    
        @Bean
        Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }
    }
    
    
    logging:
      level:
        # feign日志以什么级别监控哪个接口
        com.jia.learn.service.PaymentFeignService: debug
    	
    

    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值