forked from shaogezhu/easy-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
### Java template | ||
# Compiled class file | ||
*.class | ||
|
||
# Log file | ||
*.log | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.nar | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
|
||
### Example user template | ||
# IntelliJ project files | ||
.idea | ||
*.iml | ||
out | ||
gen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>easy-rpc</artifactId> | ||
<groupId>com.shaogezhu</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>easy-rpc-core</artifactId> | ||
|
||
<properties> | ||
<netty.version>4.1.6.Final</netty.version> | ||
<fastjson.version>1.2.29</fastjson.version> | ||
<jboss-marshalling-river.version>1.4.11.Final</jboss-marshalling-river.version> | ||
<jboss-marshalling-serial.version>1.4.11.Final</jboss-marshalling-serial.version> | ||
<slf4j-api.version>1.7.13</slf4j-api.version> | ||
<javassist.version>3.21.0-GA</javassist.version> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.javassist</groupId> | ||
<artifactId>javassist</artifactId> | ||
<version>${javassist.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.netty</groupId> | ||
<artifactId>netty-all</artifactId> | ||
<version>${netty.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>${slf4j-api.version}</version> | ||
</dependency> | ||
|
||
<!--fastjson序列化工具--> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
<version>${fastjson.version}</version> | ||
</dependency> | ||
|
||
<!--JDK自带的序列化 接收方工具--> | ||
<dependency> | ||
<groupId>org.jboss.marshalling</groupId> | ||
<artifactId>jboss-marshalling-river</artifactId> | ||
<version>${jboss-marshalling-river.version}</version> | ||
</dependency> | ||
|
||
<!--JDK自带的序列化 处理工具--> | ||
<dependency> | ||
<groupId>org.jboss.marshalling</groupId> | ||
<artifactId>jboss-marshalling-serial</artifactId> | ||
<version>${jboss-marshalling-serial.version}</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
48 changes: 48 additions & 0 deletions
48
easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/client/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.shaogezhu.easy.rpc.core.client; | ||
|
||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
import io.netty.handler.codec.string.StringDecoder; | ||
import io.netty.handler.codec.string.StringEncoder; | ||
|
||
/** | ||
* @Author peng | ||
* @Date 2023/2/23 22:48 | ||
*/ | ||
public class Client { | ||
|
||
public ChannelFuture startClientApplication() throws InterruptedException { | ||
NioEventLoopGroup clientGroup = new NioEventLoopGroup(); | ||
Bootstrap bootstrap = new Bootstrap(); | ||
bootstrap.group(clientGroup); | ||
bootstrap.channel(NioSocketChannel.class); | ||
bootstrap.handler(new ChannelInitializer<SocketChannel>() { | ||
@Override | ||
protected void initChannel(SocketChannel ch) throws Exception { | ||
ch.pipeline().addLast(new StringEncoder()); | ||
ch.pipeline().addLast(new StringDecoder()); | ||
ch.pipeline().addLast(new ClientHandler()); | ||
} | ||
}); | ||
|
||
ChannelFuture channelFuture = bootstrap.connect("localhost", 8010).sync(); | ||
System.out.println("========== Client start success =========="); | ||
return channelFuture; | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
Client client = new Client(); | ||
ChannelFuture channelFuture = client.startClientApplication(); | ||
for (int i = 100; i < 999; ++i){ | ||
Thread.sleep(1000); | ||
String msg = i+":msg from client."; | ||
channelFuture.channel().writeAndFlush(Unpooled.copiedBuffer(msg.getBytes())); | ||
} | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/client/ClientHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.shaogezhu.easy.rpc.core.client; | ||
|
||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
import io.netty.util.ReferenceCountUtil; | ||
|
||
/** | ||
* @Author peng | ||
* @Date 2023/2/23 22:51 | ||
*/ | ||
public class ClientHandler extends ChannelInboundHandlerAdapter { | ||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
System.out.printf("[client] 接收到的数据为: %s \n", msg.toString()); | ||
ReferenceCountUtil.release(msg); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | ||
super.exceptionCaught(ctx, cause); | ||
Channel channel = ctx.channel(); | ||
if(channel.isActive()){ | ||
ctx.close(); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/server/Server.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.shaogezhu.easy.rpc.core.server; | ||
|
||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelOption; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
import io.netty.handler.codec.string.StringDecoder; | ||
import io.netty.handler.codec.string.StringEncoder; | ||
|
||
/** | ||
* @Author peng | ||
* @Date 2023/2/23 22:21 | ||
*/ | ||
public class Server { | ||
|
||
public void startServerApplication() throws InterruptedException { | ||
NioEventLoopGroup bossGroup = new NioEventLoopGroup(); | ||
NioEventLoopGroup workerGroup = new NioEventLoopGroup(); | ||
ServerBootstrap bootstrap = new ServerBootstrap(); | ||
bootstrap.group(bossGroup, workerGroup); | ||
bootstrap.channel(NioServerSocketChannel.class); | ||
bootstrap.option(ChannelOption.TCP_NODELAY, true); | ||
bootstrap.option(ChannelOption.SO_BACKLOG, 1024); | ||
bootstrap.option(ChannelOption.SO_SNDBUF, 16*1024) | ||
.option(ChannelOption.SO_RCVBUF, 16*1024) | ||
.option(ChannelOption.SO_KEEPALIVE, true); | ||
|
||
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { | ||
@Override | ||
protected void initChannel(SocketChannel ch) throws Exception { | ||
ch.pipeline().addLast(new StringEncoder()); | ||
ch.pipeline().addLast(new StringDecoder()); | ||
ch.pipeline().addLast(new ServerHandler()); | ||
} | ||
}); | ||
|
||
bootstrap.bind(8010).sync(); | ||
System.out.println("========== Server start success =========="); | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
Server server = new Server(); | ||
server.startServerApplication(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/server/ServerHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.shaogezhu.easy.rpc.core.server; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.Channel; | ||
import io.netty.channel.ChannelHandlerContext; | ||
import io.netty.channel.ChannelInboundHandlerAdapter; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* @Author peng | ||
* @Date 2023/2/23 22:33 | ||
*/ | ||
public class ServerHandler extends ChannelInboundHandlerAdapter { | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
System.out.println("[Server] 接收到请求数据"+ msg.toString()); | ||
ctx.writeAndFlush(Unpooled.copiedBuffer("Hello client from server".getBytes(StandardCharsets.UTF_8))); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | ||
cause.printStackTrace(); | ||
Channel channel = ctx.channel(); | ||
if (channel.isActive()) { | ||
ctx.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.shaogezhu</groupId> | ||
<artifactId>easy-rpc</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<modules> | ||
<module>easy-rpc-core</module> | ||
</modules> | ||
|
||
<packaging>pom</packaging> | ||
|
||
<description>一款轻量级的Java RPC框架</description> | ||
|
||
</project> |