# spring-cloud-starter-grpc
**Repository Path**: zhangjingl02/spring-cloud-starter-grpc-parent
## Basic Information
- **Project Name**: spring-cloud-starter-grpc
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2018-10-13
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# spring-cloud-starter-grpc-parent
#### 项目介绍
spring-cloud-starter-grpc
目前只实现了基于consul服务注册与发现,后续不断完善
#### 使用说明
1. 添加依赖
```xml
com.vlefen
spring-cloud-starter-grpc
${project.version}
```
2. 服务端
``` java
import com.vlefen.springcloud.grpc.EnableGRpc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableGRpc
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app=new SpringApplication(Application.class);
app.run(args);
}
}
```
3. 服务端实现
```java
import com.vlefen.springcloud.grpc.GRpc;
import io.grpc.examples.helloworld.GreeterGrpc;
import io.grpc.examples.helloworld.HelloReply;
import io.grpc.examples.helloworld.HelloRequest;
import io.grpc.stub.StreamObserver;
import org.springframework.stereotype.Service;
@Service
@GRpc
public class GreeterImpl extends GreeterGrpc.GreeterImplBase{
@Override
public void sayHello(HelloRequest request, StreamObserver responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + request.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
```
4. 客户端
```java
import com.vlefen.springcloud.grpc.EnableGRpc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableGRpc
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app=new SpringApplication(Application.class);
app.run(args);
}
}
```
5. 客户端引用
```java
import com.vlefen.springcloud.grpc.GRpc;
import com.vlefen.springcloud.grpc.grpc.GRpcType;
import io.grpc.examples.helloworld.GreeterGrpc;
import io.grpc.examples.helloworld.HelloRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Rest {
@Autowired
@GRpc(serviceDefinition = GreeterGrpc.class,type = GRpcType.BlockingStub)
private GreeterGrpc.GreeterBlockingStub blockingStub;
@GetMapping("/")
public String test(@RequestParam String name){
return blockingStub.sayHello(HelloRequest.newBuilder().setName(name).build()).getMessage();
}
}
```
#### 参与贡献