In this article, we are going to create a login registration service using gRPC and Java. If you are completely new to gRPC with Java, then please check this article https://gangforcode.com/grpc-project-using-java/ first because there are some dependencies and plugins required for gRPC and proto files.
Database
For this project, i used MySQL and created a table named ‘registration’
mysql> create table registration(name varchar(100),emailId varchar(100),password varchar(100));
Query OK, 0 rows affected (0.28 sec)
I’m assuming that you already added all the required dependencies and plugins for gRPC. now let’s start with the proto file
For this project I created two endpoints one is login and another is registration
login-reg.proto
syntax="proto3";
package loginNRegistration;
option java_multiple_files = true;
service loginAndRegistration{
rpc Register(registration) returns (response);
rpc Login(login) returns (response);
}
message registration{
string name=1;
string emailId=2;
string password=3;
}
message response{
string reply=1;
}
message login{
string emailId=1;
string password=2;
}
Database connection with JDBC
Now we need to make a connection with MySQL database for storing and retrieving data for that I used jdbc. you need to add the following dependencies in your pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
Now create a package named ‘db’ and inside that package create a class named ‘dbConnection’ , inside this class write a method ‘mysqlConnection’ which returns Connection
dbConnection.java
package db;
import java.sql.Connection;
import java.sql.DriverManager;
public class dbConnection {
public static Connection mysqlConnection(){
Connection connection = null;
try
{
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/grpcDB?"+"user=root&password=root");
}
catch (Exception e){
System.out.println(e.getMessage());
}
return connection;
}
}
Now we need to implement a service for that first we need to generate proto stubs from a proto file, for that use the following mvn command
mvn clean install
auto generated code from proto file
Now we need to write our service class ‘serviceImpl’ by extending ‘loginAndRegistrationImplBase’
in the following code, I write the code for registration and login according to my requirement
serviceImpl.java
package service;
import db.dbConnection;
import io.grpc.stub.StreamObserver;
import loginNRegistration.login;
import loginNRegistration.loginAndRegistrationGrpc;
import loginNRegistration.registration;
import loginNRegistration.response;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class serviceImpl extends loginAndRegistrationGrpc.loginAndRegistrationImplBase {
Connection connection = dbConnection.mysqlConnection();
@Override
public void register(registration request, StreamObserver<response> responseObserver) {
String query = "insert into registration(name,emailId,password) values(?,?,?)";
try{
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1,request.getName());
preparedStatement.setString(2,request.getEmailId());
preparedStatement.setString(3,request.getPassword());
preparedStatement.execute();
System.out.println("registered");
} catch (SQLException e) {
throw new RuntimeException(e);
}
responseObserver.onNext(response.newBuilder().setReply("added sucessfully").build());
responseObserver.onCompleted();
}
@Override
public void login(login request, StreamObserver<response> responseObserver) {
Connection connection = dbConnection.mysqlConnection();
String query = "Select * from registration where emailId = ? and password = ?";
String name="";
try{
PreparedStatement statement= connection.prepareStatement(query);
statement.setString(1,request.getEmailId());
statement.setString(2,request.getPassword());
ResultSet resultSet = statement.executeQuery();
if(resultSet.next()){
name = resultSet.getString("name");
}
else{
responseObserver.onNext(response.newBuilder().setReply("invalid credintials").build());
responseObserver.onCompleted();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
responseObserver.onNext(response.newBuilder().setReply("hiii "+ name).build());
responseObserver.onCompleted();
}
}
now we need to add this service on grpc server, for that, I used ‘Server.java’, this is the same server-class i used in my previous grpc projects
Server.java
import io.grpc.ServerBuilder;
import service.serviceImpl;
import java.io.IOException;
public class Server {
public static void main(String[] args) throws IOException, InterruptedException {
int port = 8000;
io.grpc.Server server = ServerBuilder.forPort(port).addService(new serviceImpl()).build().start();
System.out.println("server started at port: "+port );
server.awaitTermination();
}
}
it’s time to run the server and test the endpoints with bloomRPC.
first, we are going to test ‘Register’ endpoint.
after successful registration we got a response like “added sucesssfully”
Let’s check the ‘login’ endpoint
here response is “Invalid credintials” because password is wrong
now check with the correct password
now we are getting a proper response
You can check the following video for testing endpoints with bloomRPC
You can download this project from git with this link
that’s all for this project you can also check the following gRPC tutorials with java