Skip to content

Commit 8200afb

Browse files
authored
simplified gRPC impl using netty (#2914)
1 parent 2858f6e commit 8200afb

30 files changed

+1967
-0
lines changed

labs/arthas-grpc-server/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Arthas Grpc
2+
3+
这个模块提供了一个轻量级的 Grpc 实现,目前任在开发中

labs/arthas-grpc-server/pom.xml

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<parent>
7+
<artifactId>arthas-all</artifactId>
8+
<groupId>com.taobao.arthas</groupId>
9+
<version>${revision}</version>
10+
<relativePath>../../pom.xml</relativePath>
11+
</parent>
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>arthas-grpc-server</artifactId>
14+
<name>arthas-grpc-server</name>
15+
<url>https://github.com/alibaba/arthas</url>
16+
17+
<properties>
18+
<maven.compiler.source>8</maven.compiler.source>
19+
<maven.compiler.target>8</maven.compiler.target>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<grpc.version>1.46.0</grpc.version>
22+
</properties>
23+
24+
<dependencyManagement>
25+
<dependencies>
26+
<dependency>
27+
<groupId>io.grpc</groupId>
28+
<artifactId>grpc-bom</artifactId>
29+
<version>${grpc.version}</version>
30+
<type>pom</type>
31+
<scope>import</scope>
32+
</dependency>
33+
</dependencies>
34+
</dependencyManagement>
35+
36+
<dependencies>
37+
38+
<!-- https://mvnrepository.com/artifact/io.netty/netty-codec-http2 -->
39+
<dependency>
40+
<groupId>io.netty</groupId>
41+
<artifactId>netty-codec-http2</artifactId>
42+
<version>4.1.72.Final</version>
43+
</dependency>
44+
<!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
45+
<dependency>
46+
<groupId>com.google.protobuf</groupId>
47+
<artifactId>protobuf-java</artifactId>
48+
<version>3.19.2</version>
49+
</dependency>
50+
51+
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
52+
<dependency>
53+
<groupId>org.slf4j</groupId>
54+
<artifactId>slf4j-api</artifactId>
55+
<version>2.0.12</version>
56+
</dependency>
57+
58+
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
59+
<dependency>
60+
<groupId>ch.qos.logback</groupId>
61+
<artifactId>logback-classic</artifactId>
62+
<version>1.5.0</version>
63+
</dependency>
64+
65+
<dependency>
66+
<groupId>com.taobao.arthas</groupId>
67+
<artifactId>arthas-common</artifactId>
68+
<version>${project.version}</version>
69+
</dependency>
70+
71+
72+
<!-- 测试用 -->
73+
<dependency>
74+
<groupId>io.grpc</groupId>
75+
<artifactId>grpc-netty</artifactId>
76+
<scope>provided</scope>
77+
<exclusions>
78+
<exclusion>
79+
<groupId>io.netty</groupId>
80+
<artifactId>netty-codec-http2</artifactId>
81+
</exclusion>
82+
</exclusions>
83+
</dependency>
84+
<dependency>
85+
<groupId>io.grpc</groupId>
86+
<artifactId>grpc-services</artifactId>
87+
<scope>provided</scope>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.junit.vintage</groupId>
91+
<artifactId>junit-vintage-engine</artifactId>
92+
<scope>test</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.junit.jupiter</groupId>
96+
<artifactId>junit-jupiter</artifactId>
97+
<scope>test</scope>
98+
</dependency>
99+
100+
<dependency>
101+
<groupId>javax.annotation</groupId>
102+
<artifactId>javax.annotation-api</artifactId>
103+
<version>1.3.2</version>
104+
<scope>provided</scope>
105+
<optional>true</optional>
106+
</dependency>
107+
<dependency>
108+
<groupId>com.alibaba.arthas</groupId>
109+
<artifactId>arthas-repackage-logger</artifactId>
110+
</dependency>
111+
<dependency>
112+
<groupId>ch.qos.logback</groupId>
113+
<artifactId>logback-classic</artifactId>
114+
</dependency>
115+
<dependency>
116+
<groupId>ch.qos.logback</groupId>
117+
<artifactId>logback-core</artifactId>
118+
</dependency>
119+
<dependency>
120+
<groupId>org.slf4j</groupId>
121+
<artifactId>slf4j-api</artifactId>
122+
</dependency>
123+
</dependencies>
124+
125+
126+
<build>
127+
<plugins>
128+
<plugin>
129+
<groupId>org.xolstice.maven.plugins</groupId>
130+
<artifactId>protobuf-maven-plugin</artifactId>
131+
<version>0.6.1</version>
132+
<configuration>
133+
<protoSourceRoot>${basedir}/src/main/proto</protoSourceRoot>
134+
<protocArtifact>com.google.protobuf:protoc:3.11.0:exe:${os.detected.classifier}</protocArtifact>
135+
<pluginId>grpc-java</pluginId>
136+
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.28.0:exe:${os.detected.classifier}</pluginArtifact>
137+
</configuration>
138+
<executions>
139+
<execution>
140+
<goals>
141+
<goal>compile</goal>
142+
<goal>compile-custom</goal>
143+
</goals>
144+
</execution>
145+
</executions>
146+
</plugin>
147+
</plugins>
148+
<extensions>
149+
<extension>
150+
<groupId>kr.motd.maven</groupId>
151+
<artifactId>os-maven-plugin</artifactId>
152+
<version>1.4.1.Final</version>
153+
</extension>
154+
</extensions>
155+
</build>
156+
157+
<profiles>
158+
<profile>
159+
<id>mac</id>
160+
<activation>
161+
<os>
162+
<family>mac</family>
163+
</os>
164+
</activation>
165+
<properties>
166+
<os.detected.classifier>osx-x86_64</os.detected.classifier>
167+
</properties>
168+
</profile>
169+
</profiles>
170+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.taobao.arthas.grpc.server;
2+
3+
/**
4+
* @author: FengYe
5+
* @date: 2024/10/13 02:40
6+
* @description: ArthasGrpcServerBootstrap
7+
*/
8+
public class ArthasGrpcBootstrap {
9+
public static void main(String[] args) {
10+
ArthasGrpcServer arthasGrpcServer = new ArthasGrpcServer(9090, null);
11+
arthasGrpcServer.start();
12+
}
13+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.taobao.arthas.grpc.server;
2+
3+
import com.alibaba.arthas.deps.ch.qos.logback.classic.Level;
4+
import com.alibaba.arthas.deps.ch.qos.logback.classic.LoggerContext;
5+
import com.alibaba.arthas.deps.org.slf4j.Logger;
6+
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;
7+
import com.taobao.arthas.grpc.server.handler.GrpcDispatcher;
8+
import com.taobao.arthas.grpc.server.handler.Http2Handler;
9+
import com.taobao.arthas.grpc.server.handler.executor.GrpcExecutorFactory;
10+
import io.netty.bootstrap.ServerBootstrap;
11+
import io.netty.channel.Channel;
12+
import io.netty.channel.ChannelInitializer;
13+
import io.netty.channel.ChannelOption;
14+
import io.netty.channel.EventLoopGroup;
15+
import io.netty.channel.nio.NioEventLoopGroup;
16+
import io.netty.channel.socket.SocketChannel;
17+
import io.netty.channel.socket.nio.NioServerSocketChannel;
18+
import io.netty.handler.codec.http2.Http2FrameCodecBuilder;
19+
import io.netty.util.concurrent.DefaultEventExecutorGroup;
20+
import io.netty.util.concurrent.EventExecutorGroup;
21+
22+
import java.lang.invoke.MethodHandles;
23+
24+
/**
25+
* @author: FengYe
26+
* @date: 2024/7/3 上午12:30
27+
* @description: ArthasGrpcServer
28+
*/
29+
public class ArthasGrpcServer {
30+
31+
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass().getName());
32+
33+
private int port = 9090;
34+
35+
private String grpcServicePackageName;
36+
37+
public ArthasGrpcServer(int port, String grpcServicePackageName) {
38+
this.port = port;
39+
this.grpcServicePackageName = grpcServicePackageName;
40+
}
41+
42+
public void start() {
43+
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
44+
EventLoopGroup workerGroup = new NioEventLoopGroup(10);
45+
46+
GrpcDispatcher grpcDispatcher = new GrpcDispatcher();
47+
grpcDispatcher.loadGrpcService(grpcServicePackageName);
48+
GrpcExecutorFactory grpcExecutorFactory = new GrpcExecutorFactory();
49+
grpcExecutorFactory.loadExecutor(grpcDispatcher);
50+
51+
try {
52+
ServerBootstrap b = new ServerBootstrap();
53+
b.group(bossGroup, workerGroup)
54+
.channel(NioServerSocketChannel.class)
55+
.option(ChannelOption.SO_BACKLOG, 1024)
56+
.childHandler(new ChannelInitializer<SocketChannel>() {
57+
@Override
58+
public void initChannel(SocketChannel ch) {
59+
ch.pipeline().addLast(Http2FrameCodecBuilder.forServer().build());
60+
ch.pipeline().addLast(new Http2Handler(grpcDispatcher, grpcExecutorFactory));
61+
}
62+
});
63+
Channel channel = b.bind(port).sync().channel();
64+
logger.info("ArthasGrpcServer start successfully on port: {}", port);
65+
channel.closeFuture().sync();
66+
} catch (InterruptedException e) {
67+
logger.error("ArthasGrpcServer start error", e);
68+
} finally {
69+
bossGroup.shutdownGracefully();
70+
workerGroup.shutdownGracefully();
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)