/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/server/TcpServer.java

https://github.com/apache/servicecomb-java-chassis · Java · 104 lines · 74 code · 13 blank · 17 comment · 7 complexity · 909de638f9f554c81d4c378a2169db7d MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.servicecomb.foundation.vertx.server;
  18. import java.net.InetSocketAddress;
  19. import org.apache.servicecomb.foundation.common.net.URIEndpointObject;
  20. import org.apache.servicecomb.foundation.common.utils.ExceptionUtils;
  21. import org.apache.servicecomb.foundation.ssl.SSLCustom;
  22. import org.apache.servicecomb.foundation.ssl.SSLOption;
  23. import org.apache.servicecomb.foundation.ssl.SSLOptionFactory;
  24. import org.apache.servicecomb.foundation.vertx.AsyncResultCallback;
  25. import org.apache.servicecomb.foundation.vertx.VertxTLSBuilder;
  26. import org.apache.servicecomb.foundation.vertx.metrics.DefaultTcpServerMetrics;
  27. import org.apache.servicecomb.foundation.vertx.metrics.metric.DefaultServerEndpointMetric;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import io.vertx.core.Vertx;
  31. import io.vertx.core.net.NetServer;
  32. import io.vertx.core.net.NetServerOptions;
  33. import io.vertx.core.net.impl.NetSocketImpl;
  34. public class TcpServer {
  35. private static final Logger LOGGER = LoggerFactory.getLogger(TcpServer.class);
  36. private URIEndpointObject endpointObject;
  37. public TcpServer(URIEndpointObject endpointObject) {
  38. this.endpointObject = endpointObject;
  39. }
  40. public void init(Vertx vertx, String sslKey, AsyncResultCallback<InetSocketAddress> callback) {
  41. NetServer netServer;
  42. if (endpointObject.isSslEnabled()) {
  43. SSLOptionFactory factory =
  44. SSLOptionFactory.createSSLOptionFactory(sslKey, null);
  45. SSLOption sslOption;
  46. if (factory == null) {
  47. sslOption = SSLOption.buildFromYaml(sslKey);
  48. } else {
  49. sslOption = factory.createSSLOption();
  50. }
  51. SSLCustom sslCustom = SSLCustom.createSSLCustom(sslOption.getSslCustomClass());
  52. NetServerOptions serverOptions = new NetServerOptions();
  53. VertxTLSBuilder.buildNetServerOptions(sslOption, sslCustom, serverOptions);
  54. netServer = vertx.createNetServer(serverOptions);
  55. } else {
  56. netServer = vertx.createNetServer();
  57. }
  58. netServer.connectHandler(netSocket -> {
  59. DefaultTcpServerMetrics serverMetrics = (DefaultTcpServerMetrics) ((NetSocketImpl) netSocket).metrics();
  60. DefaultServerEndpointMetric endpointMetric = serverMetrics.getEndpointMetric();
  61. long connectedCount = endpointMetric.getCurrentConnectionCount();
  62. int connectionLimit = getConnectionLimit();
  63. if (connectedCount > connectionLimit) {
  64. netSocket.close();
  65. endpointMetric.onRejectByConnectionLimit();
  66. return;
  67. }
  68. TcpServerConnection connection = createTcpServerConnection();
  69. connection.init(netSocket);
  70. });
  71. netServer.exceptionHandler(e -> {
  72. LOGGER.error("Unexpected error in server.{}", ExceptionUtils.getExceptionMessageWithoutTrace(e));
  73. });
  74. InetSocketAddress socketAddress = endpointObject.getSocketAddress();
  75. netServer.listen(socketAddress.getPort(), socketAddress.getHostString(), ar -> {
  76. if (ar.succeeded()) {
  77. callback.success(socketAddress);
  78. return;
  79. }
  80. // 监听失败
  81. String msg = String.format("listen failed, address=%s", socketAddress.toString());
  82. callback.fail(new Exception(msg, ar.cause()));
  83. });
  84. }
  85. protected int getConnectionLimit() {
  86. return Integer.MAX_VALUE;
  87. }
  88. protected TcpServerConnection createTcpServerConnection() {
  89. return new TcpServerConnection();
  90. }
  91. }