请求重试

请求重试

当一次服务调用失败时,我们可以让框架选择自动重试几次,这样能提高用户侧看到的请求成功率。在 failover cluster 模式下 dubbo-go 支持自动重试。

1.介绍

本示例演示如何在 client 端调用失败时配置重试功能,完整示例源码地址

2.如何使用重试功能

在使用 client.NewClient() 创建客户端时,可以使用 client.WithClientRetries() 方法设置重试次数。

  1. cli, err := client.NewClient(
  2. client.WithClientURL("tri://127.0.0.1:20000"),
  3. client.WithClientRetries(3),
  4. )

或者,可以使用 client.WithRequestTimeout() 设置服务粒度的超时时间(以下配置对服务 svc 起作用)。

  1. svc, err := greet.NewGreetService(cli, client.WithClientRetries(5))

也可以在调用发起时,使用 client.WithCallRetries() 指定重试次数

  1. resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"}, client.WithCallRetries(6))

从上往下,以上三种方式的优先级逐步提高,client.WithCallRetries() 指定的重试次数优先级最高。

3.示例解读

3.1服务端介绍

服务端proto文件

源文件路径:dubbo-go-sample/retry/proto/greet.proto

  1. syntax = "proto3";
  2. package greet;
  3. option go_package = "github.com/apache/dubbo-go-samples/retry/proto;greet";
  4. message GreetRequest {
  5. string name = 1;
  6. }
  7. message GreetResponse {
  8. string greeting = 1;
  9. }
  10. service GreetService {
  11. rpc Greet(GreetRequest) returns (GreetResponse) {}
  12. rpc GreetTimeout(GreetRequest) returns (GreetResponse) {}
  13. }

服务端handler文件

Greet方法直接响应,GreetRetry方法用于模拟重试。

  1. package main
  2. import (
  3. "context"
  4. "github.com/pkg/errors"
  5. _ "dubbo.apache.org/dubbo-go/v3/imports"
  6. "dubbo.apache.org/dubbo-go/v3/protocol"
  7. "dubbo.apache.org/dubbo-go/v3/server"
  8. greet "github.com/apache/dubbo-go-samples/retry/proto"
  9. "github.com/dubbogo/gost/log/logger"
  10. )
  11. type GreetTripleServer struct {
  12. requestTime int
  13. }
  14. func (srv *GreetTripleServer) Greet(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
  15. resp := &greet.GreetResponse{Greeting: req.Name}
  16. logger.Info("Not need retry, request success")
  17. return resp, nil
  18. }
  19. func (srv *GreetTripleServer) GreetRetry(ctx context.Context, req *greet.GreetRequest) (*greet.GreetResponse, error) {
  20. if srv.requestTime < 3 {
  21. srv.requestTime++
  22. logger.Infof("retry %d times", srv.requestTime)
  23. return nil, errors.New("retry")
  24. }
  25. resp := &greet.GreetResponse{Greeting: req.Name}
  26. logger.Infof("retry success, current request time is %d", srv.requestTime)
  27. srv.requestTime = 0
  28. return resp, nil
  29. }
  30. func main() {
  31. srv, err := server.NewServer(
  32. server.WithServerProtocol(
  33. protocol.WithPort(20000),
  34. ),
  35. )
  36. if err != nil {
  37. panic(err)
  38. }
  39. if err := greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{
  40. requestTime: 0,
  41. }); err != nil {
  42. panic(err)
  43. }
  44. if err := srv.Serve(); err != nil {
  45. logger.Error(err)
  46. }
  47. }

3.2客户端介绍

客户端client文件,创建客户端,设置重试次数为3,分别请求GreetGreetRetry,观察服务端日志输出。

  1. package main
  2. import (
  3. "context"
  4. "dubbo.apache.org/dubbo-go/v3/client"
  5. _ "dubbo.apache.org/dubbo-go/v3/imports"
  6. greet "github.com/apache/dubbo-go-samples/retry/proto"
  7. "github.com/dubbogo/gost/log/logger"
  8. )
  9. func main() {
  10. cli, err := client.NewClient(
  11. client.WithClientURL("tri://127.0.0.1:20000"),
  12. client.WithClientRetries(3),
  13. )
  14. if err != nil {
  15. panic(err)
  16. }
  17. svc, err := greet.NewGreetService(cli)
  18. if err != nil {
  19. panic(err)
  20. }
  21. // request normal
  22. resp, err := svc.Greet(context.Background(), &greet.GreetRequest{Name: "hello world"})
  23. if err != nil {
  24. logger.Error(err)
  25. }
  26. logger.Infof("Greet response: %s", resp.Greeting)
  27. // request need retry
  28. resp, err = svc.GreetRetry(context.Background(), &greet.GreetRequest{Name: "hello world"})
  29. if err != nil {
  30. logger.Error(err)
  31. }
  32. logger.Infof("Greet response: %s", resp.Greeting)
  33. }

3.3案例效果

先启动服务端,再启动客户端,访问GreetRetry时观察到服务端日志输出了重试次数。

  1. 2024-01-23 22:39:11 INFO logger/logging.go:22 [Not need retry, request success]
  2. 2024-01-23 22:39:11 INFO logger/logging.go:42 retry [1] times
  3. 2024-01-23 22:39:11 INFO logger/logging.go:42 retry [2] times
  4. 2024-01-23 22:39:11 INFO logger/logging.go:42 retry [3] times
  5. 2024-01-23 22:39:11 INFO logger/logging.go:42 retry success, current request time is [3]

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)