com.alipay.sofa.rpc.config.ServerConfig Java Examples

The following examples show how to use com.alipay.sofa.rpc.config.ServerConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConsulRegistry.java    From sofa-rpc with Apache License 2.0 7 votes vote down vote up
private List<NewService> buildNewServices(ProviderConfig<?> config) {
    List<ServerConfig> servers = config.getServer();
    if (CommonUtils.isEmpty(servers)) {
        return Collections.emptyList();
    }
    return servers.stream().map(server -> {
        NewService service = new NewService();
        service.setId(buildServiceId(config, server));
        service.setName(buildServiceName(config));

        String host = getServerHost(server);
        int port = server.getPort();
        service.setAddress(host);
        service.setPort(port);

        Map<String, String> metaData = RegistryUtils.convertProviderToMap(config, server).entrySet().stream()
                .filter(e -> ConsulUtils.isValidMetaKey(e.getKey()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        service.setMeta(metaData);
        service.setTags(Collections.singletonList(buildUniqueName(config, server.getProtocol())));

        service.setCheck(buildCheck(host, port));
        return service;
    }).collect(Collectors.toList());
}
 
Example #2
Source File: OnewayServerMain.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("oneway-server");

        ServerConfig serverConfig2 = new ServerConfig()
            .setPort(22222)
            .setDaemon(false);

        ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>()
            .setApplication(applicationConfig)
            .setInterfaceId(HelloService.class.getName())
            .setRef(new HelloServiceImpl(1000))
            .setServer(serverConfig2);
        providerConfig.export();

        ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>()
            .setApplication(applicationConfig)
            .setInterfaceId(EchoService.class.getName())
            .setRef(new EchoServiceImpl())
            .setServer(serverConfig2);
        providerConfig2.export();
    }
 
Example #3
Source File: LazyConnectTest.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startServer() {

    RpcRunningState.setUnitTestMode(true);
    // 只有2个线程 执行
    serverConfig = new ServerConfig()
        .setStopTimeout(0)
        .setPort(22222)
        .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT)
        .setQueues(100).setCoreThreads(5).setMaxThreads(5);

    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>()
        .setInterfaceId(HelloService.class.getName())
        .setRef(new HelloServiceImpl())
        .setServer(serverConfig)
        .setRegister(false);
    providerConfig.export();
}
 
Example #4
Source File: ConsulRegistryTest.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
private ProviderConfig<?> providerConfig(String uniqueId, int... ports) {
    ProviderConfig<?> provider = new ProviderConfig();
    provider.setInterfaceId(INTERFACE_ID)
            .setUniqueId(uniqueId)
            .setApplication(new ApplicationConfig().setAppName("consul-registry-test"))
            .setProxy("javassist")
            .setRegister(true)
            .setRegistry(registryConfig)
            .setSerialization("hessian2")
            .setWeight(222)
            .setTimeout(3000);

    IntStream.of(ports)
            .mapToObj(port ->
                    new ServerConfig()
                            .setProtocol("bolt")
                            .setHost("localhost")
                            .setPort(port)
            ).forEach(provider::setServer);
    return provider;
}
 
Example #5
Source File: ServerConfigContainer.java    From sofa-rpc-boot-projects with Apache License 2.0 6 votes vote down vote up
/**
 * 释放所有 ServerConfig 对应的资源,并移除所有的 ServerConfig。
 */
public void closeAllServer() {
    destroyServerConfig(boltServerConfig);
    destroyServerConfig(restServerConfig);
    destroyServerConfig(dubboServerConfig);
    destroyServerConfig(h2cServerConfig);

    for (Map.Entry<String, ServerConfig> entry : customServerConfigs.entrySet()) {
        final ServerConfig serverConfig = entry.getValue();
        destroyServerConfig(serverConfig);
    }

    boltServerConfig = null;
    restServerConfig = null;
    dubboServerConfig = null;
    h2cServerConfig = null;
    customServerConfigs.clear();
}
 
Example #6
Source File: AbstractHttpServer.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * ServerConfig转ServerTransportConfig
 *
 * @param serverConfig 服务端配置
 * @return ServerTransportConfig 服务传输层配置
 */
private static ServerTransportConfig convertConfig(ServerConfig serverConfig) {
    ServerTransportConfig serverTransportConfig = new ServerTransportConfig();
    serverTransportConfig.setPort(serverConfig.getPort());
    serverTransportConfig.setProtocolType(serverConfig.getProtocol());
    serverTransportConfig.setHost(serverConfig.getBoundHost());
    serverTransportConfig.setContextPath(serverConfig.getContextPath());
    serverTransportConfig.setBizMaxThreads(serverConfig.getMaxThreads());
    serverTransportConfig.setBizPoolType(serverConfig.getThreadPoolType());
    serverTransportConfig.setIoThreads(serverConfig.getIoThreads());
    serverTransportConfig.setChannelListeners(serverConfig.getOnConnect());
    serverTransportConfig.setMaxConnection(serverConfig.getAccepts());
    serverTransportConfig.setPayload(serverConfig.getPayload());
    serverTransportConfig.setTelnet(serverConfig.isTelnet());
    serverTransportConfig.setUseEpoll(serverConfig.isEpoll());
    serverTransportConfig.setBizPoolQueueType(serverConfig.getQueueType());
    serverTransportConfig.setBizPoolQueues(serverConfig.getQueues());
    serverTransportConfig.setDaemon(serverConfig.isDaemon());
    serverTransportConfig.setParameters(serverConfig.getParameters());
    serverTransportConfig.setContainer(serverConfig.getTransport());
    return serverTransportConfig;
}
 
Example #7
Source File: DubboProviderBootstrap.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
private void copyServerFields(ServerConfig serverConfig, ProtocolConfig protocolConfig) {
    protocolConfig.setId(serverConfig.getId());
    protocolConfig.setName(serverConfig.getProtocol());
    protocolConfig.setHost(serverConfig.getHost());
    protocolConfig.setPort(serverConfig.getPort());
    protocolConfig.setAccepts(serverConfig.getAccepts());
    protocolConfig.setSerialization(serverConfig.getSerialization());
    if (!StringUtils.CONTEXT_SEP.equals(serverConfig.getContextPath())) {
        protocolConfig.setContextpath(serverConfig.getContextPath());
    }
    protocolConfig.setIothreads(serverConfig.getIoThreads());
    protocolConfig.setThreadpool(serverConfig.getThreadPoolType());
    protocolConfig.setThreads(serverConfig.getMaxThreads());
    protocolConfig.setPayload(serverConfig.getPayload());
    protocolConfig.setQueues(serverConfig.getQueues());

    protocolConfig.setParameters(serverConfig.getParameters());
}
 
Example #8
Source File: RpcBindingAdapter.java    From sofa-rpc-boot-projects with Apache License 2.0 6 votes vote down vote up
/**
 * post unout binding
 *
 * @param contract           binding contract
 * @param binding            binding object
 * @param target             binding target
 * @param sofaRuntimeContext sofa runtime context
 */
@Override
public void postUnoutBinding(Object contract, RpcBinding binding, Object target,
                             SofaRuntimeContext sofaRuntimeContext) {
    ProviderConfig metadata = SpringBridge.getProviderConfigHelper().getProviderConfig((Contract) contract,
        binding, target);
    try {
        String key = SpringBridge.getProviderConfigContainer().createUniqueName((Contract) contract, binding);
        List<ServerConfig> servers = SpringBridge.getProviderConfigContainer().getProviderConfig(key).getServer();
        for (ServerConfig server : servers) {
            server.getServer().unRegisterProcessor(metadata, false);
        }
        SpringBridge.getProviderConfigContainer().removeProviderConfig(key);
    } catch (Exception e) {
        throw new ServiceRuntimeException(
            LogCodes.getLog(LogCodes.ERROR_PROXY_POST_UNPUBLISH_FAIL), e);
    }
}
 
Example #9
Source File: BoltServerMain.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-server");

    ServerConfig serverConfig = new ServerConfig()
        .setPort(22000)
        .setDaemon(false);

    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>()
        .setInterfaceId(HelloService.class.getName())
        .setApplication(application)
        .setRef(new HelloServiceImpl())
        .setServer(serverConfig)
        .setRegister(false);

    ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>()
        .setInterfaceId(EchoService.class.getName())
        .setApplication(application)
        .setRef(new EchoServiceImpl())
        .setServer(serverConfig)
        .setRegister(false);

    providerConfig.export();
    providerConfig2.export();

    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
 
Example #10
Source File: RegistryUtils.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
public static Map<String, String> convertProviderToMap(ProviderConfig providerConfig, ServerConfig server) {
    Map<String, String> metaData = new HashMap<String, String>();
    metaData.put(RpcConstants.CONFIG_KEY_UNIQUEID, providerConfig.getUniqueId());
    metaData.put(RpcConstants.CONFIG_KEY_INTERFACE, providerConfig.getInterfaceId());
    metaData.put(RpcConstants.CONFIG_KEY_TIMEOUT, String.valueOf(providerConfig.getTimeout()));
    metaData.put(RpcConstants.CONFIG_KEY_DELAY, String.valueOf(providerConfig.getDelay()));
    metaData.put(RpcConstants.CONFIG_KEY_ID, providerConfig.getId());
    metaData.put(RpcConstants.CONFIG_KEY_DYNAMIC, String.valueOf(providerConfig.isDynamic()));
    metaData.put(ProviderInfoAttrs.ATTR_WEIGHT, String.valueOf(providerConfig.getWeight()));
    metaData.put(RpcConstants.CONFIG_KEY_ACCEPTS, String.valueOf(server.getAccepts()));
    metaData.put(ProviderInfoAttrs.ATTR_START_TIME, String.valueOf(RpcRuntimeContext.now()));
    metaData.put(RpcConstants.CONFIG_KEY_APP_NAME, providerConfig.getAppName());
    metaData.put(RpcConstants.CONFIG_KEY_SERIALIZATION, providerConfig.getSerialization());
    metaData.put(RpcConstants.CONFIG_KEY_PROTOCOL, server.getProtocol());
    if (null != providerConfig.getParameters()) {
        //noinspection unchecked
        metaData.putAll(providerConfig.getParameters());
    }

    // add common attr
    metaData.put(RpcConstants.CONFIG_KEY_LANGUAGE, JAVA);
    metaData.put(RpcConstants.CONFIG_KEY_PID, RpcRuntimeContext.PID);
    metaData.put(RpcConstants.CONFIG_KEY_RPC_VERSION, String.valueOf(Version.RPC_VERSION));
    return metaData;
}
 
Example #11
Source File: ServerConfigContainerTest.java    From sofa-rpc-boot-projects with Apache License 2.0 6 votes vote down vote up
@Test
public void testBoltConfiguration() {
    sofaBootRpcProperties.setBoltPort("9090");
    sofaBootRpcProperties.setBoltThreadPoolCoreSize("8080");
    sofaBootRpcProperties.setBoltThreadPoolMaxSize("7070");
    sofaBootRpcProperties.setBoltAcceptsSize(("6060"));
    sofaBootRpcProperties.setVirtualHost("127.0.0.2");
    sofaBootRpcProperties.setBoundHost("127.0.0.3");
    sofaBootRpcProperties.setVirtualPort("8888");
    ServerConfig serverConfig = serverConfigContainer.createBoltServerConfig();
    Assert.assertEquals(9090, serverConfig.getPort());
    Assert.assertEquals(8080, serverConfig.getCoreThreads());
    Assert.assertEquals(7070, serverConfig.getMaxThreads());
    Assert.assertEquals(6060, serverConfig.getAccepts());
    Assert.assertEquals(8888, serverConfig.getVirtualPort().intValue());
    Assert.assertEquals("127.0.0.2", serverConfig.getVirtualHost());
    Assert.assertEquals("127.0.0.3", serverConfig.getBoundHost());
}
 
Example #12
Source File: BoltProtobufTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void testAll() {
    ServerConfig serverConfig = new ServerConfig()
        .setProtocol("bolt") // 设置一个协议,默认bolt
        .setPort(12200) // 设置一个端口,默认12200
        .setDaemon(false); // 非守护线程

    ProviderConfig<ProtobufService> providerConfig = new ProviderConfig<ProtobufService>()
        .setInterfaceId(ProtobufService.class.getName()) // 指定接口
        .setRef(new ProtobufServiceImpl()) // 指定实现
        .setServer(serverConfig); // 指定服务端
    providerConfig.export(); // 发布服务

    ConsumerConfig<ProtobufService> consumerConfig = new ConsumerConfig<ProtobufService>()
        .setInterfaceId(ProtobufService.class.getName()) // 指定接口
        .setProtocol("bolt") // 指定协议
        .setDirectUrl("bolt://127.0.0.1:12200") // 指定直连地址
        .setSerialization("protobuf") // 指定序列化协议,默认为hessian
        .setConnectTimeout(10 * 1000);
    ProtobufService helloService = consumerConfig.refer();

    EchoRequest request = EchoRequest.newBuilder().setName("sofa").setGroup(Group.A).build();
    EchoResponse response = helloService.echoObj(request);
    LOGGER.info(response.getCode() + ": " + response.getMessage());

    boolean error = false;
    try {
        helloService.echoObj(null);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);

    Assert.assertEquals(200, response.getCode());
    Assert.assertEquals("protobuf works! sofa", response.getMessage());
}
 
Example #13
Source File: FailoverClusterTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void testRpcDirectInvokeFromContext() {

    ServerConfig serverConfig = new ServerConfig()
        .setProtocol("bolt")
        .setHost("0.0.0.0")
        .setPort(13900);

    ProviderConfig<HelloService> provider = new ProviderConfig();
    provider.setInterfaceId(HelloService.class.getName())
        .setRef(new HelloServiceImpl("x-demo-invoke"))
        .setApplication(new ApplicationConfig().setAppName("x-test-server"))
        .setProxy("javassist")
        .setSerialization("hessian2")
        .setServer(serverConfig)
        .setTimeout(3000);

    provider.export();

    ConsumerConfig<HelloService> consumer = new ConsumerConfig();
    consumer.setInterfaceId(HelloService.class.getName())
        .setApplication(new ApplicationConfig().setAppName("x-test-client"))
        .setProxy("javassist");

    HelloService proxy = consumer.refer();

    for (int i = 0; i < 3; i++) {
        RpcInvokeContext.getContext().setTargetURL("127.0.0.1:13900");
        Assert.assertEquals("x-demo-invoke", proxy.sayHello("x-demo-invoke", 1));
    }

    provider.unExport();
    consumer.unRefer();
}
 
Example #14
Source File: RegistryUtils.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public static String getServerHost(ServerConfig server) {
    String host = server.getVirtualHost();
    if (host == null) {
        host = server.getHost();
        if (NetUtils.isLocalHost(host) || NetUtils.isAnyHost(host)) {
            host = SystemInfo.getLocalHost();
        }
    }
    return host;
}
 
Example #15
Source File: DubooServerTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
//同步泛化调用,直连
public void testGenericSync() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig()
        .setStopTimeout(60000)
        .setPort(20880)
        .setProtocol("dubbo")
        .setQueues(100).setCoreThreads(1).setMaxThreads(2);

    // 发布一个服务,每个请求要执行1秒
    ApplicationConfig serverApplacation = new ApplicationConfig();
    serverApplacation.setAppName("server");
    providerConfig = new ProviderConfig<DemoService>()
        .setInterfaceId(DemoService.class.getName())
        .setRef(new DemoServiceImpl())
        .setBootstrap("dubbo")
        .setServer(serverConfig)
        // .setParameter(RpcConstants.CONFIG_HIDDEN_KEY_WARNING, "false")
        .setRegister(false).setApplication(serverApplacation);
    providerConfig.export();

    ApplicationConfig clientApplication = new ApplicationConfig();
    clientApplication.setAppName("client");
    consumerConfig = new ConsumerConfig<DemoService>()
        .setInterfaceId(DemoService.class.getName())
        .setDirectUrl("dubbo://127.0.0.1:20880")
        .setBootstrap("dubbo")
        .setTimeout(30000)
        .setRegister(false)
        .setProtocol("dubbo")
        .setApplication(clientApplication)
        .setGeneric(true);
    final GenericService demoService = (GenericService) consumerConfig.refer();

    String result = (String) demoService.$invoke("sayHello", new String[] { "java.lang.String" },
        new Object[] { "xxx" });
    Assert.assertEquals(result, "Hello xxx");

}
 
Example #16
Source File: SofaNettyJaxrsServer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Build SofaNettyJaxrsServer
 *
 * @param serverConfig ServerConfig
 */
public SofaNettyJaxrsServer(ServerConfig serverConfig) {
    if (serverConfig == null) {
        throw new IllegalArgumentException("server config is null");
    }
    this.serverConfig = serverConfig;
}
 
Example #17
Source File: AbstractHttpServer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void init(ServerConfig serverConfig) {
    this.serverConfig = serverConfig;
    this.serverTransportConfig = convertConfig(serverConfig);
    // 启动线程池
    this.bizThreadPool = initThreadPool(serverConfig);
    // 服务端处理器
    this.serverHandler = new HttpServerHandler();

    // set default transport config
    this.serverTransportConfig.setContainer(container);
    this.serverTransportConfig.setServerHandler(serverHandler);
}
 
Example #18
Source File: MulticastRegistry.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void register(ProviderConfig config) {
    String appName = config.getAppName();
    if (!registryConfig.isRegister()) {
        if (LOGGER.isInfoEnabled(appName)) {
            LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
        }
        return;
    }
    if (config.isRegister()) {
        List<ServerConfig> serverConfigs = config.getServer();
        if (CommonUtils.isNotEmpty(serverConfigs)) {
            for (ServerConfig server : serverConfigs) {
                String serviceName = MulticastRegistryHelper.buildListDataId(config, server.getProtocol());
                ProviderInfo providerInfo = MulticastRegistryHelper.convertProviderToProviderInfo(config, server);
                if (LOGGER.isInfoEnabled(appName)) {
                    LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_PUB_START, serviceName));
                }
                doRegister(appName, serviceName, providerInfo);

                if (LOGGER.isInfoEnabled(appName)) {
                    LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_PUB_OVER, serviceName));
                }
            }
            if (EventBus.isEnable(ProviderPubEvent.class)) {
                ProviderPubEvent event = new ProviderPubEvent(config);
                EventBus.post(event);
            }

        }
    }

}
 
Example #19
Source File: MeshRegistry.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void register(ProviderConfig config) {
    String appName = config.getAppName();
    if (!registryConfig.isRegister()) {
        if (LOGGER.isInfoEnabled(appName)) {
            LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
        }
        return;
    }
    if (!config.isRegister()) { // 注册中心不注册或者服务不注册
        return;
    }
    List<ServerConfig> serverConfigs = config.getServer();
    if (CommonUtils.isNotEmpty(serverConfigs)) {
        for (ServerConfig server : serverConfigs) {
            String serviceName = MeshRegistryHelper.buildMeshKey(config, server.getProtocol());
            ProviderInfo providerInfo = MeshRegistryHelper.convertProviderToProviderInfo(config, server);
            if (LOGGER.isInfoEnabled(appName)) {
                LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_PUB_START, serviceName));
            }
            doRegister(appName, serviceName, providerInfo, server.getProtocol());

            if (LOGGER.isInfoEnabled(appName)) {
                LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_PUB_OVER, serviceName));
            }
        }
        if (EventBus.isEnable(ProviderPubEvent.class)) {
            ProviderPubEvent event = new ProviderPubEvent(config);
            EventBus.post(event);
        }

    }
}
 
Example #20
Source File: BoltServer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void init(ServerConfig serverConfig) {
    this.serverConfig = serverConfig;
    // 启动线程池
    bizThreadPool = initThreadPool(serverConfig);
    boltServerProcessor = new BoltServerProcessor(this);
}
 
Example #21
Source File: BoltServer.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
protected ThreadPoolExecutor initThreadPool(ServerConfig serverConfig) {
    ThreadPoolExecutor threadPool = BusinessPool.initPool(serverConfig);
    threadPool.setThreadFactory(new NamedThreadFactory(
        "SEV-BOLT-BIZ-" + serverConfig.getPort(), serverConfig.isDaemon()));
    threadPool.setRejectedExecutionHandler(new SofaRejectedExecutionHandler());
    if (serverConfig.isPreStartCore()) { // 初始化核心线程池
        threadPool.prestartAllCoreThreads();
    }
    return threadPool;
}
 
Example #22
Source File: SofaRegistry.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void register(ProviderConfig config) {
    String appName = config.getAppName();
    if (!registryConfig.isRegister()) {
        if (LOGGER.isInfoEnabled(appName)) {
            LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
        }
        return;
    }
    if (!config.isRegister()) {
        // 注册中心不注册或者服务不注册
        return;
    }
    List<ServerConfig> serverConfigs = config.getServer();
    if (CommonUtils.isNotEmpty(serverConfigs)) {
        for (ServerConfig server : serverConfigs) {
            String serviceName = SofaRegistryHelper.buildListDataId(config, server.getProtocol());
            String serviceData = SofaRegistryHelper.convertProviderToUrls(config, server);
            if (LOGGER.isInfoEnabled(appName)) {
                LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_PUB_START, serviceName));
            }
            String groupId = config.getParameter(SofaRegistryConstants.SOFA_GROUP_KEY);
            groupId = groupId == null ? SofaRegistryHelper.SUBSCRIBER_LIST_GROUP_ID : groupId;
            doRegister(appName, serviceName, serviceData, groupId);

            if (LOGGER.isInfoEnabled(appName)) {
                LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_PUB_OVER, serviceName));
            }
        }
    }
}
 
Example #23
Source File: MultiProtocolServerTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiProtocol() {

    try {
        // 只有2个线程 执行
        ServerConfig serverConfig = new ServerConfig()
            .setStopTimeout(0)
            .setPort(22222)
            .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT)
            .setQueues(100).setCoreThreads(1).setMaxThreads(2);

        // 发布一个服务,每个请求要执行1秒
        ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>()
            .setInterfaceId(RestService.class.getName())
            .setRef(new RestServiceImpl())
            .setServer(serverConfig)
            .setRepeatedExportLimit(1)
            .setRegister(false);
        providerConfig.export();

        ServerConfig serverConfig2 = new ServerConfig()
            .setStopTimeout(0)
            .setPort(22223)
            .setProtocol(RpcConstants.PROTOCOL_TYPE_REST)
            .setQueues(100).setCoreThreads(1).setMaxThreads(2);

        // 发布一个服务,每个请求要执行1秒
        ProviderConfig<RestService> providerConfig2 = new ProviderConfig<RestService>()
            .setInterfaceId(RestService.class.getName())
            .setRef(new RestServiceImpl())
            .setServer(serverConfig2)
            .setRepeatedExportLimit(1)
            .setRegister(false);
        providerConfig2.export();
    } catch (Throwable e) {
        Assert.fail();
    }
}
 
Example #24
Source File: TripleServerRegistryApplication.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("triple-server");

        int port = 50051;
        if (args.length != 0) {
            LOGGER.debug("first arg is {}", args[0]);
            port = Integer.valueOf(args[0]);
        }

        /*   RegistryConfig registryConfig = new RegistryConfig()
               .setProtocol("zookeeper")
               .setAddress("127.0.0.1:2181");*/

        ServerConfig serverConfig = new ServerConfig()
            .setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE)
            .setPort(port);

        ProviderConfig<SofaGreeterTriple.IGreeter> providerConfig = new ProviderConfig<SofaGreeterTriple.IGreeter>()
            .setApplication(applicationConfig)
            .setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE)
            .setInterfaceId(SofaGreeterTriple.IGreeter.class.getName())
            .setRef(new TripleGreeterImpl())
            .setServer(serverConfig)
            .setRegister(false);
        // .setRegistry(registryConfig);

        providerConfig.export();

        synchronized (TripleServerRegistryApplication.class) {
            try {
                while (true) {
                    TripleServerRegistryApplication.class.wait();
                }
            } catch (InterruptedException e) {
                LOGGER.error("Exit by Interrupted");
            }
        }

    }
 
Example #25
Source File: RestServerMain.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-server");

    /*
     访问地址:
     POST http://127.0.0.1:8888/rest/hello/code/name
     GET http://127.0.0.1:8888/rest/hello/code
     PUT http://127.0.0.1:8888/rest/hello/code/name
     DELETE http://127.0.0.1:8888/rest/hello/code
     GET http://127.0.0.1:8888/rest/get/1234567890
     POST http://127.0.0.1:8888/rest/post/1234567890 bodydddddd
     */

    ServerConfig serverConfig = new ServerConfig()
        .setProtocol("rest")
        .setPort(8888)
        .setDaemon(false);

    ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>()
        .setInterfaceId(RestService.class.getName())
        .setApplication(application)
        .setRef(new RestServiceImpl())
        .setBootstrap("rest")
        .setServer(serverConfig)
        .setRegister(false);

    providerConfig.export();

    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
 
Example #26
Source File: ZookeeperBoltServerMain.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        /**
         * 运行需要pom.xml里增加依赖 
         <dependency>
             <groupId>org.apache.curator</groupId>
             <artifactId>curator-recipes</artifactId>
             <scope>test</scope>
         </dependency>
         */
        RegistryConfig registryConfig = new RegistryConfig()
            .setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK)
            .setAddress("127.0.0.1:2181");

        ServerConfig serverConfig = new ServerConfig()
            .setPort(22101)
            .setDaemon(false);

        ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>()
            .setInterfaceId(HelloService.class.getName())
            .setRef(new HelloServiceImpl("result from 22101"))
            .setServer(serverConfig)
            .setRegistry(registryConfig);

        ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>()
            .setInterfaceId(EchoService.class.getName())
            .setRef(new EchoServiceImpl())
            .setServer(serverConfig)
            .setRegistry(registryConfig);

        providerConfig.export();
        providerConfig2.export();

        LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    }
 
Example #27
Source File: LocalRegistry.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void unRegister(ProviderConfig config) {
    String appName = config.getAppName();
    if (!registryConfig.isRegister()) { // 注册中心不注册
        if (LOGGER.isInfoEnabled(appName)) {
            LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
        }
        return;
    }
    if (!config.isRegister()) { // 服务不注册
        return;
    }
    List<ServerConfig> serverConfigs = config.getServer();
    if (CommonUtils.isNotEmpty(serverConfigs)) {
        for (ServerConfig server : serverConfigs) {
            String serviceName = LocalRegistryHelper.buildListDataId(config, server.getProtocol());
            ProviderInfo providerInfo = LocalRegistryHelper.convertProviderToProviderInfo(config, server);
            try {
                doUnRegister(serviceName, providerInfo);
                if (LOGGER.isInfoEnabled(appName)) {
                    LOGGER.infoWithApp(appName,
                        LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_UNPUB, serviceName, "1"));
                }
            } catch (Exception e) {
                LOGGER.errorWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_UNPUB, serviceName, "0"),
                    e);
            }
        }
    }
}
 
Example #28
Source File: SofaRegistry.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void unRegister(ProviderConfig config) {
    String appName = config.getAppName();
    if (!registryConfig.isRegister()) {
        // 注册中心不注册
        if (LOGGER.isInfoEnabled(appName)) {
            LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
        }
        return;
    }
    if (!config.isRegister()) {
        // 服务不注册
        return;
    }
    List<ServerConfig> serverConfigs = config.getServer();
    if (CommonUtils.isNotEmpty(serverConfigs)) {
        for (ServerConfig server : serverConfigs) {
            String serviceName = SofaRegistryHelper.buildListDataId(config, server.getProtocol());
            try {
                String groupId = config.getParameter(SofaRegistryConstants.SOFA_GROUP_KEY);
                groupId = groupId == null ? SofaRegistryHelper.SUBSCRIBER_LIST_GROUP_ID : groupId;
                doUnRegister(appName, serviceName, groupId);
                if (LOGGER.isInfoEnabled(appName)) {
                    LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_UNPUB,
                        serviceName, "1"));
                }
            } catch (Exception e) {
                LOGGER.errorWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_UNPUB,
                    serviceName, "0"), e);
            }
        }
    }
}
 
Example #29
Source File: GenericServerMain.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("generic-server");

    ServerConfig serverConfig2 = new ServerConfig()
        .setPort(22222)
        .setDaemon(false);

    ProviderConfig<TestGenericService> providerConfig = new ProviderConfig<TestGenericService>()
        .setApplication(applicationConfig)
        .setInterfaceId(TestGenericService.class.getName())
        .setRef(new TestGenericServiceImpl())
        .setServer(serverConfig2);
    providerConfig.export();
}
 
Example #30
Source File: SofaRpcApplication.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "unExport")
public ProviderConfig provider() {
    ServerConfig config = new ServerConfig().setProtocol("bolt").setPort(12200).setDaemon(true);

    ProviderConfig<SofaRpcDemoService> providerConfig = new ProviderConfig<SofaRpcDemoService>().setInterfaceId(SofaRpcDemoService.class
        .getName()).setRef(new SofaRpcDemoServiceImpl()).setServer(config);

    providerConfig.export();
    return providerConfig;
}