# gateway **Repository Path**: youkelike/gateway ## Basic Information - **Project Name**: gateway - **Description**: go微服务网关 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-06-23 - **Last Updated**: 2025-06-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 功能 #### 支持的代理协议 - http/https - tcp - grpc - websocket #### 支持的功能 - 流量统计 - 限流、熔断、降级 - IP 黑白名单 - etcd 服务注册和发现 - 负载均衡 - mysql 读写分离 - 后端控制台 #### 部署方式 - docker-compose - k8s ## 技术栈 - Golang、Gin、Gorm、Redis、Mysql、Etcd、Hystrix、Grpc、Websocket、Jwt、Swaggo、Docker、K8s ## docker compose 部署 ### 交叉编译环境变量设置 ```sh go env -w GOOS=linux go env -w CGO_ENABLED=0 ``` ### 编译、制作镜像 ```sh 编译主程序 go build -o ./bin/gateway 构建 dashboard 镜像 docker build -f dockerfile-dashboard -t gateway-dashboard . 构建 server 镜像 docker build -f dockerfile-server -t gateway-server . 编译 http 服务端 go build -o ./bin/httpbackend1 ./k8s/example/httpbackend1/main.go go build -o ./bin/httpbackend2 ./k8s/example/httpbackend2/main.go 编译通过 gin 启动的 http 服务端 go build -o ./bin/httpbackend3 ./example/ginserver/main.go docker build -f dockerfile-httpbackend3 -t gateway-httpbackend3 . 构建 httpbackend 镜像 docker build -f dockerfile-httpbackend1 -t gateway-httpbackend1 . docker build -f dockerfile-httpbackend2 -t gateway-httpbackend2 . 编译 https 服务端 go build -o ./bin/httpsbackend ./k8s/example/httpsbackend/main.go 构建 httpsbackend 镜像 docker build -f dockerfile-httpsbackend -t gateway-httpsbackend . 编译 grpc 服务端 go build -o ./bin/grpcbackend ./example/grpcserver/main.go 构建 grpcbackend 镜像 docker build -f dockerfile-grpcbackend -t gateway-grpcbackend . 编译 tcp 服务端(如果在 tcp 中使用了熔断,要把 hystrix 的超时时间加到足够长) go build -o ./bin/tcpbackend ./example/tcpserver/main.go 构建 tcpbackend 镜像 docker build -f dockerfile-tcpbackend -t gateway-tcpbackend . 编译 websocket 服务端(如果在 websocket 中使用了熔断,要把 hystrix 的超时时间加到足够长) go build -o ./bin/websocketbackend ./example/websocket/main.go 构建 websocketbackend 镜像 docker build -f dockerfile-websocketbackend -t gateway-websocketbackend . ``` ### 启动 ```sh docker compose up ``` ## 测试 ```sh # 访问后台 * http://192.168.34.4:8880/dist/#/dashboard # 访问 http 服务 * http://192.168.34.4:30080/test_strip_uri 路径接入 * http://test.com:30080/ 域名接入(配置 host 把 test.com 指向 192.168.34.4) # 访问 https 服务(配置 host 把 server.https.gateway.local 指向 192.168.34.4) * https://server.https.gateway.local:30443/test_https_server # websocket 客户端访问 * http://server.gateway.local:30080/websocket #获取 grpc 访问的 token * curl --location --request POST 'http://192.168.34.4:30080/oauth/tokens' --header 'Authorization: Basic YXBwX2lkX2I6OGQ3YjExZWM5YmUwZTU5YTM2YjUyZjMyMzY2YzA5Y2I=' --form 'grant_type="client_credentials"' --form 'scope="read_write"' # grpc 客户端访问 * 先修改代码中的服务端地址 * go run example/grpcclient/main.go # tcp 客户端访问 * 修改代码中的服务端地址 * go run example/grpcclient/main.go ``` ### http 压测 ```sh docker pull williamyeh/wrk curl http://192.168.34.4:30080/test_strip_uri docker run --rm williamyeh/wrk -t24 -c1000 -d30s --latency "http://192.168.34.4:30080/test_strip_uri" ``` #### tcp 代理 压测代码 PS gateway\test\thrift_server_client\concurrency_bench> go run main.go ```sh result qps:82950 succ:1659009 fail:0 ``` #### grpc 代理 压测代码 PS gateway\test\grpc_bench> go run main.go ```sh result qps:2826 succ:56535 fail:0 ``` ## k8s 部署 [k8s 部署](k8s/readme.md)