zoukankan      html  css  js  c++  java
  • Feign进行服务间调用

    Feign进行服务间调用

    • 前提:开启服务注册与发现

    1、导依赖(在消费端导入)

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>2.2.3.RELEASE</version>
    </dependency>
    

    2、开启Feign(消费端的启动类)

    @EnableFeignClients
    

    3、编写调用接口

    package com.wagn.s.feign;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.*;
    
    //name为在注册中心中的微服务名称
    @FeignClient(name = "server-oss",fallback = OssFeignCallback.class)
    @Component
    
    public interface OssFeign{
        @PostMapping("/oss/remoteTest")//这里为接口全路径
        public String remoteTest(@RequestBody String id);
    }
    
        //这是远程微服务的接口
        //@PostMapping("remoteTest")
        //public String remoteTest(@RequestBody String id){
        //    return "yes"+id;
        //}
    

    4、绑定失败回调类

    package com.wagn.s.feign;
    
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;
    
    @Component
    public class OssFeignCallback implements OssFeign {
        @Override
        public String remoteTest(String id) {
            return null;
        }
    }
    

    5、调用该接口实现远程服务调用

    package com.wagn.s.controller;
    
    
    import com.wagn.s.feign.OssFeign;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import org.springframework.web.bind.annotation.RestController;
    /**
     * <p>
     *  前端控制器
     * </p>
     *
     * @author testjava
     * @since 2020-09-12
     */
    @RestController
    @RequestMapping("/s/user-pwd")
    public class UserPwdController {
    
        @Autowired
        private OssFeign ossFeign;
    
        @PostMapping("remoteTest")
        public String remoteTest(@RequestBody String id){
            return ossFeign.remoteTest(id);
        }
    
    
    
    }
    
  • 相关阅读:
    IE8上传插件jquery-form.js上传请求参数设置type为post失效问题
    路径参数汉字兼容问题
    vue-cli2移动端适配
    事件委托原生、jQuery实现
    new Date()在移动端的问题
    create-react-app配置less
    删除左右两边空格
    日期转换
    Git 常用命令
    单页面应用
  • 原文地址:https://www.cnblogs.com/yizhixiang/p/13660358.html
Copyright © 2011-2022 走看看