forked from sofastack/sofa-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
25 changed files
with
1,860 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
example/src/main/java/com/alipay/sofa/rpc/grpc/examples/registry/GreeterImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* 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.grpc.registry; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
import java.time.LocalDateTime; | ||
|
||
import io.grpc.examples.helloworld.GreeterGrpc; | ||
import io.grpc.examples.helloworld.HelloRequest; | ||
import io.grpc.examples.helloworld.HelloReply; | ||
import io.grpc.stub.StreamObserver; | ||
|
||
class GreeterImpl extends GreeterGrpc.GreeterImplBase { | ||
|
||
//Intentionally using unsupported format | ||
static final DateTimeFormatter[] datetimeFormatter = new DateTimeFormatter[] { DateTimeFormatter.ISO_DATE_TIME, | ||
DateTimeFormatter.ISO_LOCAL_DATE_TIME, | ||
DateTimeFormatter.BASIC_ISO_DATE }; | ||
|
||
@Override | ||
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) { | ||
HelloRequest.DateTime reqDateTime = req.getDateTime(); | ||
int i = 0; | ||
try { | ||
i = Integer.parseInt(reqDateTime.getTime()); | ||
} catch (Exception e) { | ||
//TODO: handle exception | ||
} | ||
LocalDateTime dt = LocalDateTime.now(); | ||
String dtStr = dt.format(datetimeFormatter[i % datetimeFormatter.length]); | ||
HelloRequest.DateTime rplyDateTime = HelloRequest.DateTime.newBuilder(reqDateTime) | ||
.setDate(dtStr).build(); | ||
HelloReply reply = HelloReply.newBuilder() | ||
.setMessage("Hello " + req.getName()) | ||
.setDateTime(rplyDateTime) | ||
.build(); | ||
responseObserver.onNext(reply); | ||
responseObserver.onCompleted(); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
...c/main/java/com/alipay/sofa/rpc/grpc/examples/registry/GrpcClientRegistryApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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.grpc.registry; | ||
|
||
import com.alipay.sofa.rpc.config.RegistryConfig; | ||
import com.alipay.sofa.rpc.config.ConsumerConfig; | ||
import com.alipay.sofa.rpc.grpc.registry.GreeterImpl; | ||
import com.alipay.sofa.rpc.log.Logger; | ||
import com.alipay.sofa.rpc.log.LoggerFactory; | ||
|
||
import io.grpc.StatusRuntimeException; | ||
import io.grpc.examples.helloworld.GreeterGrpc; | ||
import io.grpc.examples.helloworld.GreeterGrpc; | ||
import io.grpc.examples.helloworld.HelloReply; | ||
import io.grpc.examples.helloworld.HelloRequest; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
import java.time.LocalDateTime; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Luan Yanqiang</a> | ||
*/ | ||
public class GrpcClientRegistryApplication { | ||
|
||
static final DateTimeFormatter[] datetimeFormatter = new DateTimeFormatter[] { DateTimeFormatter.ISO_DATE_TIME, | ||
DateTimeFormatter.ISO_LOCAL_DATE_TIME, | ||
DateTimeFormatter.BASIC_ISO_DATE }; | ||
|
||
public static void main(String[] args) { | ||
final Logger LOGGER = LoggerFactory.getLogger(GrpcClientRegistryApplication.class); | ||
|
||
RegistryConfig registryConfig = new RegistryConfig(); | ||
registryConfig.setProtocol("zookeeper").setAddress("127.0.0.1:2181"); | ||
|
||
ConsumerConfig<GreeterGrpc.GreeterBlockingStub> consumerConfig = new ConsumerConfig<GreeterGrpc.GreeterBlockingStub>(); | ||
consumerConfig.setInterfaceId(GreeterGrpc.class.getName()) | ||
.setProtocol("grpc") | ||
.setRegistry(registryConfig); | ||
|
||
// GreeterGrpc.GreeterBlockingStub s = new | ||
GreeterGrpc.GreeterBlockingStub greeterBlockingStub = (GreeterGrpc.GreeterBlockingStub) consumerConfig.refer(); | ||
|
||
LOGGER.info("Grpc stub bean successful: {}", greeterBlockingStub.getClass().getName()); | ||
|
||
LOGGER.info("Will try to greet " + "world" + " ..."); | ||
HelloRequest.DateTime dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00") | ||
.build(); | ||
HelloRequest request = HelloRequest.newBuilder().setName("world").build(); | ||
HelloReply reply = null; | ||
try { | ||
for (int i = 0; i < 10000; i++) { | ||
try { | ||
HelloRequest.DateTime reqDateTime = HelloRequest.DateTime.newBuilder(dateTime).setTime("" + i) | ||
.build(); | ||
request = HelloRequest.newBuilder(request).setName("world_" + i).setDateTime(reqDateTime).build(); | ||
reply = greeterBlockingStub.sayHello(request); | ||
LOGGER.info("Greeting: {}, {}", reply.getMessage(), reply.getDateTime().getDate()); | ||
// Object r = greeterBlockingStub.sayHello(request); | ||
// LOGGER.info("Greeting: {}, {}", r.toString(), r.toString()); | ||
} catch (StatusRuntimeException e) { | ||
LOGGER.error("RPC failed: {}", e.getStatus()); | ||
} catch (Throwable e) { | ||
LOGGER.error("Unexpected RPC call breaks", e); | ||
} | ||
|
||
try { | ||
Thread.sleep(2000); | ||
} catch (Exception e) { | ||
} | ||
} | ||
} catch (Exception e) { | ||
LOGGER.error("Unexpected RPC call breaks", e); | ||
} | ||
|
||
synchronized (GrpcClientRegistryApplication.class) { | ||
try { | ||
while (true) { | ||
GrpcClientRegistryApplication.class.wait(); | ||
} | ||
} catch (InterruptedException e) { | ||
LOGGER.error("Exit by Interrupted"); | ||
} | ||
} | ||
|
||
consumerConfig.unRefer(); | ||
|
||
} | ||
} |
Oops, something went wrong.