微服务之间Feign调用

需使用的服务

@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);
   }
@Slf4j
@Service
public class SysUserServiceFallback implements SysUserService {

    @Override
    public List<SysUserVo> getOrderUserName(String username) {
        log.error("调用getOrderUserName方法异常,参数:{}", username);
        return null;
    }
}
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);
            }
        }
    }
}

另外一个微服务中

 @ApiOperation(value = "订单下单用户模糊查询", notes = "订单下单用户模糊查询", httpMethod = "GET")
    @GetMapping(value="/getOrderUserName")
    public List<SysUserVo> getOrderUserName(@RequestParam(value ="username", required = false) String username){
        return sysUserService.getOrderUserName(username);
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud微服务架构中,Nacos是一个注册中心和配置中心。Feign是一个声明式的Web服务客户端,它使得编写Web服务客户端变得更加容易。 使用Feign调用接口需要以下步骤: 1. 在pom.xml中添加Feign依赖 ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 在启动类上添加@EnableFeignClients注解启用Feign客户端 ```java @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 3. 创建接口,并使用@FeignClient注解指定调用的服务名称和服务路径 ```java @FeignClient(name = "service-provider") public interface UserService { @GetMapping("/user/{id}") String getUserById(@PathVariable("id") Long id); } ``` 其中,name属性指定服务名称,GetMapping注解指定服务路径。 4. 在需要使用该服务的地方注入UserService并调用方法即可 ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/user/{id}") public String getUserById(@PathVariable("id") Long id) { return userService.getUserById(id); } } ``` 在这个例子中,我们定义了一个名为UserService的Feign客户端,指定了调用的服务名称和服务路径。然后在UserController中注入了UserService并调用了其方法。最终,Feign会自动将该请求转发到名为service-provider的微服务,并返回结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值