Skip to content

Commit

Permalink
Feat/triple common (sofastack#933)
Browse files Browse the repository at this point in the history
* support other serializer POC

* change the way to get methodName

* support custom serialization
  • Loading branch information
OrezzerO authored Jun 10, 2020
1 parent ade8295 commit ec569f2
Show file tree
Hide file tree
Showing 27 changed files with 3,077 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import com.alipay.sofa.rpc.bootstrap.DefaultProviderBootstrap;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.proxy.ProxyFactory;
import com.alipay.sofa.rpc.server.ProviderProxyInvoker;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
Expand All @@ -32,6 +35,8 @@
@Extension("tri")
public class TripleProviderBootstrap<T> extends DefaultProviderBootstrap<T> {

private static final Logger LOGGER = LoggerFactory.getLogger(TripleProviderBootstrap.class);

/**
* 构造函数
*
Expand All @@ -49,6 +54,11 @@ protected void preProcessProviderTarget(ProviderConfig providerConfig, ProviderP
Object obj = ProxyFactory.buildProxy(providerConfig.getProxy(), providerConfig.getProxyClass(),
providerProxyInvoker);
method.invoke(providerConfig.getRef(), obj);
} catch (NoSuchMethodException e) {
LOGGER
.info(
"{} don't hava method setProxiedImpl, will treated as origin provider service instead of grpc service.",
implClass);
} catch (Exception e) {
throw new IllegalStateException(
"Failed to set sofa proxied service impl to stub, please make sure your stub "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.triple;

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.config.ApplicationConfig;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.RegistryConfig;
import com.alipay.sofa.rpc.config.ServerConfig;
import com.alipay.sofa.rpc.context.RpcRunningState;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import io.grpc.StatusRuntimeException;

/**
* @author zhaowang
* @version : GenericTripleDemo.java, v 0.1 2020年05月28日 3:15 下午 zhaowang Exp $
*/
public class GenericTripleDemo {
private static final Logger LOGGER = LoggerFactory.getLogger(GenericTripleDemo.class);

public static void main(String[] args) {
RpcRunningState.setDebugMode(true);

ApplicationConfig clientApp = new ApplicationConfig().setAppName("triple-client");

ApplicationConfig serverApp = new ApplicationConfig().setAppName("triple-server");

int port = 50052;
if (args.length != 0) {
LOGGER.debug("first arg is {}", args[0]);
port = Integer.valueOf(args[0]);
}

RegistryConfig registryConfig = new RegistryConfig()
.setProtocol("zookeeper")
.setAddress("127.0.0.1:2181");

ServerConfig serverConfig = new ServerConfig()
.setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE)
.setPort(port);

ProviderConfig<OriginHello> providerConfig = new ProviderConfig<OriginHello>()
.setApplication(serverApp)
.setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE)
.setInterfaceId(OriginHello.class.getName())
.setRef(new OriginHelloImpl())
.setServer(serverConfig)
.setRegistry(registryConfig);

providerConfig.export();

ConsumerConfig<OriginHello> consumerConfig = new ConsumerConfig<OriginHello>();
consumerConfig.setInterfaceId(OriginHello.class.getName())
.setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE)
.setRegistry(registryConfig)
.setApplication(clientApp);

OriginHello helloService = consumerConfig.refer();

LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());

LOGGER.info("Will try to greet " + "world" + " ...");
while (true) {
try {
try {
HelloRequest1 helloRequest1 = new HelloRequest1();
helloRequest1.setName("ab");
HelloRequest2 helloRequest2 = new HelloRequest2();
helloRequest2.setName("cd");
HelloResponse result = helloService.hello2(helloRequest1, helloRequest2);
LOGGER.info("Invoke Success,hello: {} ", result.getMessage());
} catch (StatusRuntimeException e) {
LOGGER.error("RPC failed: {}", e.getStatus());
} catch (Throwable e) {
LOGGER.error("Unexpected RPC call breaks", e);
}
} catch (Exception e) {
LOGGER.error("Unexpected RPC call breaks", e);
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.triple;

/**
* @author zhaowang
* @version : HelloRequest1.java, v 0.1 2020年05月28日 7:21 下午 zhaowang Exp $
*/
public class HelloRequest1 {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.triple;

/**
* @author zhaowang
* @version : HelloRequest2.java, v 0.1 2020年05月28日 7:21 下午 zhaowang Exp $
*/
public class HelloRequest2 {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.triple;

/**
* @author zhaowang
* @version : HelloResponse.java, v 0.1 2020年05月28日 7:21 下午 zhaowang Exp $
*/
public class HelloResponse {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
32 changes: 32 additions & 0 deletions example/src/test/java/com/alipay/sofa/rpc/triple/OriginHello.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.triple;

import io.grpc.examples.helloworld.HelloReply;
import io.grpc.examples.helloworld.HelloRequest;

/**
* @author zhaowang
* @version : OriginHello.java, v 0.1 2020年05月28日 3:10 下午 zhaowang Exp $
*/
public interface OriginHello {
String hello0();

String hello1(String hello);

HelloResponse hello2(HelloRequest1 helloRequest, HelloRequest2 helloReply);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.triple;

/**
* @author zhaowang
* @version : OriginHelloImpl.java, v 0.1 2020年05月28日 3:12 下午 zhaowang Exp $
*/
public class OriginHelloImpl implements OriginHello {
@Override
public String hello0() {
return "hello0" + 123;
}

@Override
public String hello1(String hello) {
return "hello, " + hello;
}

@Override
public HelloResponse hello2(HelloRequest1 helloRequest, HelloRequest2 request2) {
HelloResponse helloResponse = new HelloResponse();
helloResponse.setMessage(helloRequest.getName() + request2.getName());
return helloResponse;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,28 @@ public static void main(String[] args) {
.build();
HelloRequest request = HelloRequest.newBuilder().setName("world").build();
HelloReply reply = null;
try {
while (true) {
try {
HelloRequest.DateTime reqDateTime = HelloRequest.DateTime.newBuilder(dateTime).setTime("")
.build();
request = HelloRequest.newBuilder(request).setName("world").setDateTime(reqDateTime).build();
reply = greeterBlockingStub.sayHello(request);
LOGGER.info("Invoke Success,Greeting: {}, {}", reply.getMessage(), reply.getDateTime().getDate());
} catch (StatusRuntimeException e) {
LOGGER.error("RPC failed: {}", e.getStatus());
} catch (Throwable e) {
try {
HelloRequest.DateTime reqDateTime = HelloRequest.DateTime.newBuilder(dateTime).setTime("")
.build();
request = HelloRequest.newBuilder(request).setName("world").setDateTime(reqDateTime).build();
reply = greeterBlockingStub.sayHello(request);
LOGGER.info("Invoke Success,Greeting: {}, {}", reply.getMessage(), reply.getDateTime().getDate());
} catch (StatusRuntimeException e) {
LOGGER.error("RPC failed: {}", e.getStatus());
} catch (Throwable e) {
LOGGER.error("Unexpected RPC call breaks", e);
}
} catch (Exception e) {
LOGGER.error("Unexpected RPC call breaks", e);
}
} catch (Exception e) {
LOGGER.error("Unexpected RPC call breaks", e);
}

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import io.grpc.examples.helloworld.HelloRequest;
import io.grpc.examples.helloworld.SofaGreeterTriple;
import io.grpc.stub.StreamObserver;
import org.springframework.util.ClassUtils;

import java.util.Set;

public class TripleGreeterImpl extends SofaGreeterTriple.GreeterImplBase {

Expand Down
Loading

0 comments on commit ec569f2

Please sign in to comment.