作者热门文章
- 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
- 915. Partition Array into Disjoint Intervals 分割数组
- 932. Beautiful Array 漂亮数组
- 940. Distinct Subsequences II 不同的子序列 II
1、 broker-service->auth-service->postgresdb;
2、 zipkin监控:需代码入侵;
1、 通过context传递span;
main.go
package main
import (
"broker-service/auth-service"
"broker-service/svc1"
"context"
"fmt"
"log"
"net/http"
"time"
"github.com/opentracing/opentracing-go"
zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing"
"github.com/openzipkin/zipkin-go"
zipkinhttp "github.com/openzipkin/zipkin-go/reporter/http"
)
var webPort = "8080"
const (
// Our service name.
serviceName = "client"
// Host + port of our service.
hostPort = "0.0.0.0:0"
// Endpoint to send Zipkin spans to.
zipkinHTTPEndpoint = "http://localhost:9411/api/v2/spans"
// Base endpoint of our Auth service.
authEndpoint = "http://localhost:8090"
)
type Config struct {
Client Services
Ctx context.Context
}
type Services struct {
Auth auth.Service
}
func main() {
fmt.Println("Starting broker service: ", webPort)
// set up a span reporter
reporter := zipkinhttp.NewReporter(zipkinHTTPEndpoint)
defer reporter.Close()
// create our local service endpoint
endpoint, err := zipkin.NewEndpoint(serviceName, hostPort)
if err != nil {
log.Fatalf("unable to create local endpoint: %+v\n", err)
}
// initialize our tracer
nativeTracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint))
if err != nil {
log.Fatalf("unable to create tracer: %+v\n", err)
}
// use zipkin-go-opentracing to wrap our tracer
tracer := zipkinot.Wrap(nativeTracer)
// optionally set as Global OpenTracing tracer instance
opentracing.SetGlobalTracer(tracer)
// Create Client to auth Services
as := auth.NewHTTPClient(tracer, authEndpoint)
// Create Root Span for duration of the interaction with svc1
span := opentracing.StartSpan("Run")
// Put root span in context so it will be used in our calls to the client.
ctx := opentracing.ContextWithSpan(context.Background(), span)
//setup config
app := Config{
Client: Services{
Auth: as,
},
Ctx: ctx,
}
span.Finish()
srv := &http.Server{
Addr: ":8080",
Handler: app.routes(),
}
err = srv.ListenAndServe()
if err != nil {
log.Panic(err)
}
}
1、 路由到auth服务;
router.go
package main
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
)
func (app *Config) routes() http.Handler {
mux := chi.NewRouter()
mux.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{
"https://*", "http://*"},
AllowedMethods: []string{
"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{
"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
ExposedHeaders: []string{
"link"},
AllowCredentials: true,
MaxAge: 300,
}))
mux.Use(middleware.Heartbeat("/ping"))
mux.Post("/", app.Broker)
mux.Post("/authenticate", app.Authenticate)
mux.Post("/auth", app.Auth)
return mux
}
package main
import (
"broker-service/auth-service"
"broker-service/event"
"broker-service/logs"
"context"
"encoding/json"
"errors"
"log"
"net/http"
"net/rpc"
"strconv"
"time"
"github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type RequestPayload struct {
Action string json:"action"
Auth AuthPayload json:"auth,omitempty"
Log loggerPayload json:"log,omitempty"
}
type AuthPayload struct {
Email string json:"email"
Password string json:"password"
}
type loggerPayload struct {
Name string json:"name"
Data string json:"data"
}
// zipkin 未监控
func (app *Config) authenticate(w http.ResponseWriter, r *http.Request, a AuthPayload) {
var jsonFromService jsonResponse
// create json and send to the auth microservice
log.Println("auth: ", a)
jsonData, _ := json.Marshal(a)
//call the service
msg := &HttpMessage{
Url: "http://authentication-service",
Api: "authenticate",
Data: jsonData,
Method: "POST"}
jsonFromService, status := msg.HttpClient()
//make sure get back the correct status code
if status == http.StatusUnauthorized {
app.errorJSON(w, errors.New("invalid credentials"))
return
} else if status != http.StatusOK {
log.Println("Data: ", jsonFromService, " | status:", status)
app.errorJSON(w, errors.New("calling from service failed"))
return
}
var payload jsonResponse
payload.Error = false
payload.Message = "Authenticated!"
payload.Data = jsonFromService.Data
app.writeJSON(w, http.StatusAccepted, payload)
}
// zipkin 监控
func (app *Config) Auth(w http.ResponseWriter, r *http.Request) {
span, ctx := opentracing.StartSpanFromContext(app.Ctx, "Auth")
defer span.Finish()
var payload jsonResponse
payload.Error = true
payload.Message = "Authentication failed!"
var requestPayload AuthPayload
log.Println("request Auth")
err := app.readJSON(w, r, &requestPayload)
if err != nil {
app.errorJSON(w, err)
return
}
log.Println(requestPayload)
span.LogFields(otlog.String("event", "Call Auth"))
response, err := app.Client.Auth.Auth(
ctx, auth.AuthPayload(requestPayload))
//check
if err != nil {
app.errorJSON(w, errors.New("calling from svc1 failed"))
log.Printf("Response err: %s\n", err.Error())
return
}
log.Printf("Auth Rresponse: %v Err: %+v\n", response, err)
payload.Error = false
payload.Message = response.Message
payload.Data = response.Data
app.writeJSON(w, http.StatusAccepted, payload)
}
对我来说遇到了一个棘手的问题......基于主干的路由 Controller 和期望的效果没有实现。 我需要仅根据 URL 的第一部分获取 URL,并根据任何一种可能性定向到路线。 JS routes
假设链路上同时存在弹性流量和非弹性流量(如图所示)。那么它们中的哪一个将主导链路(弹性或非弹性)。根据我的说法,这应该取决于如果非弹性流量作为弹性流量可以调整流量,那么流量可以根据它进行调整。所以任何
我是一名优秀的程序员,十分优秀!