zoukankan      html  css  js  c++  java
  • 使用openfeign作为微服务之间的相互调用

    1 例如当前有微服务a和b,微服务a和b现在都成功注册到服务注册中心nacos,目前需要实现服务a需要调用服务b的需求,实现步骤如下:

    服务a引入openfeign的依赖:

    <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-openfeign</artifactId>

    </dependency>

    例如服务b中有个服务如下,a想调用这个服务:

    @RequestMapping("/memeber/list")

        public R couponOfMember(){

            CouponEntity entity=new CouponEntity();

            entity.setCouponName("满一百减十元");

            return R.ok().put("coupon",new CouponEntity());

        }

    在微服务a中编写一个接口,告诉springcloud这个接口需要调用远程服务

    @FeignClient(name ="b" )

    public interface CouponFeign {

        @RequestMapping("/shopcoupon/coupon/memeber/list")

        public R couponOfMember();

    }

    name ="b":b就是微服务b的名字,

      @RequestMapping("/shopcoupon/coupon/memeber/list")请求的地址必须和需要调用的服务的地址一样

    接下来在主程序入口使用注解@EnableFeignClients(basePackages ="com.fengbao.shop.shopmember.feign")开启远程服务的调用:

    @SpringBootApplication

    @MapperScan("com.fengbao.shop.shopmember.dao")

    @EnableDiscoveryClient

    @EnableFeignClients(basePackages = "com.fengbao.shop.shopmember.feign")

    public class ShopMemberApplication {

    public static void main(String[] args) {

    SpringApplication.run(ShopMemberApplication.class, args);

    }

    }

    最后比如我们需要在服务a的某个controller中需要调用这个服务按如下添加:

    @Autowired

    private CouponFeign couponFeign;

    然后就可以使用couponFeign来调用方法了

  • 相关阅读:
    .html(),.text()和.val()的差异总结:
    获取或设置checkbox radio select的值
    sublime 搜索时忽略文件夹
    转载------一小时包教会 —— webpack 入门指南
    转载--git教程
    转载--网站数据统计分析中的日志收集原理及其实现
    devexpress 安装及破解
    基于socket的客户端和服务端聊天简单使用 附Demo
    Ajax技术原理小结
    oracle 资源学习汇总
  • 原文地址:https://www.cnblogs.com/mibing/p/15150233.html
Copyright © 2011-2022 走看看