异构微服务远程调用如何打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}
相关推荐
团儿.3 小时前
解锁MySQL高可用新境界:深入探索MHA架构的无限魅力与实战部署
数据库·mysql·架构·mysql之mha架构
王彬泽3 小时前
【微服务】服务注册与发现、分布式配置管理 - Nacos
微服务·服务注册与发现·分布式配置管理
艾伦~耶格尔12 小时前
Spring Boot 三层架构开发模式入门
java·spring boot·后端·架构·三层架构
攸攸太上13 小时前
Spring Gateway学习
java·后端·学习·spring·微服务·gateway
hhzz14 小时前
Linux Shell编程快速入门以及案例(Linux一键批量启动、停止、重启Jar包Shell脚本)
android·linux·jar
_.Switch15 小时前
Python机器学习框架介绍和入门案例:Scikit-learn、TensorFlow与Keras、PyTorch
python·机器学习·架构·tensorflow·keras·scikit-learn
一直在进步的派大星16 小时前
Docker 从安装到实战
java·运维·docker·微服务·容器
神一样的老师1 天前
构建5G-TSN测试平台:架构与挑战
5g·架构
huaqianzkh1 天前
付费计量系统通用功能(13)
网络·安全·架构
Gogeof1 天前
云原生化 - 基础镜像(简约版)
微服务·云原生·基础镜像