栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Feign~服务调用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Feign~服务调用

目录

1 前言

2 Feign

2.1 什么是feign

2.2 Feign的使用

1 前言

之前我们在服务中用订单微服务调用了商品微服务,这种远程服务,我们使用的是restTemplate。但是在我们的微服务中肯定有提供远程调用的组件,像feign和openFeign。今天我们先简单了解下feign的简单使用。

2 Feign

2.1 什么是feign

Feign是SpringCloud提供的一个声明式的伪Http客户端,它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口并且添加一个注解即可。

2.2 Feign的使用

1)feign的依赖


    org.springframework.cloud
    spring-cloud-starter-openfeign

2)在启动类上加上@EnableFeignClients注解

3)在调用方添加接口

 比如:之前是在订单微服务中调用商品微服务的接口,采用的是如下的方式。

现在在订单微服务中,添加接口。如下:

import com.liubujun.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;



@FeignClient(value = "server-product") // value为用于指定调用nacos下哪个微服务
public interface ProductService {

    
    @RequestMapping("/product/{pid}")
    Product findById(@PathVariable Integer pid);
}

4)启动nacos,重启服务.访问 http://localhost:8091/order/prod/2成功。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/856699.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号