use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class FailoverClusterTest method testRpcDirectInvokeFromContextNotAllowed.
@Test(expected = SofaRouteException.class)
public void testRpcDirectInvokeFromContextNotAllowed() {
boolean prev = RpcConfigs.getBooleanValue(RpcOptions.RPC_CREATE_CONN_WHEN_ABSENT);
// disable create connection from context
RpcConfigs.putValue(RpcOptions.RPC_CREATE_CONN_WHEN_ABSENT, false);
try {
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();
RpcInvokeContext.getContext().setTargetURL("127.0.0.1:13900");
proxy.sayHello("x-demo-invoke", 1);
provider.unExport();
consumer.unRefer();
} finally {
RpcConfigs.putValue(RpcOptions.RPC_CREATE_CONN_WHEN_ABSENT, prev);
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class TripleServerTest method testSyncSampleService.
@Test
public void testSyncSampleService() {
ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("triple-server");
int port = 50052;
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
ProviderConfig<SampleService> providerConfig = new ProviderConfig<SampleService>().setApplication(applicationConfig).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(SampleService.class.getName()).setRef(new SampleService() {
@Override
public String hello(String name) {
return "Hello! " + name;
}
}).setServer(serverConfig);
providerConfig.export();
ConsumerConfig<SampleService> consumerConfig = new ConsumerConfig<SampleService>();
consumerConfig.setInterfaceId(SampleService.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("tri://127.0.0.1:" + port);
SampleService sampleService = consumerConfig.refer();
String reply = sampleService.hello("world");
Assert.assertNotNull(reply);
Assert.assertEquals(reply, "Hello! world");
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class Http2ClearTextBadRequestTest method testAll.
@Test
public void testAll() throws Exception {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12333).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).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();
ClientTransportConfig clientTransportConfig = new ClientTransportConfig();
clientTransportConfig.setProviderInfo(ProviderHelper.toProviderInfo("h2c://127.0.0.1:12333"));
Http2ClientTransport clientTransport = new Http2ClientTransport(clientTransportConfig);
clientTransport.connect();
{
// GET 图标
String url = "/favicon.ico";
FullHttpRequest httpRequest = buildHttpRequest(url);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
Assert.assertTrue(handler.content.length == 0);
}
{
// 其它未知命令
String url = "/com.alipay.sofa.rpc.server.http.HttpService/add";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.OPTIONS);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(400, handler.response.status().code());
Assert.assertTrue(StringUtils.isNotEmpty(getStringContent(handler)));
}
{
// HEAD 不存在的服务
String url = "/com.alipay.sofa.rpc.server.http.HttpService12313/add";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.HEAD);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
}
{
// HEAD 不存在的方法
String url = "/com.alipay.sofa.rpc.server.http.HttpService:uuu/xasdasdasd";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.HEAD);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
}
{
// HEAD 存在的方法
String url = "/com.alipay.sofa.rpc.server.http.HttpService:uuu/add";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.setMethod(HttpMethod.HEAD);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
}
{
// POST 异常:地址不对
String url = "/com.alipay.sofa";
FullHttpRequest httpRequest = buildHttpRequest(url);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(400, handler.response.status().code());
Assert.assertTrue(getStringContent(handler).contains("ip:port"));
}
{
// POST 未知序列化
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/asdasdas?code=xxx";
FullHttpRequest httpRequest = buildHttpRequest(url);
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(400, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
}
{
// POST 不存在的接口
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/echoPb";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
}
{
// POST 不存在的方法
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/adasdada";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(404, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
}
{
// POST 不传 HEAD_SERIALIZE_TYPE
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/echoPb";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, "application/x-protobuf");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
Assert.assertNotNull(getStringContent(handler));
EchoResponse response = EchoResponse.parseFrom(handler.content);
Assert.assertEquals("helloxxx", response.getMessage());
}
{
// POST 正常
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/echoPb";
FullHttpRequest httpRequest = buildHttpRequest(url);
httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
httpRequest.headers().add(RemotingConstants.HEAD_TARGET_APP, "serverApp1");
MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
Assert.assertEquals(200, handler.response.status().code());
EchoResponse response = EchoResponse.parseFrom(handler.content);
Assert.assertEquals("helloxxx", response.getMessage());
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class RestClientMain method main.
public static void main(String[] args) throws InterruptedException {
ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
ConsumerConfig<RestService> consumerConfig = new ConsumerConfig<RestService>().setApplication(application).setInterfaceId(RestService.class.getName()).setProtocol("rest").setBootstrap("rest").setDirectUrl("rest://127.0.0.1:8888").setTimeout(3000);
RestService helloService = consumerConfig.refer();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
while (true) {
try {
String s = helloService.add(22, "xxx");
LOGGER.warn("add {}", s);
s = helloService.query(22);
LOGGER.warn("get {}", s);
List<ExampleObj> es = new ArrayList<ExampleObj>();
es.add(new ExampleObj().setName("xxx").setId(1));
es.add(new ExampleObj().setName("yyy").setId(2));
List<ExampleObj> rs = helloService.objects(es);
LOGGER.warn("rs {}", rs.size());
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
try {
Thread.sleep(2000);
} catch (Exception e) {
}
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class RestServerMain method main.
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);
}
Aggregations