You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2019/11/15 15:05:36 UTC
[servicecomb-pack] 04/09: SCB-1593 Support Native transports for
Linux (since 4.0.16) and MacOS/BSD (since 4.1.11)
This is an automated email from the ASF dual-hosted git repository.
ningjiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-pack.git
commit 70a9d03b3a9df13809eec41fcc58e571e64295fd
Author: Lei Zhang <zh...@apache.org>
AuthorDate: Fri Nov 15 16:03:29 2019 +0800
SCB-1593 Support Native transports for Linux (since 4.0.16) and MacOS/BSD (since 4.1.11)
---
.../pack/alpha/server/GrpcServerConfig.java | 7 +++
.../pack/alpha/server/GrpcStartable.java | 63 ++++++++++++++++++++--
.../src/main/resources/application.yaml | 1 +
3 files changed, 68 insertions(+), 3 deletions(-)
diff --git a/alpha/alpha-server/src/main/java/org/apache/servicecomb/pack/alpha/server/GrpcServerConfig.java b/alpha/alpha-server/src/main/java/org/apache/servicecomb/pack/alpha/server/GrpcServerConfig.java
index 7904b23..9f8911a 100644
--- a/alpha/alpha-server/src/main/java/org/apache/servicecomb/pack/alpha/server/GrpcServerConfig.java
+++ b/alpha/alpha-server/src/main/java/org/apache/servicecomb/pack/alpha/server/GrpcServerConfig.java
@@ -55,6 +55,9 @@ public class GrpcServerConfig {
@Value("${alpha.server.ssl.clientCert:client.crt}")
private String clientCert;
+ @Value("${alpha.feature.native:false}")
+ private boolean nettyTransport;
+
public String getHost() {
return host;
}
@@ -98,6 +101,10 @@ public class GrpcServerConfig {
public String getClientCert() {
return clientCert;
}
+
+ public boolean isNettyTransport() {
+ return nettyTransport;
+ }
}
diff --git a/alpha/alpha-server/src/main/java/org/apache/servicecomb/pack/alpha/server/GrpcStartable.java b/alpha/alpha-server/src/main/java/org/apache/servicecomb/pack/alpha/server/GrpcStartable.java
index 4760bac..20980c2 100644
--- a/alpha/alpha-server/src/main/java/org/apache/servicecomb/pack/alpha/server/GrpcStartable.java
+++ b/alpha/alpha-server/src/main/java/org/apache/servicecomb/pack/alpha/server/GrpcStartable.java
@@ -20,6 +20,12 @@
package org.apache.servicecomb.pack.alpha.server;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.ServerChannel;
+import io.netty.channel.epoll.EpollEventLoopGroup;
+import io.netty.channel.epoll.EpollServerSocketChannel;
+import io.netty.channel.kqueue.KQueueEventLoopGroup;
+import io.netty.channel.kqueue.KQueueServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.io.IOException;
@@ -103,9 +109,9 @@ public class GrpcStartable implements ServerStartable {
private ServerBuilder getServerBuilder(int port) {
return NettyServerBuilder.forAddress(
new InetSocketAddress(serverConfig.getHost(), port))
- .channelType(NioServerSocketChannel.class)
- .bossEventLoopGroup(new NioEventLoopGroup(1))
- .workerEventLoopGroup(new NioEventLoopGroup());
+ .channelType(selectorServerChannel())
+ .bossEventLoopGroup(selectorEventLoopGroup(1))
+ .workerEventLoopGroup(selectorEventLoopGroup(0));
}
private SslContextBuilder getSslContextBuilder(GrpcServerConfig config) {
@@ -202,4 +208,55 @@ public class GrpcStartable implements ServerStartable {
throw error[0];
}
}
+
+ /**
+ * https://netty.io/wiki/native-transports.html
+ *
+ * RHEL/CentOS/Fedora:
+ * sudo yum install autoconf automake libtool make tar \
+ * glibc-devel libaio-devel \
+ * libgcc.i686 glibc-devel.i686
+ * Debian/Ubuntu:
+ * sudo apt-get install autoconf automake libtool make tar \
+ * gcc-multilib libaio-dev
+ *
+ * brew install autoconf automake libtool
+ * */
+ private Class<? extends ServerChannel> selectorServerChannel() {
+ Class<? extends ServerChannel> channel = NioServerSocketChannel.class;
+ if (serverConfig.isNettyTransport()) {
+ if (OSInfo.isLinux()) {
+ channel = EpollServerSocketChannel.class;
+ } else if (OSInfo.isMacOS()) {
+ channel = KQueueServerSocketChannel.class;
+ }
+ }
+ LOG.info("Netty channel type is " + channel.getSimpleName());
+ return channel;
+ }
+
+ private EventLoopGroup selectorEventLoopGroup(int nThreads) {
+ EventLoopGroup group = new NioEventLoopGroup(nThreads);
+ if (serverConfig.isNettyTransport()) {
+ if (OSInfo.isLinux()) {
+ group = new EpollEventLoopGroup(nThreads);
+ } else if (OSInfo.isMacOS()) {
+ group = new KQueueEventLoopGroup(nThreads);
+ }
+ }
+ LOG.info("Netty event loop group is " + group.getClass().getSimpleName());
+ return group;
+ }
+
+ static class OSInfo {
+ private static String OS = System.getProperty("os.name").toLowerCase();
+
+ public static boolean isLinux() {
+ return OS.indexOf("linux") >= 0;
+ }
+
+ public static boolean isMacOS() {
+ return OS.indexOf("mac") >= 0;
+ }
+ }
}
diff --git a/alpha/alpha-server/src/main/resources/application.yaml b/alpha/alpha-server/src/main/resources/application.yaml
index e0d5b23..2a24d57 100644
--- a/alpha/alpha-server/src/main/resources/application.yaml
+++ b/alpha/alpha-server/src/main/resources/application.yaml
@@ -23,6 +23,7 @@ alpha:
host: ${server.host}
port: 8080
feature:
+ native: false # Default disabled Netty Native Transport
akka:
enabled: false
channel: