Skip to content

Commit

Permalink
添加
Browse files Browse the repository at this point in the history
  • Loading branch information
upliveapp committed Mar 16, 2019
1 parent 2157278 commit 73fad31
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 11 deletions.
86 changes: 85 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,86 @@
# jsonrpc
simple rpc scheme

### simple rpc scheme

> 使用时,仅需要添加自己实现了slf4j-api的log框架,强烈推荐使用logback
<pre>
package com.github.xincao9.jsonrpc;

import com.github.xincao9.jsonrpc.client.JsonRPCClient;
import com.github.xincao9.jsonrpc.server.SyncMethod;
import com.github.xincao9.jsonrpc.server.JsonRPCServer;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
*
* @author xincao9@gmail.com
*/
public class JsonRPCServerTest {

public JsonRPCServerTest() {
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
}

@After
public void tearDown() {
}

public static class PingMethodImpl implements SyncMethod {

@Override
public Object exec(Request request) {
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
}
return request.getParams();
}

@Override
public String getName() {
return "ping";
}
}

@Test
public void testPingMethod() throws Throwable {
int port = RandomUtils.nextInt(1025, 65535);
JsonRPCServer jsonRPCServer = JsonRPCServer.defaultJsonRPCServer(port, 1, Runtime.getRuntime().availableProcessors());
jsonRPCServer.register(new PingMethodImpl());
jsonRPCServer.start();
JsonRPCClient jsonRPCClient = JsonRPCClient.defaultJsonRPCClient();
jsonRPCClient.start();
for (int no = 0; no < 5000; no++) {
String value = RandomStringUtils.randomAscii(128);
Request request = Request.createRequest(Boolean.TRUE, "ping", Collections.singletonList(value));
request.setHost("127.0.0.1");
request.setPort(port);
Response<List<Object>> response = jsonRPCClient.invoke(request);
System.out.println(JSONObject.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect));
}
jsonRPCClient.shutdown();
jsonRPCServer.shutdown();
}
}</pre>
84 changes: 74 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,129 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.xincao9</groupId>
<artifactId>jsonrpc</artifactId>
<version>1.0</version>
<version>1.0-RELEASE</version>
<packaging>jar</packaging>
<name>jsonrpc</name>
<description>SIMPLE JSON RPC</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<netty.version>4.1.25.Final</netty.version>
<fastjson.version>1.2.45</fastjson.version>
<slf4j.version>1.7.25</slf4j.version>
<logback.version>1.2.3</logback.version>
<commons-lang3.version>3.7</commons-lang3.version>
<commons-codec.version>1.11</commons-codec.version>
<junit.version>4.12</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
</dependencies>
<developers>
<developer>
<email>xincao9@gmail.com</email>
<name>xin.cao</name>
<roles>
<role>onwer</role>
</roles>
<url>https://github.com/xincao9</url>
</developer>
</developers>
</project>

0 comments on commit 73fad31

Please sign in to comment.