### Install Buf for Proto Generation Source: https://github.com/zhihu/fust/blob/main/examples/fust-boot-example/README.md Command to install the 'buf' tool using Homebrew. Buf is used for managing and generating code from Protocol Buffers definitions. ```shell brew install bufbuild/buf/buf ``` -------------------------------- ### Create MySQL Database and Table Source: https://github.com/zhihu/fust/blob/main/examples/fust-boot-example/README.md SQL script to create the 'db1' database and the 'yd_user' table with specified schema. This is a prerequisite for the application's data storage. ```sql create database db1; CREATE TABLE `yd_user` ( `id` bigint NOT NULL AUTO_INCREMENT, `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间', `birthday` date NOT NULL, `name` varchar(64) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ``` -------------------------------- ### Build and Run gRPC Service with Maven and Shell Scripts Source: https://github.com/zhihu/fust/blob/main/examples/fust-boot-example/README.md Shell commands to package the Java project using Maven and then execute a build script. Followed by running the gRPC service using a shell script, specifying the service path. ```shell mvn package bash build.sh ``` ```shell bash run.sh services/felis-boot-example-grpc ``` -------------------------------- ### Generate gRPC Proto Code with Buf Source: https://github.com/zhihu/fust/blob/main/examples/fust-boot-example/README.md Shell commands to update buf modules and generate gRPC code from proto definitions. This step is crucial for gRPC service communication. ```shell cd proto buf mod update cd .. # will generate proto code in felis-boot-example-grpc/gen-src buf generate proto ``` -------------------------------- ### Get User gRPC Service (Java) Source: https://github.com/zhihu/fust/blob/main/fust-docs/guide/grpc-service.md Implements the 'getUser' RPC method to retrieve a user by their ID. It fetches the user from the UserService and returns the user details or a 'not found' error. ```Java @Override public void getUser(GetUserRequest request, StreamObserver responseObserver) { try { long userId = request.getId(); log.info("Getting user by ID: {}", userId); UserModel userModel = userService.getUserById(userId); User response = UserProtoConverter.toProto(userModel); responseObserver.onNext(response); responseObserver.onCompleted(); } catch (UserNotFoundException e) { log.warn("User not found: {}", request.getId()); responseObserver.onError(new StatusRuntimeException(Status.NOT_FOUND .withDescription(e.getMessage()))); } catch (Exception e) { log.error("Error getting user", e); responseObserver.onError(new StatusRuntimeException(Status.INTERNAL .withDescription("Internal error: " + e.getMessage()))); } } ``` -------------------------------- ### Create User gRPC Service (Java) Source: https://github.com/zhihu/fust/blob/main/fust-docs/guide/grpc-service.md Implements the 'createUser' RPC method to handle user creation requests. It validates input, interacts with the UserService, and returns the created user or an error. ```Java @Override public void createUser(CreateUserRequest request, StreamObserver responseObserver) { try { log.info("Creating user: {}", request.getName()); UserModel userModel = UserProtoConverter.toModel(request); UserModel createdUserModel = userService.createUser(userModel); User response = UserProtoConverter.toProto(createdUserModel); responseObserver.onNext(response); responseObserver.onCompleted(); } catch (DateTimeParseException e) { log.warn("Invalid date format for birthday: {}", request.getBirthday()); responseObserver.onError(new StatusRuntimeException(Status.INVALID_ARGUMENT .withDescription("Invalid date format. Expected yyyy-MM-dd but got: " + request.getBirthday()))); } catch (Exception e) { log.error("Error creating user", e); responseObserver.onError(new StatusRuntimeException(Status.INTERNAL .withDescription("Internal error: " + e.getMessage()))); } } ``` -------------------------------- ### Update User gRPC Service (Java) Source: https://github.com/zhihu/fust/blob/main/fust-docs/guide/grpc-service.md Implements the 'updateUser' RPC method to modify an existing user. It checks for user existence, updates the user via UserService, and returns the updated user or an error. ```Java @Override public void updateUser(UpdateUserRequest request, StreamObserver responseObserver) { try { long userId = request.getId(); log.info("Updating user: {}", userId); // 确保用户存在 userService.getUserById(userId); UserModel userModel = UserProtoConverter.toModel(request); boolean updated = userService.updateUser(userModel); if (updated) { User response = UserProtoConverter.toProto(userModel); responseObserver.onNext(response); responseObserver.onCompleted(); } else { responseObserver.onError(new StatusRuntimeException(Status.INTERNAL .withDescription("Failed to update user"))); } } catch (UserNotFoundException e) { log.warn("User not found: {}", request.getId()); responseObserver.onError(new StatusRuntimeException(Status.NOT_FOUND .withDescription(e.getMessage()))); } catch (DateTimeParseException e) { log.warn("Invalid date format: {}", request.getBirthday()); responseObserver.onError(new StatusRuntimeException(Status.INVALID_ARGUMENT .withDescription("Invalid date format. Expected yyyy-MM-dd but got: " + request.getBirthday()))); } catch (Exception e) { log.error("Error updating user", e); responseObserver.onError(new StatusRuntimeException(Status.INTERNAL .withDescription("Internal error: " + e.getMessage()))); } } ``` -------------------------------- ### Delete User gRPC Service (Java) Source: https://github.com/zhihu/fust/blob/main/fust-docs/guide/grpc-service.md Implements the 'deleteUser' RPC method to remove a user. It verifies the user's existence before attempting deletion via UserService and returns a success status or an error. ```Java @Override public void deleteUser(DeleteUserRequest request, StreamObserver responseObserver) { try { long userId = request.getId(); log.info("Deleting user: {}", userId); // 确保用户存在 userService.getUserById(userId); boolean deleted = userService.deleteUser(userId); DeleteUserResponse response = DeleteUserResponse.newBuilder() .setSuccess(deleted) .build(); responseObserver.onNext(response); responseObserver.onCompleted(); } catch (UserNotFoundException e) { log.warn("User not found: {}", request.getId()); responseObserver.onError(new StatusRuntimeException(Status.NOT_FOUND .withDescription(e.getMessage()))); } catch (Exception e) { log.error("Error deleting user", e); responseObserver.onError(new StatusRuntimeException(Status.INTERNAL .withDescription("Internal error: " + e.getMessage()))); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.