需求 : 项目开发到尾期,仓库系统需要对接我们这边的制造系统,

为的是制造系统所使用物料时,需向仓库系统发送请求物料信息,所以需要调用

仓库接口.

使用技术: RPC

数据传输格式: json

开发环境:springboot

1.如何向仓库发送数据?

注意:发送的数据格式为json  例如 [{"sex":"nan","name":"1111"}] 对象外面需要 [] 直接上代码 

这里转换json 其实有很多种方法 只要是 [{}]  array格式即可

1.1发送数据

@Test
    void contexts() throws Throwable {

        Product product = new Product();
        product.setName("1111");
        product.setSex("nan");
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(JSONUtil.parse(product));
        RpcClientUtil.postJsonRPC("http://127.0.0.1:8888/acceptSigned", jsonArray,"acceptSigned");
    }

1.2 RpcClientUtil.java  工具类方便发送数据自定义封装即可

import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import java.net.URL;

/**
 * address:要送数据到哪个地址
 * params : json数据
 * methodName: 接口的方法名字
 */
public class RpcClientUtil {
    public static String postJsonRPC(String address, Object params, String methodName) throws Throwable {
        URL url = null;
        JsonRpcHttpClient jsonRpcHttpClient = null;
        String result = null;
        try {
            url = new URL(address);
            jsonRpcHttpClient = new JsonRpcHttpClient(url);
            result = jsonRpcHttpClient.invoke(methodName, params, String.class);
        } catch (Exception e) {
            throw e;
        }  finally {
            url = null;
            jsonRpcHttpClient = null;
        }
        return result;
    }

}

2.仓库如何接收数据?

需要编写接口 @JsonRpcService(value = "acceptSigned")   acceptSigned 远程地址拼接用的

例如 http://127.0.0.1:8888/acceptSigned 

2.1接口AcceptSignedAPI.java
package com.example.rpctest.service;

import com.googlecode.jsonrpc4j.JsonRpcService;

@JsonRpcService(value = "acceptSigned")
public interface AcceptSignedAPI {
    public void acceptSigned(Object o);
}

  2.2 接口实现类 注意使用 @AutoJsonRpcServiceImpl   方法名为 acceptSigned

package com.example.rpctest.service.impl;

import com.example.rpctest.service.AcceptSignedAPI;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.springframework.stereotype.Service;

@Service
@AutoJsonRpcServiceImpl
public class AcceptSignedImpl implements AcceptSignedAPI {

    @Override
    public void acceptSigned(Object o) {
        System.out.println("222"+o);
    }
}

maven 依赖

spring boot rpc框架 springboot rpc调用_json

spring boot rpc框架 springboot rpc调用_spring_02

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>rpctest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rpctest</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--rpc-->
        <dependency>
            <groupId>com.github.briandilley.jsonrpc4j</groupId>
            <artifactId>jsonrpc4j</artifactId>
            <version>1.5.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-core -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.6.6</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.72</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.10</version>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

View Code

 注意发送数据格式如下

spring boot rpc框架 springboot rpc调用_maven_03

 

 成功接收

spring boot rpc框架 springboot rpc调用_spring_04