微服务之间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);
    }
相关推荐
JXNL@14 分钟前
电池管理系统(BMS)核心架构与 AFE 选型全解析
架构
喵了几个咪2 小时前
选择第三方IAM还是自建权限体系?中小型后台系统权限架构决策指南
数据库·oracle·架构
Sam_Deep_Thinking2 小时前
聊聊Java中的of
java·开发语言·架构
搭贝3 小时前
低代码+AI赋能文化传媒财务结算:搭贝平台技术架构与实战解析
人工智能·低代码·架构
爱吃羊的老虎4 小时前
【JAVA】python转java:Spring Boot 入门
java·spring boot·python
weixin_397574094 小时前
Agent OS治理平台:资源平面、执行平面与控制平面的架构
人工智能·平面·架构
_qingche5 小时前
H2 数据库到 MySQL 数据迁移
java·数据库·spring boot·mysql·spring·重构·kotlin
@PHARAOH6 小时前
WHAT - NextAuth 权限认证机制
前端·微服务·服务端
wb043072017 小时前
前厅翻修记——从阿明的“8 秒点餐页“,看前端工程化与用户体验的全面升级
前端·架构·ux
葡萄城技术团队7 小时前
【SpreadJS 新版本特性揭秘】从“单元格”到“结构化数据”——V19.1 企业级多端协同架构演进
架构