use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class Http1ServerTest method testHttp1Json.
@Test
public void testHttp1Json() 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();
{
// POST jackson不存在的接口
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/adasdad";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
ExampleObj obj = new ExampleObj();
obj.setId(1);
obj.setName("xxx");
byte[] bytes = mapper.writeValueAsBytes(obj);
ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// POST 不存在的方法
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/adasdad";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
ExampleObj obj = new ExampleObj();
obj.setId(1);
obj.setName("xxx");
byte[] bytes = mapper.writeValueAsBytes(obj);
ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// POST 不传 HEAD_SERIALIZE_TYPE
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/object";
HttpPost httpPost = new HttpPost(url);
ExampleObj obj = new ExampleObj();
obj.setId(1);
obj.setName("xxx");
byte[] bytes = mapper.writeValueAsBytes(obj);
ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
ExampleObj result = mapper.readValue(data, ExampleObj.class);
Assert.assertEquals("xxxxx", result.getName());
}
{
// POST 正常请求
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/object";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
ExampleObj obj = new ExampleObj();
obj.setId(1);
obj.setName("xxx");
byte[] bytes = mapper.writeValueAsBytes(obj);
ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
ExampleObj result = mapper.readValue(data, ExampleObj.class);
Assert.assertEquals("xxxxx", result.getName());
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class ServerB method main.
public static void main(String[] args) {
// B服务里的C服务客户端
ConsumerConfig<ServiceC> consumerConfig = new ConsumerConfig<ServiceC>().setApplication(new ApplicationConfig().setAppName("BBB")).setInterfaceId(ServiceC.class.getName()).setDirectUrl("bolt://127.0.0.1:12299?appName=CCC").setRegister(false).setInvokeType(// 不设置,调用级别可设置
"callback").setTimeout(2000);
ServiceC serviceC = consumerConfig.refer();
ServerConfig serverConfig = new ServerConfig().setPort(12298).setDaemon(false);
ProviderConfig<ServiceB> providerConfig = new ProviderConfig<ServiceB>().setInterfaceId(ServiceB.class.getName()).setApplication(new ApplicationConfig().setAppName("BBB")).setRef(new ServiceBImpl(serviceC)).setServer(serverConfig).setRegister(false);
providerConfig.export();
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class ServerC method main.
public static void main(String[] args) {
ServerConfig serverConfig = new ServerConfig().setPort(12299).setDaemon(false);
ProviderConfig<ServiceC> providerConfig = new ProviderConfig<ServiceC>().setInterfaceId(ServiceC.class.getName()).setApplication(new ApplicationConfig().setAppName("CCC")).setRef(new ServiceCImpl(1000)).setServer(serverConfig).setRegister(false);
providerConfig.export();
}
use of com.alipay.sofa.rpc.config.ApplicationConfig 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);
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class DubboConsumerBootstrap method copyApplication.
private void copyApplication(ConsumerConfig<T> consumerConfig, ReferenceConfig<T> referenceConfig) {
ApplicationConfig applicationConfig = consumerConfig.getApplication();
com.alibaba.dubbo.config.ApplicationConfig dubboConfig = new com.alibaba.dubbo.config.ApplicationConfig();
dubboConfig.setName(applicationConfig.getAppName());
referenceConfig.setApplication(dubboConfig);
}
Aggregations