andersonalmada2

Untitled

Nov 29th, 2022
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. package client;
  2.  
  3. import io.grpc.ManagedChannel;
  4. import io.grpc.ManagedChannelBuilder;
  5. import proto.UserOuterClass.CreateUserRequest;
  6. import proto.UserOuterClass.CreateUserResponse;
  7. import proto.UserOuterClass.GetUserRequest;
  8. import proto.UserOuterClass.GetUserResponse;
  9. import proto.UserOuterClass.ListUserRequest;
  10. import proto.UserOuterClass.ListUserResponse;
  11. import proto.UserOuterClass.User;
  12. import proto.UserServiceGrpc;
  13.  
  14. public class UserClient {
  15.  
  16.     public static void main(String[] args) {
  17.         UserClient main = new UserClient();
  18.        
  19.         ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 50051).usePlaintext().build();
  20.         UserServiceGrpc.UserServiceBlockingStub userClient = UserServiceGrpc.newBlockingStub(channel);
  21.  
  22.         // CREATE USER
  23.         User user = User.newBuilder().setName("luiz").setEmail("teste@teste.com").build();
  24.         CreateUserResponse createUserResponse = userClient
  25.                 .createUser(CreateUserRequest.newBuilder().setUser(user).build());
  26.         System.out.println(createUserResponse.toString());
  27.  
  28.         int userId = createUserResponse.getUser().getId();
  29.        
  30.         // Get USER
  31.         GetUserResponse getUserResponse = userClient.getUser(GetUserRequest.newBuilder().setUserId(userId).build());
  32.         System.out.println(getUserResponse.getUser());
  33.  
  34.         // DELETE USER
  35.         //DeleteUserResponse deleteUserResponse = userClient
  36.         //      .deleteUser(DeleteUserRequest.newBuilder().setUserId(userId).build());
  37.         //System.out.println(deleteUserResponse.getUserId());
  38.  
  39.         // LIST USERS
  40.         ListUserResponse listUserResponse = userClient.listUser(ListUserRequest.newBuilder().build());
  41.         System.out.println(listUserResponse.getUserList());
  42.     }
  43. }
  44.  
Add Comment
Please, Sign In to add comment