login registration using grpc and java

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’

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

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

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

grpc java

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

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

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

https://gangforcode.com/grpc-client-interceptor-with-java/

https://gangforcode.com/grpc-project-using-java/

Leave a Comment