ntc-jgrpc

ntc-jgrpc is a example java gRPC

License

License

Categories

Categories

gRPC Net Networking
GroupId

GroupId

com.streetcodevn
ArtifactId

ArtifactId

ntc-jgrpc
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

ntc-jgrpc
ntc-jgrpc is a example java gRPC
Project URL

Project URL

https://github.com/congnghia0609/ntc-jgrpc
Source Code Management

Source Code Management

https://github.com/congnghia0609/ntc-jgrpc/tree/master

Download ntc-jgrpc

How to add to project

<!-- https://jarcasting.com/artifacts/com.streetcodevn/ntc-jgrpc/ -->
<dependency>
    <groupId>com.streetcodevn</groupId>
    <artifactId>ntc-jgrpc</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.streetcodevn/ntc-jgrpc/
implementation 'com.streetcodevn:ntc-jgrpc:1.0.0'
// https://jarcasting.com/artifacts/com.streetcodevn/ntc-jgrpc/
implementation ("com.streetcodevn:ntc-jgrpc:1.0.0")
'com.streetcodevn:ntc-jgrpc:jar:1.0.0'
<dependency org="com.streetcodevn" name="ntc-jgrpc" rev="1.0.0">
  <artifact name="ntc-jgrpc" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.streetcodevn', module='ntc-jgrpc', version='1.0.0')
)
libraryDependencies += "com.streetcodevn" % "ntc-jgrpc" % "1.0.0"
[com.streetcodevn/ntc-jgrpc "1.0.0"]

Dependencies

compile (7)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar 1.2.3
ch.qos.logback : logback-core jar 1.2.3
org.slf4j : slf4j-api jar 1.7.30
com.streetcodevn : ntc-configuration jar 1.0.0
io.grpc : grpc-netty-shaded jar 1.30.0
io.grpc : grpc-protobuf jar 1.30.0
io.grpc : grpc-stub jar 1.30.0

provided (1)

Group / Artifact Type Version
org.apache.tomcat : annotations-api jar 6.0.53

test (1)

Group / Artifact Type Version
junit : junit jar 4.13

Project Modules

There are no modules declared in this project.

ntc-jgrpc

ntc-jgrpc is an example java gRPC

Maven

<dependency>
    <groupId>com.streetcodevn</groupId>
    <artifactId>ntc-jgrpc</artifactId>
    <version>1.0.0</version>
</dependency>

gRPC Server

Example CalculatorService Server:

public class MainApp {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            // 2. GServer
            GServer gs = new GServer("tutorial", new CalculatorImpl());
            gs.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

gRPC Client

Example CalculatorService Client:

public class CalGClient {
    private static final Logger log = LoggerFactory.getLogger(CalGClient.class);
    
    private String name;
    private GClient gc;

    public String getName() {
        return name;
    }

    public GClient getGClient() {
        return gc;
    }

    public CalGClient(String name) throws SSLException {
        this.name = name;
        gc = GClient.getInstance(name);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            CalGClient cgc = new CalGClient("tutorial");
            
            cgc.callSum();
            
            cgc.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void shutdown() {
        if (gc != null) {
            gc.shutdown();
        }
    }

    public void callSum() {
        try {
            System.out.println("Call sum...");
            gc = GClient.getInstance(name);
            CalculatorServiceGrpc.CalculatorServiceBlockingStub stub = CalculatorServiceGrpc.newBlockingStub(gc.getChannel());
            SumRequest req = SumRequest.newBuilder().setNum1(3).setNum2(5).build();
            SumResponse resp = stub.sum(req);
            log.info("sum api response " + resp.getResult());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

HAProxy Config Load Balancer for gRPC Server

frontend grpc_fe
	bind *:3330 ssl crt /ssl/haproxy.pem alpn h2
	option http-use-htx
    option logasap
	default_backend grpc_be

backend grpc_be
	balance roundrobin
	server grpc-go 127.0.0.1:3333 check ssl alpn h2 ca-file /ssl/server.crt
	server grpc-java 127.0.0.1:3334 check ssl alpn h2 ca-file /ssl/server.crt

NginX Config Load Balancer for gRPC Server, mode Non-SSL

Reference document at NginX blog and Module ngx_http_grpc_module

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  1800;

    map $http_upgrade $connection_upgrade {
         default upgrade;
         ''      close;
    }

    upstream grpcservers {
        server 127.0.0.1:3333;
        server 127.0.0.1:3334;
    }

    server {
        listen 3330 http2;

        # Can use location detail with a path of gRPC Service. Ex: /helloworld.Greeter
        location /ngrpc.CalculatorService {
            # The 'grpc://' ==> Non-SSL gRPC
            # The 'grpcs://' ==> SSL gRPC
            grpc_pass grpc://grpcservers;
        }
    }
}

NginX Config Load Balancer for gRPC Server, mode SSL

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  1800;

    map $http_upgrade $connection_upgrade {
         default upgrade;
         ''      close;
    }

    upstream grpcservers {
        server 127.0.0.1:3333;
        server 127.0.0.1:3334;
    }

    server {
        listen 3330 ssl http2;

        ## Create a certificate that points to the hostname.
        ## $ openssl req -nodes -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -subj '/CN=nginx'
        ssl_certificate /ssl/server.crt;
        ssl_certificate_key /ssl/server.pem;

        # Can use location detail with a path of gRPC Service. Ex: /helloworld.Greeter
        location /ngrpc.CalculatorService {
            # The 'grpc://' ==> Non-SSL gRPC
            # The 'grpcs://' ==> SSL gRPC
            grpc_pass grpcs://grpcservers;
        }
    }
}

License

This code is under the Apache License v2.

Versions

Version
1.0.0