Giter Site home page Giter Site logo

jackstaff / grpc Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 440 KB

A java RPC framework , gRPC based, smooth and friendly use of gRPC, & spring boot starter

License: Apache License 2.0

Java 100.00%
grpc grpc-java java-rpc-framework java-rpc rpc proto microservices java spring-boot-starter

grpc's Introduction

Jackstaff gRPC framework

Quick Starts / Advance Usage / Smooth and friendly use of gRPC / Origin

Quick Starts:

Step 0: Import grpc-spring-boot-starter in pom.xml. jdk1.8

<!-- https://mvnrepository.com/artifact/org.jackstaff.grpc/grpc-spring-boot-starter -->
<dependency>
    <groupId>org.jackstaff.grpc</groupId>
    <artifactId>grpc-spring-boot-starter</artifactId>
    <version>2.0.13</version>
</dependency>

Step 1: Define protocol interface:

import java.util.function.Consumer;

public interface HelloService {

    String sayHello(String greeting); //Unary RPCs

    void lotsOfReplies(String greeting, Consumer<String> replies);//Server streaming RPCs

    Consumer<String> lotsOfGreetings(List<String> friends); //Client streaming RPCs

    Consumer<String> bidiHello(List<String> friends, Consumer<String> replies); //Bidirectional streaming RPCs

}

Step 2: Implements protocol interface(at "server side micro service" project):

import org.jackstaff.grpc.annotation.Server;
@Server(HelloService.class)
public class MyHelloService implements HelloService {

    @Override
    public String sayHello(String greeting) {
        //todo
        return "reply " + greeting;
    }

    @Override
    public void lotsOfReplies(String greeting, Consumer<String> replies) {
        //todo
        replies.accept("hi "+ greeting);
        replies.accept(":)");
    }

    @Override
    public Consumer<String> lotsOfGreetings(List<String> friends) {
        //todo
        return s-> System.out.println(s);
    }

    @Override
    public Consumer<String> bidiHello(List<String> friends, Consumer<String> replies) {
        //todo
        CompletableFuture.runAsync(()-> friends.forEach(friend->replies.accept("hi "+ friend)));
        return s-> System.out.println(s);
    }

}

Step 3: Use protocol interface (at "client side micro service" project):

import org.jackstaff.grpc.annotation.Client;

@Service
public class MyClientService {

    @Client("my-server") 
    private HelloService helloService;

    public String sayHello(String greeting){
        return helloService.sayHello(greeting);
    }
    
}

Step 4: Config "client side " application.yml:

spring:
  grpc:
    client:
      my-server:
        host: localhost
        port: 9000

Step 5: Config "server side " application.yml:

spring:
  grpc:
    server:
      port: 9000

done.

grpc's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.