spring cloud feign调用实现逻辑

实现步骤:

服务端:

1. 添加feign的依赖
 <!--openfeign (feign调用依赖)-->
            <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2. 启动类添加feign的注解
    @EnableFeignClients

 

客户端:

1. 添加feign的依赖
 <!--openfeign (feign调用依赖)-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2. 编写feign接口
package com.hnu.bigscreen;

import com.hnu.common.respone.PojoBaseResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

// name 为服务名,接口的路径名为restcontrol的路径+接口的路径
@FeignClient(name = "bigScrren")
public interface CarConditionFeign {

    /**
     * 根据carId获取该车最新车况信息
     * @param id
     * @return
     */
    @RequestMapping("condition/getOneConditionByCarId/{id}")
    public PojoBaseResponse getOneConditionByCarId(@PathVariable("id") String id);

    /**
     * kafka发送数据
     * @return
     */
    @PostMapping("condition/sendKafkaMessage")
    public PojoBaseResponse sendKafkaMessage();
}

feign客户端配置

1. feiclient文件添加config注释,如下
@FeignClient(url = "${cmp.url}", name = "cmpService", configuration = FeignClientConfig.class)
public interface aaa {

}

2. 编写feignclient配置文件类
public class FeignClientConfig {
    /**
     * 超时时间
     */
    @Value("${client.retryer.period}")
    private Long period;

    /**
     * 重试间隔时间
     */
    @Value("${client.retryer.maxPeriod}")
    private Long maxPeriod;

    /**
     * 需要重试的次数 + 第一次请求的次数1
     */
    @Value("${client.retryer.maxAttempts}")
    private int maxAttempts;

    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default(period, maxPeriod, maxAttempts);
    }
}

整合断路器:

1. 整合hystric断路器
@FeignClient(url = "${vms.address}", name = "vmsService", configuration = FeignClientConfig.class, fallbackFactory = VmsClientFallback.class)
//TODO 添加fallback
public interface VmsClient {
}

2. 编写Fallback.class文件
@Component
public class VmsClientFallback implements FallbackFactory<VmsClient> {

    @Autowired
    private BdsSdRecordErrorLogService errorLogService;

    @Override
    public VmsClient create(Throwable throwable) {
        return new VmsClient() {
            @Override
            public VmsResponse<VmsVehicleModelMetaData> getVehicleModelData(Object params) {
                return new VmsResponse<>("001", throwable.getMessage());
            }

            @Override
            public VmsResponse<VmsVehicleInspection> getVehicleInspectionData(Object params) {
                return null;
            }

            @Override
            public VmsResponse<VmsVehicleOffLine> getVehicleOffLineData(Object params) {
                return null;
            }

            @Override
            public VmsResponse<VmsVehicleSaleInfo> getVehicleSaleInfoData(Object params) {
                return null;
            }

            @Override
            public VmsResponse<VmsDealerInfo> getVehicleDealerData(Object params) {
                return null;
            }
        };
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一一可可

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值