Search in sources :

Example 1 with RegistryConfig

use of com.alipay.sofa.rpc.config.RegistryConfig in project sofa-rpc by sofastack.

the class SofaRegistryClient method main.

public static void main(String[] args) {
    /**
     * 运行时项目引入依赖
     *         <dependency>
     *             <groupId>com.alipay.sofa</groupId>
     *             <artifactId>registry-client-all</artifactId>
     *             <version>5.2.0</version>
     *         </dependency>
     */
    RegistryConfig registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_SOFA).setAddress("127.0.0.1:9603");
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRegistry(registryConfig).setProtocol("bolt").setConnectTimeout(10 * 1000);
    HelloService helloService = consumerConfig.refer();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
    try {
        while (true) {
            try {
                System.out.println(helloService.sayHello("world"));
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            }
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) HelloService(com.alipay.sofa.rpc.quickstart.HelloService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 2 with RegistryConfig

use of com.alipay.sofa.rpc.config.RegistryConfig in project sofa-rpc by sofastack.

the class ZookeeperRegistryTest method setUp.

@BeforeClass
public static void setUp() {
    registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK).setSubscribe(true).setAddress("127.0.0.1:2181").setRegister(true);
    registry = (ZookeeperRegistry) RegistryFactory.getRegistry(registryConfig);
    registry.init();
    Assert.assertTrue(registry.start());
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) BeforeClass(org.junit.BeforeClass)

Example 3 with RegistryConfig

use of com.alipay.sofa.rpc.config.RegistryConfig in project sofa-rpc by sofastack.

the class ZookeeperAuthBoltServerTest method testUseCorrentAuth.

@Test
public void testUseCorrentAuth() {
    parameters.put("scheme", "digest");
    // 如果存在多个认证信息,则在参数形式为为user1:passwd1,user2:passwd2
    parameters.put("addAuth", "sofazk:rpc1");
    registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK).setAddress("127.0.0.1:2181/authtest").setParameters(parameters);
    serverConfig = new ServerConfig().setProtocol(// 设置一个协议,默认bolt
    "bolt").setPort(// 设置一个端口,默认12200
    12200).setDaemon(// 非守护线程
    false);
    ProviderConfig<EchoService> providerConfig = new ProviderConfig<EchoService>().setRegistry(registryConfig).setInterfaceId(// 指定接口
    EchoService.class.getName()).setRef(// 指定实现
    new EchoServiceImpl()).setServer(// 指定服务端
    serverConfig);
    // 发布服务
    providerConfig.export();
    ConsumerConfig<EchoService> consumerConfig = new ConsumerConfig<EchoService>().setRegistry(registryConfig).setInterfaceId(// 指定接口
    EchoService.class.getName()).setProtocol(// 指定协议
    "bolt").setTimeout(3000).setConnectTimeout(10 * 1000);
    EchoService echoService = consumerConfig.refer();
    String result = echoService.echoStr("auth test");
    Assert.assertEquals("auth test", result);
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) EchoService(com.alipay.sofa.rpc.test.EchoService) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) EchoServiceImpl(com.alipay.sofa.rpc.test.EchoServiceImpl) Test(org.junit.Test) BaseZkTest(com.alipay.sofa.rpc.registry.base.BaseZkTest)

Example 4 with RegistryConfig

use of com.alipay.sofa.rpc.config.RegistryConfig in project sofa-rpc by sofastack.

the class RegistryFactory method destroyAll.

/**
 * 关闭全部注册中心
 */
public static void destroyAll() {
    for (Map.Entry<RegistryConfig, Registry> entry : ALL_REGISTRIES.entrySet()) {
        RegistryConfig config = entry.getKey();
        Registry registry = entry.getValue();
        try {
            registry.destroy();
            ALL_REGISTRIES.remove(config);
        } catch (Exception e) {
            LOGGER.error(LogCodes.getLog(LogCodes.ERROR_DESTRORY_REGISTRY, config), e);
        }
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)

Example 5 with RegistryConfig

use of com.alipay.sofa.rpc.config.RegistryConfig in project sofa-rpc by sofastack.

the class DubboServerTest method testRegistrySync.

@Test
public // 同步调用,走服务注册中心
void testRegistrySync() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(10).setPort(20880).setProtocol("dubbo").setQueues(100).setCoreThreads(1).setMaxThreads(2).setHost(SystemInfo.getLocalHost());
    // 发布一个服务,每个请求要执行1秒
    ApplicationConfig serverApplacation = new ApplicationConfig();
    serverApplacation.setAppName("server");
    List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
    RegistryConfig registryConfig;
    registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK).setAddress("127.0.0.1:2181").setSubscribe(true).setRegister(true);
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    MethodConfig methodConfig = new MethodConfig();
    methodConfig.setTimeout(3000);
    methodConfig.setName("sayHello");
    methodConfigs.add(methodConfig);
    registryConfigs.add(registryConfig);
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setServer(serverConfig).setRegister(true).setBootstrap("dubbo").setRegistry(registryConfigs).setApplication(serverApplacation);
    providerConfig.export();
    ApplicationConfig clientApplication = new ApplicationConfig();
    clientApplication.setAppName("client");
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setTimeout(30000).setRegister(true).setProtocol("dubbo").setApplication(clientApplication).setRegistry(registryConfigs).setMethods(methodConfigs).setInJVM(false);
    final HelloService demoService = consumerConfig.refer();
    String result = demoService.sayHello("xxx", 22);
    Assert.assertNotNull(result);
    ConsumerBootstrap bootstrap = consumerConfig.getConsumerBootstrap();
    Assert.assertTrue(bootstrap instanceof DubboConsumerBootstrap);
    Assert.assertTrue(bootstrap.isSubscribed());
    Assert.assertNotNull(bootstrap.getProxyIns());
    bootstrap.unRefer();
    try {
        bootstrap.getCluster();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof UnsupportedOperationException);
    }
    try {
        bootstrap.subscribe();
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof UnsupportedOperationException);
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ArrayList(java.util.ArrayList) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) BaseZkTest(com.alipay.sofa.rpc.registry.base.BaseZkTest)

Aggregations

RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)85 Test (org.junit.Test)39 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)25 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)21 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)15 HelloService (com.alipay.sofa.rpc.test.HelloService)12 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)11 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)9 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)9 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)8 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)8 Registry (com.alipay.sofa.rpc.registry.Registry)7 BaseZkTest (com.alipay.sofa.rpc.registry.base.BaseZkTest)6 EchoService (com.alipay.sofa.rpc.test.EchoService)6 Before (org.junit.Before)6 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)5 HashMap (java.util.HashMap)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 BeforeClass (org.junit.BeforeClass)5 SofaBootRpcRuntimeException (com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException)4