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);
}
}
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());
}
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);
}
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);
}
}
}
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);
}
}
Aggregations