In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people are at a loss about how springcloud integrates grpc. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Introduction to GRPC
Is a high-performance, general-purpose RPC framework open source by Google. Like other RPC, client applications can call remote service methods directly, just as if they were local methods. It hides the underlying implementation details, including serialization (XML, JSON, binary), data transfer (TCP, HTTP, UDP), deserialization, etc., developers only need to focus on the business itself, without paying attention to the technical details of RPC.
Like other RPC frameworks, gRPC follows the idea of defining services (similar to the idea of defining interfaces). The gRPC client declares an interface method that can be called remotely by defining the method name, method parameters, and return type. The server implements the client-defined interface methods and runs a gRPC service to handle gPRC client calls. Note that the gRPC client and server share the same interface method.
Springcloud and grpc
Springcloud uses restful api for internal communication, using http1, while grpc uses http2 as the communication protocol
Not to mention the advantages of http2, for many e-commerce services, the internal call chain is very complex, the use of grpc can effectively shorten the communication time.
Springboot2 integrated net.devh.grpc
Here the serialization framework uses protobuf
Grpc-server
1. Increase dependence
Io.protostuff protostuff-core 1.6.0 io.protostuff protostuff-runtime 1.6.0 io.grpc grpc-all ${grpc.version} org.springframework.boot spring-boot-configuration-processor true org.projectlombok lombok true net.devh grpc-server-spring-boot-starter
2. Add protobuf configuration file
Add configuration file hello.proto under src/main/proto
Syntax = "proto3"; package com.demo;option java_package = "com.demo"; message HelloRequest {string name = 1;} message HelloResponse {string name = 1; string status = 1;} / / rpc service service HelloService {rpc hello (HelloRequest) returns (HelloResponse) {}}
3. Automatically generate java code and add grpcservice through proto
@ GrpcServicepublic class HelloGrpcService extends HelloServiceGrpc.HelloServiceImplBase {private static Logger logger = LoggerFactory.getLogger (UserProfileGrpcService.class); @ Override public void hello (Hello.HelloRequest request, StreamObserver responseObserver) {logger.info ("hello start"); final Hello.HelloResponse.Builder replyBuilder = Hello.HelloResponse.newBuilder () .setName (request.getName ()) .setStatus ("success"); responseObserver.onNext (replyBuilder.build ()); responseObserver.onCompleted ();}}
4. Add startup class
@ EnableDiscoveryClientpublic class GrpcServerApplication {public static void main (String [] args) {SpringApplication.run (GrpcServerApplication.class, args);}}
5. Add yml configuration
Server: port: 8011grpc: server: port: 6000spring: application: name: hello-grpc-servergrpc-client
1. Dependent package
Io.protostuff protostuff-core 1.6.0 io.protostuff protostuff-runtime 1.6.0 io.grpc grpc-all ${grpc.version} org.springframework.boot spring-boot-configuration-processor true org.springframework.boot spring-boot-starter-web org.projectlombok lombok true net.devh grpc-client-spring-boot-starter
2. Add calls to service
Servicepublic class GrpcClientService {@ GrpcClient ("hello-grpc-server") private Channel serverChannel; public String hello (String name) {HelloServiceGrpc.HelloServiceBlockingStub stub = HelloServiceGrpc.newBlockingStub (serverChannel); Hello.HelloRequest.Builder builder= Hello.HelloRequest.newBuilder (). SetName ("xiaoli"); Hello.HelloResponse response = stub.hello (builder.build ()); return "{'responseStatus':'" + response.getStatus () + "', 'result': []}";}}
Load balancer has been added here. The load balancer solution recommended by grpc official website is to manage http2 long connection pool at the application layer.
Add controller
@ RestController@RequestMapping ("/ test") public class HelloController {private static Logger logger = LoggerFactory.getLogger (HelloController.class); @ Autowired private GrpcClientService grpcClientService; @ RequestMapping (value = "/ hello", method = RequestMethod.GET) public String hello () {try {String result = grpcClientService.hello ("aa"); logger.debug ("respString: {}", result); return result } catch (Throwable e) {logger.error ("hello error", e);} return null;}
Add yml configuration
Info: version: 1.0 name: hello-grpc-clientserver: port: 8012 grpc: client: hello-grpc-server: enableKeepAlive: true keepAliveWithoutCalls: true negotiationType: plaintext
Start the service to access http://localhost:8012/test/hello? Conduct a test
This integration requires writing proto interface files and generating code automatically each time, and additional assembly parameters are required for both the client and the server.
However, the advantage is that there is a detailed interface specification (protobuf) and can support heterogeneous language calls.
I'll talk about the integration of only java language calls, but not having to write proto files every time.
After reading the above, have you mastered how springcloud integrates grpc? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.