feign 学习

微服务项目 debug 模式

如何实现 postman 中发送主服务接口的请求,可以跳转到 feign 调用的服务接口 进行debug

调用服务 openapi
被调用服务  alarm

1 找到 openapi 调用 alarm 的 @Feign 注解位置

@ApiIgnore
@FeignClient(value = "alarm-test", url = "集群中微服务 service", contextId = "alarm-test")
@RequestMapping("/test")
public interface ToAlarmApi{}

2 更改URL
@FeignClient(....)url 为集群中微服务的service,直接postman发送主服务的接口时,feign时会通过该 url 找到微服务接口,这样访问的微服务接口是集群中微服务的接口,有时是服务调用失败,而且这样debug 是没有意义的

于是将 url 改为 debug 模式下微服务接口 的本地请求路径

@ApiIgnore
@FeignClient(value = "alarm-test", url = "localhost:30102/alarm", contextId = "alarm-test")
@RequestMapping("/test")
public interface ToAlarmApi{}

**还不知道为啥上边步骤是不行的**
目前改 application.yaml 中的url可以

集群:
  service:
    alarm: ${集群.service:http://集群.service.namespace.svc.cluster.local}
    
改为

集群:
  service:
    alarm: http://localhost:30102

3 测试
debug 模式运行 openapi 和 alarm
openapi 和 alarm 中都打好断点
postman 中填入 主服务的请求,发送请求,可以发现 能够进入 alarm 的断点位置

请求链接参考 https://blog.csdn.net/qq_40813329/article/details/121791593
在这里插入图片描述

feign返回体映射

feign 返回的格式是 json

调用的接口

/web/auths

返回内容

{
    "infos": {
        "xxx-count": 1,
        "yyy-count": 100,
        ....
    }
}

接收返回体定义

@Data
@Builder
@ApiModel
@NoArgsConstructor
@AllArgsConstructor
public class AuthorizationInfos {

    @ApiModelProperty(value = "描述信息")
    @JsonProperty("infos")     feign返回的json格式根据 json 的 name 映射到属性
    private Map<String, Integer> infos;
}

feign调用

@ApiIgnore
@FeignClient(value = "feign调用名字对应application.yml中配置", url = "${application.yml中配置}")
public interface StreamingManager {

    @GetMapping(value = "/web/auths", produces = MediaType.APPLICATION_JSON_VALUE)
    AuthorizationInfos getAuthorizationList();
	//或者直接 Map<String, Integer> getAuthorizationList();
}

feign 拦截器

使用RequestInterceptor拦截器实现request转发(包括处理特殊字符
Feign 支持请求拦截器,在发送请求前,可以对发送的模板进行操作,例如设置请求头等属性,自定请求拦截器需要实现 feign.RequestInterceptor 接口,该接口的方法 apply 有参数 template ,该参数类型为 RequestTemplate,我们可以根据实际情况对请求信息进行调整,示例如下:

创建自定义请求拦截器,在发送请求前增加了一个请求头信息,进行身份校验

@Slf4j
@Component
public class FeignAddHeaderRequestInterceptor implements RequestInterceptor {
 
    @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();
                    if (name.startsWith(HttpHeader.HTTP_HEADER_PREFIX) || name.startsWith(HttpHeader.HTTP_HEADER_PREFIX.toLowerCase())) {
                        String values = request.getHeader(name);
                        template.header(name, values);
                    }
                }
            }
        }else{
            template.header("Unif-Subsystem-Code", "07");
        }
    }
}
package xxx.feign.interceptor;

@Slf4j
@Component
public class xxxFeignInterceptor implements RequestInterceptor {

    private static final String SOMETHING = "something";

    @Resource
    private xxx xxx;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        Class<?> targetClass = requestTemplate.feignTarget().type();
        if (xxx.class.equals(targetClass)) {
            ...
            requestTemplate.header(SOMETHING,...);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值