jsonrpc一个简单的 Java RPC 组件
jsonrpc 是一个简单的 java rpc 组件, 部分代码参考:https://github.com/RitwikSaikia/jsonrpc 这部分代码版权归 RitwikSaikia 所有。服务端缓存、客服端缓存、注解支持,spring 支持等版权归 ada.young 所有。
maven使用
<dependency> <groupId>com.quhaodian.jsonrpc</groupId> <artifactId>core</artifactId> <version>1.01</version> </dependency>
使用方法
-
定义接口
@RestFul(api=Calculator.class,value="calculator") public interface Calculator { double add(double x, double y); double multiply(double x, double y); }
-
实现接口
public class SimpleCalculatorImpl implements Calculator { public double add(double x, double y) { return x + y; } public double multiply(double x, double y) { return x * y; } }
-
通过与 spring 集成,配置 servlet 。
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>rpc</servlet-name> <servlet-class>com.quhaodian.servlet.RpcServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>rpc</servlet-name> <url-pattern>/rpc</url-pattern> </servlet-mapping>
-
客服端访问
String url="远程服务端servlet地址" Calculator s = RestFulClient.getService(url, Calculator .class); double result = calc.add(1.2, 7.5);
评论