use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class MeshRegistryTest method testOnlyPublish.
@Test
public void testOnlyPublish() throws InterruptedException {
Field registedAppField = null;
try {
registedAppField = MeshRegistry.class.getDeclaredField("registedApp");
registedAppField.setAccessible(true);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
Boolean registedAppValue = null;
// in case of effected by other case.
try {
registedAppValue = (Boolean) registedAppField.get(registry);
registedAppField.set(registry, false);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
registry.register(provider);
Thread.sleep(3000);
try {
registedAppValue = (Boolean) registedAppField.get(registry);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
LOGGER.info("final registedAppValue is " + registedAppValue);
Assert.assertTrue(registedAppValue);
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class NacosRegistryHelperTest method convertProviderToInstances.
@Test
public void convertProviderToInstances() {
ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId("com.alipay.xxx.TestService").setApplication(new ApplicationConfig().setAppName("test-server")).setUniqueId("nacos-test").setProxy("javassist").setRegister(true).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
List<Instance> instances = NacosRegistryHelper.convertProviderToInstances(provider);
assertNotNull(instances);
assertEquals(1, instances.size());
Instance instance = instances.get(0);
assertNotNull(instance);
assertEquals(NacosRegistryHelper.DEFAULT_CLUSTER, instance.getClusterName());
assertEquals(serverConfig.getPort(), instance.getPort());
assertEquals(serverConfig.getProtocol(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_PROTOCOL));
assertEquals(provider.getSerialization(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_SERIALIZATION));
assertEquals(provider.getUniqueId(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_UNIQUEID));
assertEquals(provider.getWeight(), Integer.parseInt(instance.getMetadata().get(RpcConstants.CONFIG_KEY_WEIGHT)));
assertEquals(provider.getTimeout(), Integer.parseInt(instance.getMetadata().get(RpcConstants.CONFIG_KEY_TIMEOUT)));
assertEquals(provider.getSerialization(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_SERIALIZATION));
assertEquals(provider.getAppName(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_APP_NAME));
assertEquals("com.alipay.xxx.TestService:nacos-test:DEFAULT", instance.getServiceName());
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class ConsulRegistryAclTest method providerConfig.
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;
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class BoltExceptionTest method testAll.
@Test
public void testAll() {
final String directUrl = "bolt://127.0.0.1:12300";
final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl(directUrl).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setBootstrap("bolt").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
HelloService helloService = consumerConfig.refer();
// 关闭后再调用一个抛异常
try {
helloService.sayHello("xx", 22);
} catch (Exception e) {
// 应该抛出异常
Assert.assertTrue(e instanceof SofaRouteException);
Assert.assertTrue(e.getMessage().contains(directUrl));
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class Http1ServerTest method testHttp1General.
@Test
public void testHttp1General() throws Exception {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_HTTP).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setUniqueId("uuu").setRegister(false);
providerConfig.export();
HttpClient httpclient = HttpClientBuilder.create().build();
{
// GET 图标
String url = "http://127.0.0.1:12300/favicon.ico";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
Assert.assertTrue(StringUtils.isEmpty(getStringContent(httpResponse)));
}
{
// 其它未知命令
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/add";
HttpOptions httpGet = new HttpOptions(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertTrue(StringUtils.isNotEmpty(getStringContent(httpResponse)));
}
{
// HEAD 不存在的服务
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/add";
HttpHead httpHead = new HttpHead(url);
HttpResponse httpResponse = httpclient.execute(httpHead);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
}
{
// HEAD 不存在的方法
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/xasdasdasd";
HttpHead httpHead = new HttpHead(url);
HttpResponse httpResponse = httpclient.execute(httpHead);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
}
{
// HEAD 存在的方法
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/add";
HttpHead httpHead = new HttpHead(url);
HttpResponse httpResponse = httpclient.execute(httpHead);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
}
{
// GET 异常:地址不对
String url = "http://127.0.0.1:12300/com.alipay.sofa";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// GET 不存在的接口
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/asdasdas?code=xxx";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
Assert.assertTrue(getStringContent(httpResponse).contains("asdasdas"));
}
{
// GET 不存在的方法
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/asdasdas?code=xxx";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
Assert.assertTrue(getStringContent(httpResponse).contains("asdasdas"));
}
{
// GET 异常:参数数量不对
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/query";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// GET 异常:参数数量不对
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/add?code=1";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// GET 异常:参数类型不对
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/query?code=xxx";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// GET 正确
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/add?code=1&name=22";
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpclient.execute(httpGet);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
Assert.assertEquals("221", getStringContent(httpResponse));
}
{
// POST 未知序列化
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/adasdad";
HttpPost httpPost = new HttpPost(url);
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
ByteArrayEntity entity = new ByteArrayEntity(request.toByteArray(), null);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(400, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
}
Aggregations