Khả năng phục hồi
Tổng quan
Fountain tích hợp sẵn các mẫu hình phục hồi — flow control, circuit breaking và rate limiting — giúp service ổn định dưới tải cao.
Các package hiện có:
fsentinel— flow control, circuit breaking và bảo vệ hệ thống thích ứng dựa trên Sentinel.resilient/backoff— retry với backoff (exponential, fixed, jitter…).resilient/circuit_breaker— circuit breaker.resilient/locker— distributed lock.resilient/ratelimit— rate limiting.resilient/recaptcha— xác minh reCAPTCHA.
Sentinel
fsentinel cung cấp tích hợp với Alibaba Sentinel — bao gồm giới hạn lưu lượng (flow control), circuit breaking và bảo vệ hệ thống:
go
import "gitlab.soludian.com/soludian/fountain/libs/fsentinel"
// Cài đặt Sentinel từ config key "sentinel"
sentinel := fsentinel.WithConfigKey("sentinel").InstallFountainInstance()Retry với Backoff
go
import "gitlab.soludian.com/soludian/fountain/libs/resilient/backoff"
err := backoff.Retry(ctx, func() error {
return callExternalService()
}, backoff.WithMaxRetries(3), backoff.WithExponential(100*time.Millisecond))Ví dụ
Cài đặt Sentinel flow-control:
go
/* !!
* File: main.go
* File Created: Tuesday, 28th June 2022 4:24:43 pm
* Author: Kim Ericko ([email protected])
* -----
* Last Modified: Thursday, 30th June 2022 4:25:20 pm
* Modified By: Kim Ericko ([email protected])
* -----
* Copyright 2022 Soludian, soludian.com
* All rights reserved.
*
* Licensed under the SOLUDIAN TECHNOLOGY SOLUTION CO., LTD Software License Agreement.
* Unauthorized use, reproduction, or distribution is prohibited (the "License");
*
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* [email protected] / https://www.soludian.com/license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -----
*
* HISTORY:
*
* Date By Comments
* ---------- --- ---------------------------------------------------------
*/
package main
import (
"fmt"
"github.com/gofiber/fiber/v3"
"gitlab.soludian.com/soludian/fountain"
"gitlab.soludian.com/soludian/fountain/libs/flog"
"gitlab.soludian.com/soludian/fountain/libs/fnet/fhttp"
)
var logger = flog.NewFountainLoggerOnce()
type APIServer struct {
*fhttp.Server
}
func (s *APIServer) Initialize() error {
s.Server = fhttp.InstallFountainInstance()
s.Server.Get("/panic", func(ctx fiber.Ctx) error {
err := fmt.Errorf("Route panic")
logger.WErr(err).Panicf(err.Error())
return err
})
s.Server.Get("/200", func(ctx fiber.Ctx) error {
return ctx.SendString("hello")
})
s.Server.Get("/hello", func(ctx fiber.Ctx) error {
return ctx.JSON("Hello client: " + ctx.Get("app"))
})
s.Server.Get("/500", func(ctx fiber.Ctx) error {
return ctx.Status(500).JSON("Hello client: " + ctx.Get("app"))
})
return nil
}
// export FOUNTAIN_DEBUG_MODE=true && go run main.go
// ab -n 10 -c 10 http://127.0.0.1:9007/hello, you can see 429, indicating current limit
func main() {
server := &APIServer{}
fountain.WithAppInstances(server).Serving()
}Xem thêm
- Tham chiếu:
fsentinel,resilient/backoff,resilient/circuit_breaker,resilient/ratelimit - Khả năng quan sát — giám sát hành vi mà các rule này kiểm soát
- Ví dụ