# application.yml grpc: client: user-service: address: static://localhost:9090 negotiationType: plaintext enableKeepAlive: true keepAliveTime: 30s keepAliveTimeout: 10s server: port: 8081回到顶部八、REST网关集成RestController RequestMapping(/api/users) public class UserRestController { Autowired private UserGrpcClient grpcClient; GetMapping(/{id}) public MapString, Object getUser(PathVariable int id) { GetUserResponse resp grpcClient.getUser(id); return Map.of(id, resp.getId(), name, resp.getName(), email, resp.getEmail(), age, resp.getAge()); } GetMapping public ListMapString, Object listUsers( RequestParam(defaultValue 0) int page, RequestParam(defaultValue 10) int size) { return grpcClient.listUsers(page, size).stream() .map(r - Map.of(id, r.getId(), name, r.getName(), email, r.getEmail(), age, r.getAge())) .collect(Collectors.toList()); } }回到顶部九、性能对比指标 REST/JSON gRPC/Protobuf ----------------------------------------------- 序列化体积 ~2.1KB ~0.4KB 平均延迟 45ms 12ms QPS (单连接) ~3,200 ~9,500 CPU占用 高 低 代码生成 无 自动 流式支持 SSE(单向) 双向流