微服务之间Feign调用

需使用的服务

java 复制代码
@FeignClient(name = "rdss-back-service", fallback = SysUserServiceFallback.class, configuration =
        FeignConfiguration.class)
public interface SysUserService {

    /**
     * 订单下单用户模糊查询
     */
    @GetMapping(value = "/user/getOrderUserName")
    List<SysUserVo> getOrderUserName(@RequestParam(value = "username", required = false) String username);
   }
java 复制代码
@Slf4j
@Service
public class SysUserServiceFallback implements SysUserService {

    @Override
    public List<SysUserVo> getOrderUserName(String username) {
        log.error("调用getOrderUserName方法异常,参数:{}", username);
        return null;
    }
}
java 复制代码
package com.rdss.common.config;

import com.rdss.common.constants.CommonConstants;
import feign.Body;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@Configuration
public class FeignConfiguration implements RequestInterceptor {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void apply(RequestTemplate template) {
        if(template==null)return;
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes();
        if(attributes==null){
            template.header("ticket", CommonConstants.INNER_FEIGN_TRANS_TICKET);
            return;
        }
        HttpServletRequest request = attributes.getRequest();
        if(request==null){
            return;
        }
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String values = request.getHeader(name);
                //使用body,请求头的content-length与body不一致,所以会出现too many bytes written executing错误,跳过它即可
                /*if(name.equals("content-length")){
                    continue;
                }*/
                template.header(name, values);

            }
        }
        Enumeration<String> bodyNames = request.getParameterNames();
        if (bodyNames != null) {
            while (bodyNames.hasMoreElements()) {
                String name = bodyNames.nextElement();
                String values = request.getParameter(name);
                template.header(name, values);
            }
        }
    }
}

另外一个微服务中

java 复制代码
 @ApiOperation(value = "订单下单用户模糊查询", notes = "订单下单用户模糊查询", httpMethod = "GET")
    @GetMapping(value="/getOrderUserName")
    public List<SysUserVo> getOrderUserName(@RequestParam(value ="username", required = false) String username){
        return sysUserService.getOrderUserName(username);
    }
相关推荐
考虑考虑19 小时前
Jpa使用union all
java·spring boot·后端
稻草人22221 天前
java Excel 导出 ,如何实现八倍效率优化,以及代码分层,方法封装
后端·架构
数据智能老司机1 天前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
bobz9651 天前
k8s svc 实现的技术演化:iptables --> ipvs --> cilium
架构
云舟吖1 天前
基于 electron-vite 实现一个 RPA 网页自动化工具
前端·架构
阿杆1 天前
同事嫌参数校验太丑,我直接掏出了更优雅的 SpEL Validator
java·spring boot·后端
brzhang1 天前
当AI接管80%的执行,你“不可替代”的价值,藏在这20%里
前端·后端·架构
Lei活在当下2 天前
【业务场景架构实战】4. 支付状态分层流转的设计和实现
架构·android jetpack·响应式设计
昵称为空C2 天前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端