当前位置:网站首页>Grpc: implement grpc proxy
Grpc: implement grpc proxy
2022-06-24 01:48:00 【Trespass 】
Introduce
How does this article describe rk-boot Quickly build gRPC agent .
What is? gRPC agent ?
gRPC Agent will accept gRPC request , And forward to others according to the user policy gRPC service . There are not many application scenarios , For example, according to the environmental parameters , Forward the request to a different gRPC service .
install
go get github.com/rookie-ninja/rk-boot go get github.com/rookie-ninja/rk-grpc
Quick start
Use rk-boot Starting up gRPC The agency has a limitation . Only requests sent in code form , To be represented .grpc-gateway perhaps grpcurl Form requests are temporarily not supported .
at present ,rk-boot Support 3 In the strategy .
- headerBased: adopt gRPC In request Metadata Value to determine the proxy destination .
- pathBased: Determine the proxy destination through the request path .
- ipBased: By remote IP Address to determine the proxy destination .
In the following example , We start three services .
- proxy(8080): Agency service , according to headerBased Strategy , forward rk.api.v1.RkCommonService.Healthy Ask to test In service .
- test(8081): Test domain gRPC service , Accept proxy Acting as a proxy for incoming requests .
- client: Go to proxy Service delivery rk.api.v1.RkCommonService.Healthy request .
1. establish proxy/boot.yaml & proxy/main.go
monitor 8080 port ,proxy The service does not implement any gRPC Method , If gRPC Requested Metadata Contained in the domain:test, Will forward .
The proxy defaults from proxy.rules.dest Select an address to forward .
- boot.yaml
---
grpc:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
proxy:
enabled: true # Optional, enable proxy server
rules:
- type: headerBased # Optional, options:[headerBased, pathBased, ipBased]
headerPairs: ["domain:test"] # Optional, header pairs separated by colon(:)
dest: ["localhost:8081"] # Optional, destinations
interceptors:
loggingZap:
enabled: true- main.go
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"github.com/rookie-ninja/rk-boot"
_ "github.com/rookie-ninja/rk-grpc/boot"
)
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}2. establish test/boot.yaml & test/main.go
monitor 8081 port .
start-up CommonService service , receive Proxy The agent came here rk.api.v1.RkCommonService.Healthy request .
- boot.yaml
---
grpc:
- name: greeter # Required
port: 8081 # Required
enabled: true # Required
commonService:
enabled: true # Optional, default: false
interceptors:
loggingZap:
enabled: true- main.go
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"github.com/rookie-ninja/rk-boot"
_ "github.com/rookie-ninja/rk-grpc/boot"
)
// Application entrance.
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}3.client/main.go
Sent request metadata in , add to domain:test, Give Way proxy Agent request .
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
api "github.com/rookie-ninja/rk-grpc/boot/api/third_party/gen/v1"
"github.com/rookie-ninja/rk-grpc/interceptor/context"
"github.com/rookie-ninja/rk-grpc/interceptor/log/zap"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"log"
)
// In this example, we will create a simple gRpc client and enable RK style logging interceptor.
func main() {
// ********************************************
// ********** Enable interceptors *************
// ********************************************
opts := []grpc.DialOption{
grpc.WithChainUnaryInterceptor(
rkgrpclog.UnaryClientInterceptor(),
),
grpc.WithInsecure(),
grpc.WithBlock(),
}
// 1: Create grpc client
conn, client := createCommonServiceClient(opts...)
defer conn.Close()
// 2: Wrap context, this is required in order to use bellow features easily.
ctx := rkgrpcctx.WrapContext(context.Background())
// Add header to make proxy request to test server
ctx = metadata.AppendToOutgoingContext(ctx, "domain", "test")
// 3: Call server
if resp, err := client.Healthy(ctx, &api.HealthyRequest{}); err != nil {
rkgrpcctx.GetLogger(ctx).Fatal("Failed to send request to server.", zap.Error(err))
} else {
rkgrpcctx.GetLogger(ctx).Info(fmt.Sprintf("[Message]: %s", resp.String()))
}
}
func createCommonServiceClient(opts ...grpc.DialOption) (*grpc.ClientConn, api.RkCommonServiceClient) {
// 1: Set up a connection to the server.
conn, err := grpc.DialContext(context.Background(), "localhost:8080", opts...)
if err != nil {
log.Fatalf("Failed to connect: %v", err)
}
// 2: Create grpc client
client := api.NewRkCommonServiceClient(conn)
return conn, client
}4. Folder structure
$ tree
.
├── client
│ ├── go.mod
│ └── main.go
├── proxy
│ ├── boot.yaml
│ ├── go.mod
│ ├── go.sum
│ └── main.go
└── test
├── boot.yaml
├── go.mod
└── main.go
3 directories, 9 files5. start-up proxy,test
$ go run proxy/main.go $ go run test/main.go
6. verification
start-up client/main.go
- client End log
2021-11-13T02:14:33.598+0800 INFO client/main.go:45 [Message]: fields:{key:"healthy" value:{bool_value:true}}
------------------------------------------------------------------------
endTime=2021-11-13T02:14:33.597875+08:00
startTime=2021-11-13T02:14:33.59247+08:00
elapsedNano=5405233
timezone=CST
ids={"eventId":"7ffe293d-24f4-42b3-9325-64bde414912c"}
app={"appName":"rk","appVersion":"","entryName":"grpc","entryType":"grpc"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"grpcMethod":"Healthy","grpcService":"rk.api.v1.RkCommonService","grpcType":"unaryClient","remoteIp":"localhost","remotePort":"8080"}
error={}
counters={}
pairs={}
timing={}
remoteAddr=localhost:8080
operation=/rk.api.v1.RkCommonService/Healthy
resCode=OK
eventStatus=Ended
EOE- proxy End log
proxy Terminal grpcType yes streamServer.
------------------------------------------------------------------------
endTime=2021-11-13T02:14:33.597337+08:00
startTime=2021-11-13T02:14:33.593252+08:00
elapsedNano=4085300
timezone=CST
ids={"eventId":"f32f7895-a4b2-4c8c-bedc-1d4733db78a7"}
app={"appName":"rk-demo","appVersion":"master-2c9c6fd","entryName":"greeter","entryType":"GrpcEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"grpcMethod":"Healthy","grpcService":"rk.api.v1.RkCommonService","grpcType":"streamServer","gwMethod":"","gwPath":"","gwScheme":"","gwUserAgent":""}
error={}
counters={}
pairs={}
timing={}
remoteAddr=127.0.0.1:58441
operation=/rk.api.v1.RkCommonService/Healthy
resCode=OK
eventStatus=Ended
EOE- test End log
test Terminal grpcType yes unaryServer.
------------------------------------------------------------------------
endTime=2021-11-13T02:14:33.596967+08:00
startTime=2021-11-13T02:14:33.59692+08:00
elapsedNano=47149
timezone=CST
ids={"eventId":"6eb9b10a-153a-4955-85b0-f227e3ec54b2"}
app={"appName":"rk-demo","appVersion":"master-2c9c6fd","entryName":"greeter","entryType":"GrpcEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"grpcMethod":"Healthy","grpcService":"rk.api.v1.RkCommonService","grpcType":"unaryServer","gwMethod":"","gwPath":"","gwScheme":"","gwUserAgent":""}
error={}
counters={}
pairs={"healthy":"true"}
timing={}
remoteAddr=10.8.0.2:58443
operation=/rk.api.v1.RkCommonService/Healthy
resCode=OK
eventStatus=Ended
EOEpathBased Strategy
It can be modified by boot.yaml File modification agent policy .
---
grpc:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
proxy:
enabled: true # Optional, enable proxy server
rules:
- type: pathBased # Optional, options:[headerBased, pathBased, ipBased]
paths: ["/rk.api.v1.RkCommonService/Healthy"] # Optional, gRPC method, support golang regex.
dest: ["localhost:8081"] # Optional, destinationsipBased Strategy
It can be modified by boot.yaml File modification agent policy .
proxy.rules.ips Support CIDR.
---
grpc:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
proxy:
enabled: true # Optional, enable proxy server
rules:
- type: ipBased # Optional, options:[headerBased, pathBased, ipBased]
ips: ["127.0.0.0/24"] # Optional, remote Ips, support CIDR.
dest: ["localhost:8081"] # Optional, destinations边栏推荐
- [technical grass planting] look what I did with my server!
- [combat power upgrade] Tencent cloud's first arm architecture instance was launched! Experience the new architecture computing power!
- Gin framework: implementing distributed log tracing
- Clubhouse online supports replay function; Webobs live streaming tools are going to be popular |w
- LeetCode 1289. Descent path min and II
- Glusterfs version 4.1 selection and deployment
- Typescript is a weak type
- Smart supply chain collaborative management platform for the home industry integrated upstream and downstream collaboration of the supply chain to improve management efficiency
- PHP implementation of interval sorting of classified data
- Spatial4j introduction practice
猜你喜欢
![[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)](/img/b5/a8c4bbaf868dd20b7dc9449d2a4378.jpg)
[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
![[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)](/img/c8/f6c2a62b8ab8fa88bd2b3d8f35f592.jpg)
[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)
随机推荐
Ppt layout design how to make pages not messy
Cloud computing "keeping the promise"
Software cost evaluation: basic knowledge interpretation of cosmoc method
Line/kotlin jdsl: kotlin DSL for JPA criteria API
Moment. JS how to get the zero hour time of the current time
[tcapulusdb knowledge base] how to clean up tables in tcapulusdb table management?
SMS marketing is the key to retain customers
Note sharing (5) -precautions for Oracle to MySQL
Can the server be restarted through the fortress machine? How are the fortress machines connected to the server
Introduction to trusted service manager
Echo framework: implementing service end flow limiting Middleware
Note 3 of disruptor: basic operation of ring queue (without disruptor class)
How do users of Fortress computers add servers? How much does it cost to add servers for fortress users?
MySQL architecture
NFS file systems - mount and optimize
How does Huawei weautomate RPA achieve the natural growth of government enterprise automation?
How does smart digital operation get through offline collection and online marketing?
Login server in VNC mode
2021-11-14:Fizz Buzz。 I'll give you an integer n and find the number from 1 to n
[tcapulusdb knowledge base] common problems of tcapulusdb local deployment