Client
Tổng quan
Fountain đi kèm các protocol client dưới clients/ phản ánh các cài đặt server trong libs/fnet. Chúng chia sẻ quy ước cấu hình và khả năng quan sát của framework.
Các client hiện có:
fhttp_client— HTTP client với retry, mã hóa và hỗ trợ tracing.fgrpc_client— gRPC client với metadata và phân giải service discovery.ftcp_client— TCP client.fudp_client— UDP client.fquic_client— QUIC client.
Ví dụ
Một HTTP client gọi đến endpoint được mã hóa:
go
/* !!
* File: main.go
* File Created: Friday, 28th October 2022 5:19:38 pm
* Author: Kim Ericko ([email protected])
* -----
* Last Modified: Wednesday, 8th March 2023 4:39:51 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 (
"context"
"log"
"gitlab.soludian.com/soludian/fountain"
"gitlab.soludian.com/soludian/fountain/clients/fhttp_client"
"gitlab.soludian.com/soludian/fountain/libs/base/fson"
"gitlab.soludian.com/soludian/fountain/libs/crypto"
"gitlab.soludian.com/soludian/fountain/libs/ftracer"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
)
func main() {
fountain.WithInvokers(invokerHTTP, callHTTP, callEncryption).Serving()
}
var fhttpClient *fhttp_client.Client
func invokerHTTP() error {
fhttpClient = fhttp_client.InstallFountainInstance()
crypto.InstallFountainInstances()
return nil
}
func callHTTP() error {
tracer := ftracer.NewTracer(trace.SpanKindClient)
req := fhttpClient.R()
ctx, span := tracer.Start(context.Background(), "callHTTP()", propagation.HeaderCarrier(req.Header))
defer span.End()
// Inject traceId Into Header
// c1 := ftracer.HeaderInjector(ctx, req.Header)
log.Println(span.SpanContext().TraceID())
info, err := req.SetContext(ctx).SetHeader("x-uid", "101").Get("/hello?app=client-1")
if err != nil {
return err
}
log.Println(info)
return nil
}
func callEncryption() error {
log.Println("------------------")
req := fhttpClient.R()
info, err := req.Get("/encrypt")
if err != nil {
return err
}
log.Println(info)
log.Printf("------------------CBC------------------")
res, err := req.Get("/encrypt-cbc")
if err != nil {
return err
}
log.Printf("Before decryption: %s", res.Body())
data, err1 := fhttp_client.DecryptionResponse[map[string]any](res)
if err1 != nil {
log.Printf("Error: %s", err1.Error())
}
log.Printf("Data map: %s", fson.JSONString(data))
log.Printf("------------------CTR------------------")
res, err2 := req.Get("/encrypt-ctr")
if err2 != nil {
return err2
}
log.Printf("Before decryption: %s", res.Body())
dat3, err3 := fhttp_client.DecryptionResponse[map[string]any](res)
if err3 != nil {
log.Printf("Error: %s", err3.Error())
}
log.Printf("After decryption: %s", fson.JSONString(dat3))
return nil
}Xem thêm
- Tham chiếu:
fhttp_client,fgrpc_client,ftcp_client,fudp_client,fquic_client - Server — phía server của các giao thức này
- Ví dụ