openfeign简单入门操作

使用到三模块(其中两个微服务)

1,yiliao-feign-api模块

2,yiliao-health服务消费者微服务模块

3,yiliao-record服务提供者微服务模块

1,在需要定义提供feign调用接口的yiliao-feign-api模块导入相应依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2,feign-api模块中新增接口

yialio-feign-api微服务新建调用yiliao-record服务的接口规范

package com.wzc.apis.record;

import com.wzc.model.inspect.dtos.ToothDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(value = "yiliao-record")
public interface IToothClient {
    @PostMapping("/api/v1/article/save")
    public boolean saveTooth(@RequestBody ToothDto dto);
}

解释:

value:

  • 服务提供者的名称
  • 支持占位符 ${project.feign-prefix} 形式进行指定, 从配置文件中读取
  • 如果没有配置url, 将作为服务发现使用 http://value/

url:

  • url指定服务的地址
    列: http://127.0.0.1:8080 | http://www.baidu.com
  • 不经过Ribbon的服务选择
  • 支持占位符 ${project.feign-url} 形式进行指定, 从配置文件中读取

3,在yiliao-record中实现该方法

新建一个feign实现feign-api服务中定义的接口IToothClient

package com.wzc.record.feign;

import com.wzc.apis.record.IToothClient;
import com.wzc.model.common.dtos.ResponseResult;
import com.wzc.model.inspect.dtos.ToothDto;
import com.wzc.model.inspect.pojo.Tooth;
import com.wzc.record.service.IToothService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ToothClient implements IToothClient {

    @Autowired
    private IToothService toothService;

    /**
     *  保存成功后,将id返回
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveTooth(ToothDto dto) {
        Tooth tooth = new Tooth();
        BeanUtils.copyProperties(dto,tooth);
        toothService.save(tooth);
        return ResponseResult.okResult(tooth.getId());
    }
}

4,yiliao-health中feign远程接口调用

1,yiliao-health微服务依赖医疗-feign-api

<dependency>
    <groupId>com.wzc</groupId>
    <artifactId>yiliao-feign-api</artifactId>
</dependency>

2,在yiliao-health的引导类开启feign调用,并扫描包

@EnableFeignClients(basePackages="com.wzc.apis")

解释:因为在yiliao-health的服务下默认扫描的的是com.wzc.health的包,无法扫描到feign-api服务中的包,所以需要加个 包扫描

image-20240109210648578

3,在yiliao-health中注入IToothClient即可发起远程调用

image-20240109211718383

5,远程调用服务降级处理

  • 服务降级是服务自我保护的一种方式,或者保护下游服务的一种方式,用于确保服务不会受到请求突然突增影响变得不可用,确保服务不会崩溃。
  • 服务降级虽然会导致请求失败,但是不会导致阻塞。

1,在yiliao-feign-api编写降级逻辑

新建一个fallback包,然后新建IToothClientFallback类

package com.wzc.apis.record.fallback;

import com.wzc.apis.record.IToothClient;
import com.wzc.model.common.dtos.ResponseResult;
import com.wzc.model.common.enums.AppHttpCodeEnum;
import com.wzc.model.inspect.dtos.ToothDto;
import org.springframework.stereotype.Component;

@Component
public class IToothClientFallback implements IToothClient {
    @Override
    public ResponseResult saveTooth(ToothDto dto) {
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取数据失败");
    }
}

2,远程接口指向降级代码

通过fallback指向熔断降级的类

@FeignClient(value = "yiliao-record",fallback = IToothClientFallback.class)
public interface IToothClient {
    @PostMapping("/health/heath-form/save")
    public ResponseResult saveTooth(@RequestBody ToothDto dto);
}

3,yiliao-health微服务包扫描,扫描fallback包

package com.wzc.health.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.wzc.apis.record.fallback")
public class InitConfig {
}

4,yiliao-health消费端微服务配置文件开启降级

feign:
  # 开启feign对hystrix熔断降级的支持
  hystrix:
    enabled: true
  # 修改调用超时时间
  client:
    config:
      default:
        connectTimeout: 2000
        readTimeout: 2000

5,测试

​ 在锯条远程调用的实现类上新加休眠3秒(熔断降级设置了调用超时时间2秒),通过断点debug查看是否进入熔断降级,获取数据失败。

try {
    Thread.sleep(3000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

  • 24
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值