Skip to content

Commit

Permalink
[#4558] integrate-grpc-server
Browse files Browse the repository at this point in the history
1. Can manage grpcService with the settings by changing spring setting.
2. Remove the duplicate code.
  • Loading branch information
koo-taejin committed May 22, 2019
1 parent 3110f84 commit f89b955
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 454 deletions.

This file was deleted.

Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -16,16 +16,15 @@

package com.navercorp.pinpoint.collector.receiver.grpc;

import com.navercorp.pinpoint.collector.receiver.DispatchHandler;
import com.navercorp.pinpoint.collector.receiver.grpc.service.StatService;
import com.navercorp.pinpoint.common.server.util.AddressFilter;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.common.util.CollectionUtils;
import com.navercorp.pinpoint.grpc.server.MetadataServerTransportFilter;
import com.navercorp.pinpoint.grpc.server.ServerFactory;
import com.navercorp.pinpoint.grpc.server.ServerOption;
import com.navercorp.pinpoint.grpc.server.TransportMetadataFactory;
import com.navercorp.pinpoint.grpc.server.TransportMetadataServerInterceptor;
import io.grpc.BindableService;
import com.navercorp.pinpoint.grpc.server.ServerOption;
import io.grpc.Server;
import io.grpc.ServerInterceptor;
import io.grpc.ServerTransportFilter;
Expand All @@ -35,10 +34,14 @@
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import java.util.Objects;
import java.io.Closeable;
import java.util.List;
import java.util.concurrent.Executor;

public class StatServer implements InitializingBean, DisposableBean, BeanNameAware {
/**
* @author Taejin Koo
*/
public class GrpcReceiver implements InitializingBean, DisposableBean, BeanNameAware {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

private String beanName;
Expand All @@ -50,12 +53,15 @@ public class StatServer implements InitializingBean, DisposableBean, BeanNameAwa
private ServerFactory serverFactory;
private Executor executor;

private DispatchHandler dispatchHandler;
private List<BindableService> bindableServiceList;

private AddressFilter addressFilter;
private ServerOption serverOption;

private Server server;



@Override
public void afterPropertiesSet() throws Exception {
if (Boolean.FALSE == this.enable) {
Expand All @@ -64,8 +70,8 @@ public void afterPropertiesSet() throws Exception {

Assert.requireNonNull(this.beanName, "beanName must not be null");
Assert.requireNonNull(this.bindIp, "bindIp must not be null");
Assert.requireNonNull(this.dispatchHandler, "dispatchHandler must not be null");
Assert.requireNonNull(this.addressFilter, "addressFilter must not be null");
Assert.isTrue(CollectionUtils.hasLength(this.bindableServiceList), "bindableService must not be empty");

this.serverFactory = new ServerFactory(beanName, this.bindIp, this.bindPort, this.executor);
ServerTransportFilter permissionServerTransportFilter = new PermissionServerTransportFilter(addressFilter);
Expand All @@ -78,28 +84,34 @@ public void afterPropertiesSet() throws Exception {
ServerInterceptor transportMetadataServerInterceptor = new TransportMetadataServerInterceptor();
this.serverFactory.addInterceptor(transportMetadataServerInterceptor);

// Add options

// Add service
BindableService statService = new StatService(this.dispatchHandler);
serverFactory.addService(statService);
for (BindableService bindableService : bindableServiceList) {
this.serverFactory.addService(bindableService);
}

this.server = serverFactory.build();
if (logger.isInfoEnabled()) {
logger.info("Start stat server {}", this.server);
logger.info("Start {} server {}", this.beanName, this.server);
}
this.server.start();
}

@Override
public void destroy() throws Exception {
if (logger.isInfoEnabled()) {
logger.info("Destroy stat server {}", this.server);
logger.info("Destroy {} server {}", this.beanName, this.server);
}

if (this.server != null) {
this.server.shutdown();
}

for (BindableService bindableService : bindableServiceList) {
if (bindableService instanceof Closeable) {
((Closeable) bindableService).close();
}
}

if (this.serverFactory != null) {
this.serverFactory.close();
}
Expand Down Expand Up @@ -129,10 +141,6 @@ public void setBindPort(int bindPort) {
this.bindPort = bindPort;
}

public void setDispatchHandler(DispatchHandler dispatchHandler) {
this.dispatchHandler = dispatchHandler;
}

public void setAddressFilter(AddressFilter addressFilter) {
this.addressFilter = addressFilter;
}
Expand All @@ -144,4 +152,9 @@ public void setExecutor(Executor executor) {
public void setServerOption(ServerOption serverOption) {
this.serverOption = serverOption;
}
}

public void setBindableServiceList(List<BindableService> bindableServiceList) {
this.bindableServiceList = bindableServiceList;
}

}

0 comments on commit f89b955

Please sign in to comment.