目录
1、代码实现样例:
gateway转发的url根据实际去调整,转发访问的url是否存在token,也根据实际情况去调整:
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/afex/hystrix-go/hystrix"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
// 设置路由
r.GET("/service/:service", handleRequest)
// 启动服务
if err := r.Run(":8081"); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
func handleRequest(c *gin.Context) {
service := c.Param("service")
commandName := "service_" + service
// 使用 Hystrix 实现服务熔断
hystrix.ConfigureCommand(commandName, hystrix.CommandConfig{
Timeout: 1000, // 超时时间(毫秒)
MaxConcurrentRequests: 100, // 最大并发请求数
ErrorPercentThreshold: 25,