远程调用-OpenFeign(二)

目录

1.OpenFeign最佳实践

1.1Feign继承方式

 1.1.1创建一个Module

1.1.2引入依赖

1.1.3编写接口

1.1.4打Jar包

1.1.5服务提供方实现接口

1.1.6服务消费方继承接口

1.1.7测试

1.2Feign抽取方式

1.2.1创建一个Module

1.2.2引入依赖

1.2.3编写API

1.2.4打Jar包

1.2.5服务消费方使用product-api

2.服务部署


承接上文:远程调用-OpenFeign(一)-CSDN博客

1.OpenFeign最佳实践

通过观察, 我们也能看出来, Feign的客户端与服务提供者的controller代码非常相似
Feign客户端:
 @FeignClient(value = "product-service", path = "/product")
 public interface ProductApi {
     @RequestMapping("/{productId}")
     ProductInfo getProductById(@PathVariable("productId") Integer productId);
 }

服务提供方Controller:

 @RequestMapping("/product")
 @RestController
 public class ProductController {

     @RequestMapping("/{productId}")
     public ProductInfo getProductById(@PathVariable("productId") Integer productId){
         //...
     }
 }

 有没有一种方法可以简化这种写法呢?

1.1Feign继承方式

Feign支持继承的方式,我们可以把一些常见的操作封装到接口里

我们定义好一个接口,由服务提供方实现,消费方编写Feign接口时,直接继承这个接口

具体参考:Spring Cloud OpenFeign Features :: Spring Cloud Openfeign

 1.1.1创建一个Module

接口可以放在一个公共的Jar包里,供服务提供方和服务消费方使用

1.1.2引入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

1.1.3编写接口

复制ProductApi,ProductInfo到product-api模块中

import com.example.productapi.model.ProductInfo;

import org.springframework.web.bind.annotation.*;

public interface ProductInterface {
    @RequestMapping("/{productId}")
    ProductInfo getProductById(@PathVariable("productId") Integer productId);
}

目录结构如下:

1.1.4打Jar包

通过Maven的install打包

观察本地Maven仓库,Jar包是否打包成功

1.1.5服务提供方实现接口

服务提供方实现接口 ProductInterface
@RequestMapping("/product")
@RestController
public class ProductController implements ProductInterface {
    @Autowired
    private ProductService productService;

    @RequestMapping("/{productId}")
    public ProductInfo getProductById(@PathVariable("productId") Integer productId){
        return productService.selectProductById(productId);
    }
}

1.1.6服务消费方继承接口

服务消费方继承ProductInterface

@FeignClient(value = "product-service",path = "/product")
public interface ProductApi extends ProductInterface {
}

1.1.7测试

测试远程调用:http://127.0.0.1:8080/feign/1001

1.2Feign抽取方式

官方推荐Feign的使用方式为继承的方式,但在企业开发中,更多的是把Feign接口抽取作为一个独立的模块,其做法与继承类似,但是理念不同

操作方法:

将Feign的Client抽取作为一个独立的模块,并把涉及到的实体类等都放在这个模块中,打成一个Jar包,服务消费方只需要依赖该Jar包即可,Jar包通常由服务提供方实现

1.2.1创建一个Module

1.2.2引入依赖

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

1.2.3编写API

复制ProductApi,ProductInfo到product-api模块中

1.2.4打Jar包

通过Maven打包

1.2.5服务消费方使用product-api

1.删除ProductApi、ProductInfo

2.引入依赖

 <dependency>
     <groupId>org.example</groupId>
     <artifactId>product-api</artifactId>
     <version>1.0-SNAPSHOT</version>
 </dependency>

修改项目中ProductApi、ProductInfo的路径为product-api的路径

3.指定扫描类:ProductApi

在启动类添加扫描路径

 @EnableFeignClients(basePackages = {"com.bite.api"})

完整代码如下

 @EnableFeignClients(basePackages = {"com.productapi.api"})
 @SpringBootApplication
 public class OrderServiceApplication {
     public static void main(String[] args) {
         SpringApplication.run(OrderServiceApplication.class, args);
     }
 }

也可以指定需要加载的Feign客户端

@EnableFeignClients(clients = {ProductApi.class})

1.2.6测试

测试远程调用: http://127.0.0.1:8080/order/1

2.服务部署

1.修改数据库,Nacos等相关配置

2.对两个服务进行打包

Maven打包默认是从远程仓库下载的,product-api这个包在本地,有以下解决方案:

  1. 上传到Maven中央仓库
  2. 搭建Maven私服,上传Jar包到私服
  3. 从本地读取Jar包(推荐)

这里咱们使用第三种方式

修改pom文件

   <dependencies> 
       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>product-api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>D:/JavaWebSource/mvn_repository/com/example/product-api/0.0.1-SNAPSHOT/product-api-0.0.1-SNAPSHOT.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>

 其中systemPath是本地的Jar包路径

  • 22
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值