异构微服务远程调用如何打jar包

1.服务提供方打 jar 包

RemoteUserService.java

java 复制代码
package com.finance.system.api;

import com.finance.system.api.domain.dto.Enterprise;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.finance.common.core.constant.ServiceNameConstants;
import com.finance.common.core.domain.R;
import com.finance.system.api.factory.RemoteUserFallbackFactory;
import com.finance.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Map;

/**
 * 用户服务
 */
@FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = RemoteUserFallbackFactory.class)
public interface RemoteUserService
{
    /**
     * 同步公司数据
     */
    @PostMapping(value = "/company/registerEnterprise")
    public R registerEnterprise(@RequestBody Enterprise enterprise);
}
xml 复制代码
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <!-- 这里可以没有主类,作为工具包使用 -->
<!--                    <archive>-->
<!--                        <manifest>-->
<!--                            <mainClass>com.example.MainClass</mainClass>-->
<!--                        </manifest>-->
<!--                    </archive>-->
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Maven Assembly Plugin 是一个用于创建可执行的分发包的 Maven 插件。它可以将项目的依赖和资源文件打包成一个可执行的分发包,方便部署和使用。

当运行 mvn package 命令时,Maven Assembly Plugin 将会执行,并生成分发包。

完成配置后,您可以运行以下命令来生成分发包:

bash 复制代码
mvn package

生成的分发包将会位于项目的 target 目录下,后缀为 jar-with-dependencies.jar,这里是 finance-api-system-2.5.0-jar-with-dependencies.jar,里面已经包含了项目和它的所有依赖。

2.服务调用方引用 jar 包

xml 复制代码
<!-- 引用异构服务jar包 -->
<dependency>
    <groupId>com.finance</groupId>
    <artifactId>finance-api-system</artifactId>
    <version>2.5.0</version>
    <scope>system</scope>
    <systemPath>${basedir}/lib/finance-api-system-2.5.0-jar-with-dependencies.jar</systemPath>
</dependency>

调用方需扫描远程类所在路径

java 复制代码
@EnableFeignClients(basePackages = {"com.finance.system.api"})
java 复制代码
@Resource
private RemoteUserService remoteUserService;

R result = remoteUserService.registerEnterprise(enterprise);
log.info("result: {}", result);
// result: {msg=操作成功, code=200}
相关推荐
奋豆来袭4 小时前
wins中怎么用一个bat文件启动jar包和tomcat等多个服务
jar
小马哥编程4 小时前
【软考架构】第七章 系统架构设计基础知识-7.2基于架构的软件开发方法:Architecture-Based Software Design,ABSD
架构·系统架构
西陵5 小时前
Nx带来极致的前端开发体验——任务编排
前端·javascript·架构
LQ深蹲不写BUG8 小时前
微服务的保护方式以及Sentinel详解
微服务·云原生·架构
失散139 小时前
分布式专题——1.2 Redis7核心数据结构
java·数据结构·redis·分布式·架构
布列瑟农的星空9 小时前
大话设计模式——观察者模式和发布/订阅模式的区别
前端·后端·架构
鼠鼠我捏,要死了捏9 小时前
基于Apache Flink Stateful Functions的事件驱动微服务架构设计与实践指南
微服务·apache flink·实时处理
睡觉的时候不会困12 小时前
MySQL 高可用方案之 MHA 架构搭建与实践
数据库·mysql·架构
lssjzmn16 小时前
性能飙升!Spring异步流式响应终极指南:ResponseBodyEmitter实战与架构思考
java·前端·架构