springBoot集成RPC
需求 : 项目开发到尾期,仓库系统需要对接我们这边的制造系统,
为的是制造系统所使用物料时,需向仓库系统发送请求物料信息,所以需要调用
仓库接口.
使用技术: 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 依赖

<?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>
注意发送数据格式如下

成功接收

springBoot集成RPC的更多相关文章
- RabbitMQ学习笔记(一):安装及Springboot集成
前言 MQ,即消息队列Message Queue的缩写. RabbitMQ 是MQ的一种,就像招商银行是银行的一种一样.主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用. 消息 ...
- dubbo实战之二:与SpringBoot集成
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 国产时序数据库IotDB安装、与SpringBoot集成
一.简介: 本文将完成一个真实业务中的设备上报数据的一个例子,完整的展示后台服务接收到设备上报的数据后,将数据添加到时序数据库,并且将数据查询出来的一个例子.本文所有代码已经上传GitHub:http ...
- 【springBoot】springBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- SpringBoot集成security
本文就SpringBoot集成Security的使用步骤做出解释说明.
- springboot集成Actuator
Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...
- SpringBoot集成Shiro并用MongoDB做Session存储
之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...
- SpringBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
随机推荐
- STL-RBT_map,set模拟实现
set #include"26RBT_container.h" namespace test { //set通过普通迭代器使用const迭代器实现限制key不能被修改 templa ...
- STL-string模拟实现
1 #pragma once 2 3 #include<iostream> 4 #include<string.h> 5 #include<assert.h> 6 ...
- $help console 里面的入口帮助文档
$help console 里面的入口帮助文档 Object.defineProperty(window, '$help', { get: function() { // 这里面this是window ...
- 【深度学习基础】基于Numpy的循环神经网络实现和反向传播训练
本文是<深度学习进阶:自然语言处理>.<神经网络与深度学习>和<动手学深度学习>的读书笔记.本文将介绍基于Numpy的循环神经网络的前向传播和反向传播实现,包括RN ...
- libwebsockets支持外部eventloop变更
早些年还在使用2.4+版本,现在最新版已经到4.1+,centos 7也使用3.+版本.对于使用外部eventloop相关的接口发生了大的变更.libev也应为早早对iouring支持,4+版本亲睐l ...
- stable diffusion安装及使用
最近在看这个,踩了一些坑,现在有点头绪了,记录如下 安装很简单,直接git下载下来,然后进到根目录执行./web-ui.py,第一次启动你没有模型他会帮你下载个默认模型,我有模型了,就先把这个模型放到 ...
- Linux环境下Minio的安装部署与启动教程(完整版)
1.概述 MinIO是一个开源.分布式的对象存储系统,专为云原生环境设计.它提供了一个基于标准的Amazon S3兼容接口,使得开发者可以使用熟悉的API在私有云或边缘环境中部署和管理大规模非结构化数 ...
- Unity中文字体获取
Unity对中文字体支持较差,这里给出了几个可以使用的中文字体下载网站 后续会更新如何使用: 51Font -「免费字体发布平台」为您第一时间发布商用免费字体开源字体 (17font.com) fre ...
- nginx 自定义日志格式输出
修改 nginx.conf 自定义日志格式.路径 log_format my_format '$remote_addr $msec $http_host $request_uri'; 使用精准配准,对 ...
- 基于proteus的数字电路设计
基于proteus的数字电路设计 1.实验原理 proteus的数字电路仿真能力还是比较强大的.这里总结一下proteus的几个基本操作以备后用.大致包括74hc系列的使用.常用调试设备.仿真开关.器 ...