use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class FailoverClusterTest method testRetryLogicThrowException.
@Test(expected = SofaTimeOutException.class)
public void testRetryLogicThrowException() {
// one provider and retry twice
ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
// 发布一个服务,每个请求要执行2秒
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl() {
@Override
public String sayHello(String name, int age) {
try {
Thread.sleep(2000);
} catch (Exception ignore) {
}
LOGGER.info("xxxxxxxxxxxxxxxxx" + age);
return "hello " + name + " from server! age: " + age;
}
}).setServer(serverConfig).setRegister(false);
providerConfig.export();
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setCluster("failover").setTimeout(1000).setRetries(// 失败后自动重试2次
2).setRegister(false);
final HelloService helloService = consumerConfig.refer();
helloService.sayHello("xxx", 20);
}
use of com.alipay.sofa.rpc.config.ProviderConfig 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.ProviderConfig 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);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class SofaRegistryServer 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");
ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setPort(12200).setDaemon(false);
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setRegistry(registryConfig).setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setServer(serverConfig);
providerConfig.export();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class LazyBoltServerMain method main.
public static void main(String[] args) {
ServerConfig serverConfig = new ServerConfig().setPort(22100).setDaemon(false);
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl("result from 22100")).setServer(serverConfig).setRegister(false);
ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setRef(new EchoServiceImpl()).setServer(serverConfig).setRegister(false);
providerConfig.export();
providerConfig2.export();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
Aggregations