/** * 从配置文件中读取参数创建{@link ThriftServerConfig}实例 * @return */ public static ThriftServerConfig makeThriftServerConfig(){ ThriftServerConfig thriftServerConfig = new ThriftServerConfig(); CombinedConfiguration config = GlobalConfig.getConfig(); int intValue ; thriftServerConfig.setPort(config.getInt(SERVER_PORT,DEFAULT_PORT)); if((intValue = config.getInt(SERVER_CONNECTION_LIMIT,0)) >0){ thriftServerConfig.setConnectionLimit(intValue); } if((intValue = config.getInt(SERVER_IDLE_CONNECTION_TIMEMOUT,0))>0){ Duration timeout = new Duration(intValue,TimeUnit.SECONDS); thriftServerConfig.setIdleConnectionTimeout(timeout); } if((intValue = config.getInt(SERVER_WORKER_THREAD_COUNT,0))>0){ thriftServerConfig.setWorkerThreads(intValue); } return thriftServerConfig; } /**
public TestThriftClientManager() { super(DelayedMapSyncHandler.class, DelayedMap.Client.class, new ThriftServerConfig().setBindAddress(LOCALHOST_IP_ADDRESS)); }
ScribeCollector(Builder builder) { ScribeSpanConsumer scribe = new ScribeSpanConsumer(builder); ThriftServiceProcessor processor = new ThriftServiceProcessor(new ThriftCodecManager(), emptyList(), scribe); server = new ThriftServer(processor, new ThriftServerConfig().setPort(builder.port)); }
@Test public void testDefaults() { ConfigAssertions.assertRecordedDefaults( ConfigAssertions.recordDefaults(ThriftServerConfig.class) .setBindAddress("localhost") .setAcceptBacklog(1024) .setMaxFrameSize(DataSize.valueOf("64MB")) .setPort(0) .setConnectionLimit(0) .setWorkerThreads(200) .setAcceptorThreadCount(1) .setIoThreadCount(2 * Runtime.getRuntime().availableProcessors()) .setIdleConnectionTimeout(Duration.valueOf("60s")) .setTransportName("framed") .setProtocolName("binary") .setWorkerExecutorKey(null) .setTaskExpirationTimeout(Duration.valueOf("5s")) .setQueueTimeout(null) .setMaxQueuedRequests(null) .setMaxQueuedResponsesPerConnection(16) .setTrafficClass(0) ); }
public SuiteBase( Class<? extends ServiceInterface> handlerClass, Class<? extends ClientInterface> clientClass, ThriftServerConfig serverConfig) { this(handlerClass, clientClass, serverConfig, ImmutableList.<ThriftEventHandler>of()); }
protected ThriftServer createTargetServer(int numThreads) throws TException, InstantiationException, IllegalAccessException { DelayedMapSyncHandler handler = new DelayedMapSyncHandler(); return createServerFromHandler(new ThriftServerConfig().setWorkerThreads(numThreads), handler); }
/** * 设置服务端口 * @param servicePort * @return * @see ThriftServerConfig#setPort(int) */ public Builder setServerPort(int servicePort) { this.thriftServerConfig.setPort(servicePort); return this; }
@Override protected void configure() { bind(ThriftServerConfig.class).toInstance( new ThriftServerConfig().setWorkerExecutor(myExecutor) ); } }
.build(); ThriftServerConfig expected = new ThriftServerConfig() .setPort(12345) .setMaxFrameSize(DataSize.valueOf("333kB")) .setBindAddress("127.0.0.1") .setAcceptBacklog(7777) .setWorkerThreads(111) .setAcceptorThreadCount(3) .setIoThreadCount(27) .setIdleConnectionTimeout(Duration.valueOf("157ms")) .setConnectionLimit(1111) .setWorkerExecutorKey("my-executor") .setTransportName("buffered") .setProtocolName("compact") .setTaskExpirationTimeout(Duration.valueOf("10s")) .setMaxQueuedRequests(1000) .setMaxQueuedResponsesPerConnection(32) .setQueueTimeout(Duration.valueOf("167ms")) .setTrafficClass(35);
ScribeCollector(Builder builder, Lazy<AsyncSpanConsumer> consumer) { ScribeSpanConsumer scribe = new ScribeSpanConsumer(builder.category, consumer, builder.metrics); ThriftServiceProcessor processor = new ThriftServiceProcessor(new ThriftCodecManager(), emptyList(), scribe); server = new ThriftServer(processor, new ThriftServerConfig().setPort(builder.port)).start(); }
public ExceptionTest() { super(ExceptionServiceHandler.class, ExceptionServiceClient.class, new ThriftServerConfig(), ImmutableList.<ThriftEventHandler>of(new ExceptionThrowingEventHandler())); }
protected ThriftServer createAsyncServer(int numThreads, ThriftClientManager clientManager, ThriftServer targetServer) throws Exception { DelayedMapAsyncProxyHandler handler = new DelayedMapAsyncProxyHandler(clientManager, targetServer); return createServerFromHandler(new ThriftServerConfig().setWorkerThreads(numThreads), handler); }
public static void main(String ...args){ serviceConfig.parseCommandLine(args); // 设置slf4j记录日志,否则会有警告 InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory()); ThriftServerConfig config = new ThriftServerConfig() .setPort(serviceConfig.getServicePort()) .setWorkerThreads(serviceConfig.getWorkThreads()) .setConnectionLimit(serviceConfig.getConnectionLimit()) .setIdleConnectionTimeout(new Duration(serviceConfig.getIdleConnectionTimeout(),TimeUnit.SECONDS)); FaceApi faceapi = serviceConfig.getFaceapi(); ThriftServerService service = ThriftServerService.bulider() .withServices(new FaceApiThriftDecorator(faceapi)) .setThriftServerConfig(config) .build(); logger.info("FaceApi instance:{}",faceapi); service.startAsync(); }
@Override protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException { TProcessor processor = new ThriftServiceProcessor(new ThriftCodecManager(), impl); final ThriftServer server = new ThriftServer(processor, new ThriftServerConfig().setPort(url.getPort())); server.start(); return new Runnable() { public void run() { try { server.close(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } } }; }
protected ThriftServer createServerFromHandler(Object handler) throws InstantiationException, IllegalAccessException { return createServerFromHandler(new ThriftServerConfig(), handler); }
/** * Create and start the Thrift server. * * @param conf Giraph conf to set the host and ports for. * @param services Services to start */ public ClientThriftServer(GiraphConfiguration conf, List<?> services) { checkNotNull(conf, "conf is null"); checkNotNull(services, "services is null"); ThriftServiceProcessor processor = new ThriftServiceProcessor(new ThriftCodecManager(), new ArrayList<ThriftEventHandler>(), services); clientThriftServer = new ThriftServer(processor, new ThriftServerConfig()); clientThriftServer.start(); try { CLIENT_THRIFT_SERVER_HOST.set( conf, conf.getLocalHostname()); } catch (UnknownHostException e) { throw new IllegalStateException("Unable to get host information", e); } CLIENT_THRIFT_SERVER_PORT.set(conf, clientThriftServer.getPort()); }
/** * Creates a {@link com.facebook.swift.service.guice.ThriftServerModule} with the binding * for {@link com.facebook.swift.service.ThriftServerConfig} overridden to specify a specific * instance of {@link java.util.concurrent.ExecutorService} for the worker executor. */ private Module overrideThriftServerModuleWithWorkerExecutorInstance(final ExecutorService myExecutor) { return Modules.override(new ThriftServerModule()).with( new AbstractModule() { @Override protected void configure() { bind(ThriftServerConfig.class).toInstance( new ThriftServerConfig().setWorkerExecutor(myExecutor) ); } } ); }
private void testProcessor(NiftyProcessor processor) throws Exception { DummyTransportAttachObserver dummyTransportAttachObserver = new DummyTransportAttachObserver(); try (ThriftServer server = new ThriftServer( processor, new ThriftServerConfig(), new NiftyTimer("timer"), ThriftServer.DEFAULT_FRAME_CODEC_FACTORIES, ThriftServer.DEFAULT_PROTOCOL_FACTORIES, ThriftServer.DEFAULT_WORKER_EXECUTORS, ThriftServer.DEFAULT_SECURITY_FACTORY, ThriftServer.DEFAULT_SSL_SERVER_CONFIGURATION, new ThriftServer.TransportAttachObserverHolder(dummyTransportAttachObserver)).start()) { assertTrue(dummyTransportAttachObserver.getState()); server.close(); assertFalse(dummyTransportAttachObserver.getState()); } }
private List<LogEntry> testProcessor(NiftyProcessor processor, boolean plaintext) throws Exception { ImmutableList<LogEntry> messages = ImmutableList.of( new LogEntry("hello", "world"), new LogEntry("bye", "world") ); SslServerConfiguration sslConfiguration = OpenSslServerConfiguration.newBuilder() .certFile(new File(getClass().getResource("/rsa.crt").getFile())) .keyFile(new File(getClass().getResource("/rsa.key").getFile())) .allowPlaintext(plaintext) .build(); try (ThriftServer server = new ThriftServer( processor, new ThriftServerConfig(), new NiftyTimer("timer"), ThriftServer.DEFAULT_FRAME_CODEC_FACTORIES, ThriftServer.DEFAULT_PROTOCOL_FACTORIES, ThriftServer.DEFAULT_WORKER_EXECUTORS, ThriftServer.DEFAULT_SECURITY_FACTORY, new ThriftServer.SslServerConfigurationHolder(sslConfiguration), ThriftServer.DEFAULT_TRANSPORT_ATTACH_OBSERVER).start()) { assertEquals(logThrift(server.getPort(), messages), ResultCode.OK); assertEquals(logSwift(server.getPort(), toSwiftLogEntry(messages)), com.facebook.swift.service.ResultCode.OK); } return messages; }
@Test public void testGenericProcessor() throws ExecutionException, InterruptedException { ThriftCodecManager codecManager = new ThriftCodecManager(); ThriftServiceProcessor processor = new ThriftServiceProcessor(codecManager, ImmutableList.<ThriftEventHandler>of(), new GenericService()); try (ThriftServer server = new ThriftServer(processor, new ThriftServerConfig()).start(); ThriftClientManager clientManager = new ThriftClientManager(codecManager)) { ThriftClient<GenericInterface.Client> clientOpener = new ThriftClient<>(clientManager, GenericInterface.Client.class); try (GenericInterface.Client client = clientOpener.open(new FramedClientConnector(HostAndPort.fromParts("localhost", server.getPort()))).get()) { GenericStruct<String> original = new GenericStruct<>(); original.genericField = "original.genericField"; GenericStruct<String> copy = client.echo(original); assertEquals(original, copy); } } } }