微服务之间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);
    }
相关推荐
2401_857439691 小时前
SpringBoot框架在资产管理中的应用
java·spring boot·后端
怀旧6661 小时前
spring boot 项目配置https服务
java·spring boot·后端·学习·个人开发·1024程序员节
洛卡卡了1 小时前
从单层到 MVC,再到 DDD:架构演进的思考与实践
架构·mvc
乌恩大侠2 小时前
O-RAN Fronthual CU/Sync/Mgmt 平面和协议栈
5g·平面·fpga开发·架构
哎呦没4 小时前
SpringBoot框架下的资产管理自动化
java·spring boot·后端
2401_857600954 小时前
SpringBoot框架的企业资产管理自动化
spring boot·后端·自动化
NiNg_1_2348 小时前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
种树人202408198 小时前
如何在 Spring Boot 中启用定时任务
spring boot
木宛哥9 小时前
代码背后的智慧:20条编程感悟
java·后端·架构
苹果醋311 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx