- 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/auth-service/data"
"database/sql"
"fmt"
"log"
"net/http"
"os"
"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"
_ "github.com/jackc/pgconn"
_ "github.com/jackc/pgx/v4"
_ "github.com/jackc/pgx/v4/stdlib"
)
const (
// Our service name.
serviceName = "auth"
// Host + port of our service.
hostPort = "localhost:8090"
// Endpoint to send Zipkin spans to.
zipkinHTTPEndpoint = "http://localhost:9411/api/v2/spans"
)
var counts int
//auth
func main() {
// 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)
//connect to DB
conn := connectToDB()
if conn == nil {
log.Panic("Can't connect to Postgres!")
}
// create the service implementation
service := auth.NewService(conn, data.New(conn))
// create the HTTP Server Handler for the service
handler := auth.NewHTTPHandler(tracer, service)
// start the service
fmt.Printf("Starting %s on %s\n", serviceName, hostPort)
http.ListenAndServe(hostPort, handler)
}
func openDB(dsn string) (*sql.DB, error) {
db, err := sql.Open("pgx", dsn)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
return db, nil
}
func connectToDB() *sql.DB {
dsn := "host=localhost port=5432 user=postgres password=password dbname=users sslmode=disable timezone=Asia/Shanghai connect_timeout=5"
}
for {
connection, err := openDB(dsn)
if err != nil {
log.Println(dsn)
log.Println("postgres is not ready...")
time.Sleep(2 * time.Second)
counts++
} else {
log.Println("connected to postgres")
return connection
}
if counts > 100 {
log.Panic(err)
}
}
}
1、 定义auth服务;
httpService.go
package auth
import (
"log"
"net/http"
opentracing "github.com/opentracing/opentracing-go"
"broker-service/middleware"
)
type httpService struct {
service Service
}
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"
}
// authHandler is our HTTP Handlerfunc for a Auth request.
func (s *httpService) authHandler(w http.ResponseWriter, req *http.Request) {
var requestPayload AuthPayload
err := s.readJSON(w, req, &requestPayload)
if err != nil {
s.errorJSON(w, err)
return
}
log.Println("requestPayload:", requestPayload)
// call our Auth binding
result, err := s.service.Auth(req.Context(), requestPayload)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// return the result
s.writeJSON(w, http.StatusAccepted, result)
}
// NewHTTPHandler returns a new HTTP handler our svc2.
func NewHTTPHandler(tracer opentracing.Tracer, service Service) http.Handler {
// Create our HTTP Service.
svc := &httpService{
service: service}
// Create the mux.
mux := http.NewServeMux()
// Create the Auth handler.
var authHandler http.Handler
authHandler = http.HandlerFunc(svc.authHandler)
// Wrap the Auth handler with our tracing middleware.
authHandler = middleware.FromHTTPRequest(tracer, "Auth")(authHandler)
// Wire up the mux.
mux.Handle("/auth/", authHandler)
// Return the mux.
return mux
}
service.go
package auth
import (
"context"
"errors"
)
// Service interface to our svc2 service.
type Service interface {
Auth(ctx context.Context, a AuthPayload) (jsonResponse, error)
}
自定义middleware.go,context 传递 Http 请求
// Package middleware provides some usable transport middleware to deal with
// propagating Zipkin traces across service boundaries.
package middleware
import (
"fmt"
"net"
"net/http"
"strconv"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)
// RequestFunc is a middleware function for outgoing HTTP requests.
type RequestFunc func(req *http.Request) *http.Request
// ToHTTPRequest returns a RequestFunc that injects an OpenTracing Span found in
// context into the HTTP Headers. If no such Span can be found, the RequestFunc
// is a noop.
func ToHTTPRequest(tracer opentracing.Tracer) RequestFunc {
return func(req *http.Request) *http.Request {
// Retrieve the Span from context.
if span := opentracing.SpanFromContext(req.Context()); span != nil {
// We are going to use this span in a client request, so mark as such.
ext.SpanKindRPCClient.Set(span)
// Add some standard OpenTracing tags, useful in an HTTP request.
ext.HTTPMethod.Set(span, req.Method)
span.SetTag("http.host", req.URL.Host)
span.SetTag("http.path", req.URL.Path)
ext.HTTPUrl.Set(
span,
fmt.Sprintf("%s://%s%s", req.URL.Scheme, req.URL.Host, req.URL.Path),
)
// Add information on the peer service we're about to contact.
if host, portString, err := net.SplitHostPort(req.URL.Host); err == nil {
ext.PeerHostname.Set(span, host)
if port, err := strconv.Atoi(portString); err != nil {
ext.PeerPort.Set(span, uint16(port))
}
} else {
ext.PeerHostname.Set(span, req.URL.Host)
}
// Inject the Span context into the outgoing HTTP Request.
if err := tracer.Inject(
span.Context(),
opentracing.TextMap,
opentracing.HTTPHeadersCarrier(req.Header),
); err != nil {
fmt.Printf("error encountered while trying to inject span: %+v\n", err)
}
}
return req
}
}
// HandlerFunc is a middleware function for incoming HTTP requests.
type HandlerFunc func(next http.Handler) http.Handler
// FromHTTPRequest returns a Middleware HandlerFunc that tries to join with an
// OpenTracing trace found in the HTTP request headers and starts a new Span
// called operationName. If no trace could be found in the HTTP request
// headers, the Span will be a trace root. The Span is incorporated in the
// HTTP Context object and can be retrieved with
// opentracing.SpanFromContext(ctx).
func FromHTTPRequest(tracer opentracing.Tracer, operationName string,
) HandlerFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// Try to join to a trace propagated in req.
wireContext, err := tracer.Extract(
opentracing.TextMap,
opentracing.HTTPHeadersCarrier(req.Header),
)
if err != nil {
fmt.Printf("error encountered while trying to extract span: %+v\n", err)
}
// create span
span := tracer.StartSpan(operationName, ext.RPCServerOption(wireContext))
defer span.Finish()
// store span in context
ctx := opentracing.ContextWithSpan(req.Context(), span)
// update request context to include our new span
req = req.WithContext(ctx)
// next middleware or actual request handler
next.ServeHTTP(w, req)
})
}
}
处理实现验证服务
package auth
import (
"broker-service/auth-service/data"
"context"
"database/sql"
"fmt"
"log"
"github.com/opentracing/opentracing-go"
)
// Auth is our actual service implementation.
type auth struct {
DB *sql.DB
Models data.Models
}
// NewService returns a new implementation of our Service.
func NewService(db *sql.DB, models data.Models) Service {
return &auth{
DB: db,
Models: models,
}
}
// Auth implements our Service interface.
func (auth *auth) Auth(ctx context.Context, a AuthPayload) (jsonResponse, error) {
var jsonResp jsonResponse
jsonResp.Error = true
jsonResp.Message = "Auth fialed"
// Pull span from context.
span := opentracing.SpanFromContext(ctx)
// Example binary annotations.
span.SetTag("service", "auth")
span.SetTag("AuthPayload", a)
user, err := auth.Models.User.GetByEmail(span, a.Email)
if err != nil {
log.Println("get user failed from db: ", err)
span.SetTag("error", err.Error())
return jsonResp, err
}
log.Println("user:", user)
valid, err := user.PasswordMatches(a.Password)
if err != nil || !valid {
log.Println("invalid user: ", err)
span.SetTag("error", err.Error())
return jsonResp, err
}
jsonResp = jsonResponse{
Error: false,
Message: fmt.Sprintf("Logged in user %s", user.Email),
Data: user,
}
log.Println("auth response: ", jsonResp)
return jsonResp, nil
}
通过client 向服务发送验证请求(由 broker-service 调用)
package auth
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"broker-service/middleware"
opentracing "github.com/opentracing/opentracing-go"
)
// client is our actual client implementation
type client struct {
baseURL string
httpClient *http.Client
tracer opentracing.Tracer
traceRequest middleware.RequestFunc
}
// Auth implements our Service interface.
func (c *client) Auth(ctx context.Context, a AuthPayload) (data jsonResponse, err error) {
// create new span using span found in context as parent (if none is found,
// our span becomes the trace root).
span, ctx := opentracing.StartSpanFromContext(ctx, "Auth")
defer span.Finish()
log.Println("auth: ", a)
jsonData, _ := json.Marshal(a)
url := fmt.Sprintf("%s/auth/", c.baseURL)
var payload jsonResponse
payload.Error = true
payload.Message = "Authenticatioin failed!"
// create the HTTP request
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
span.SetTag("error", err.Error())
return payload, err
}
// use our middleware to propagate our trace
req = c.traceRequest(req.WithContext(ctx))
// execute the HTTP request
resp, err := c.httpClient.Do(req)
if err != nil {
// annotate our span with the error condition
span.SetTag("error", err.Error())
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusAccepted {
err = json.NewDecoder(resp.Body).Decode(&data)
log.Println("result: ", data)
if err != nil {
span.SetTag("error", err.Error())
return
}
if data.Error {
span.SetTag("error", data.Error)
return
}
return data, nil
}
return
}
// NewHTTPClient returns a new client instance to our auth using the HTTP
// transport.
func NewHTTPClient(tracer opentracing.Tracer, baseURL string) Service {
return &client{
baseURL: baseURL,
httpClient: &http.Client{
},
tracer: tracer,
traceRequest: middleware.ToHTTPRequest(tracer),
}
}
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
使用微 Controller 时,通常您必须对寄存器进行写入和读取,为了使代码更具可读性,您需要定义寄存器地址及其位。这有点好,但是当您的寄存器名称彼此非常相似时,它很快就会变得困惑,例如此处所示 #
微 Controller 背景下的“原子操作”是什么? 我正在研究 TI F28027 MCU。 The data sheet says that its operations are atomic
我正在用 PIC 微 Controller 做一个项目。我有一个 ADC 采样并将数据保存到 RAM 存储器,一旦 RAM 被填满,我需要使用 PIC 微 Controller 通过蓝牙发送它。 我的
如何确定微 Controller 中特定程序所需的堆栈内存? 例如,假设我有一个内部可能有许多子例程或线程的程序。在我开始执行程序之前,我想修复这个程序的堆栈大小。我如何标记堆栈的终点。 最佳答案 我
我知道 printf 和 sprintf 之间的基本功能差异。但是,我想知道它们之间一些与时间/延迟相关的差异。显然,我想在我的一个自定义构建 RTOS 的任务中使用它。你怎么看 ?我想知道更多它会如
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 5 年前。
我有一个一般性的问题。我在微 Controller 上记录错误。但是微 Controller 的资源比 Windows 计算机更有限。在我的例子中,我将 64 个错误代码保存在一个队列中,由 Free
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 6 年前。 Improve th
假设我有一个时钟速度为 20 Mhz 的 8 位定时器。计时器在多少时间内可以计数多远而不溢出。或者1秒内溢出多少次?我知道它可以数到 255 并且会溢出 最佳答案 时间和频率之间的关系是t = 1/
我正在开展一个全面的长期 C 编程项目,该项目需要模块化编程方法。作为设计的一部分,将创建库,因此我想确认头文件组织的正确/错误解释: 问题 假设您正在创建一个库。经过深思熟虑,您决定您希望构想的最终
1. #define timers ((dual_timers *)0x03FF6000) 这是 ARM 微 Controller 中使用的内存映射定义 结构定义在哪里 2. struct dua
我购买了 LinkSprite JPEG 彩色相机和 LPC1768 mbed 微 Controller 。通过“LinkSprite”相机,我可以拍摄 jpeg 格式的图像,根据他们提供的教程,我可
我有很多不同的时间来跟踪我的设计,但没有什么是 super 关键的。 10 毫秒 +/- 几毫秒根本不是什么大问题。但是可能有 10 个不同的定时器同时在不同的周期进行计数,显然我没有足够的专用定时器
是否可以通过串行端口与 PIC 单片机通信 Android 应用程序?我可以使用哪些低成本手机?对不起,我是哥伦比亚人。 最佳答案 不确定 PIC,但是 Arduino可能是一个很好的引用点,并且有一
今天我一直在思考以下问题: 在一台普通的 pc 中,当你分配一些内存时,你向操作系统请求它,它会跟踪哪些内存段被占用,哪些内存段没有被占用,并且不要让你弄乱其他程序的内存等。但是微 Controlle
我已经为微 Controller 的键盘开发了一个 c 驱动程序。我想改变它,例如,当我按下 1 时,它会显示 1,直到我按下另一个数字。截至目前,数字只有在我按下数字时才会改变,这意味着一旦我松开键
我有一个在线程之间共享的 volatile unsigned char array LedState[5] 变量。数组中的每个索引表示一个状态。根据每个状态,LED 将以不同的顺序闪烁。一个线程设置数
我有一个项目要对微 Controller PIC18F 进行编程,我必须将一个开关电路连接到微 Controller 板上,这个开关电路有一个电锁和一个蜂鸣器要连接到它。 锁最初是通电的。假设当我发送
我是一名优秀的程序员,十分优秀!