惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AI
AI
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
Latest news
Latest news
Microsoft Azure Blog
Microsoft Azure Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Simon Willison's Weblog
Simon Willison's Weblog
M
MIT News - Artificial intelligence
V
Visual Studio Blog
N
Netflix TechBlog - Medium
P
Palo Alto Networks Blog
C
Cybersecurity and Infrastructure Security Agency CISA
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog
V
Vulnerabilities – Threatpost
AWS News Blog
AWS News Blog
美团技术团队
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
Stack Overflow Blog
Stack Overflow Blog
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
Lohrmann on Cybersecurity
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
Hacker News - Newest:
Hacker News - Newest: "LLM"
Hugging Face - Blog
Hugging Face - Blog
O
OpenAI News
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
Google Online Security Blog
Google Online Security Blog
T
Tenable Blog
Attack and Defense Labs
Attack and Defense Labs
C
Cisco Blogs
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
Help Net Security
Help Net Security
The Last Watchdog
The Last Watchdog
S
Security @ Cisco Blogs
C
CERT Recently Published Vulnerability Notes
博客园 - 【当耐特】
T
Troy Hunt's Blog
Cloudbric
Cloudbric
IT之家
IT之家

博客园 - Bo Schwarzstein

发布关于PostGIS对于USD格式的拓展 紫微斗数之自化禄真的是损耗嘛? 紫微斗数个人经验之三合或者四化看哪个 A Practical Methodology, HSM, Handler,Service,Model, for Golang Backend Development 问ChatGPT玄学问题,看来命理师还是不会被取代的 Compile Sqlite3 Executable, Static Library, and Shared Library on Linux Benchmark JuiceFS at AWS 2 Benchmark JuiceFS on AWS 1 Work with AWS VPC, Lambda and Internet Far & Unifield Field Augmented Reality 紫微斗数是否对外国人有用 Create CloudFront Signed URL in 1 Minute 2022壬寅年天干四化 《中有成就秘笈》之中央密严刹土 IPFS与般若文海 Moira果老星宗七政四余排盘软件下载 Play Old Diablo 2 on macOS Catalina 视频平台设计思路大灌顶 Unity Input System教程
Use Go Micro Web with HTTP Handler
Bo Schwarzstein · 2020-09-25 · via 博客园 - Bo Schwarzstein

The go-micro is a very powerful framework to establish a complete microservice backend network.

go-micro has several types of services, one service called go.micro.web is just a wrapper for standard HTTP server from Golang. Another advantage to use go.micro.web rather than go.micro.api is that, user could directly use any 3rd party HTTP framework in the microservice, and do not have to use built-in api.Request/api.Response structures to process the HTTP request.

During our benchmark, the serialization operations for api.Request/api.Response is quite slow, and not convenient enought to retrieve all headers of HTTP request.

Here is an example about how to use gorilla/mux to work with go.micro.web to serve a REST+WebSocket server.

package main

import (
    "net/http"
    "time"

    "github.com/gorilla/mux"
    "github.com/gorilla/websocket"
    log "github.com/micro/go-micro/v2/logger"
    "github.com/micro/go-micro/v2/web"
)

func EventHandler(w http.ResponseWriter, r *http.Request) {
    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Errorf("failed to upgrade request [%v] to websocket", r)
    }
    defer conn.Close()

    for {
        t, msg, err := conn.ReadMessage()
        if err != nil {
            log.Error(err)
            break
        }

        log.Info(t, msg)

        e := Event{StatusCode: time.Now().Unix()}
        err = conn.WriteJSON(e)
        if err != nil {
            log.Error(err)
            break
        }
    }
}

func main() {
    svc := web.NewService(web.Name("go.micro.web.helloworld"))

    if err := svc.Init(); err != nil {
        log.Fatal(err)
    }

    router := mux.NewRouter()
    router.HandleFunc("/event", EventHandler)
    svc.Handle("/", router)

    if err := svc.Run(); err != nil {
        log.Fatal(err)
    }
}

That's it, so easy right ? You do not have to use anything related to api.Request/api.Response, but everything is the same as standard usage.

How to call this API ? By default, it should be accessible from http://localhost:8083/helloworld/event, it depends on at which port your go.micro.web is running.

This way of working has a lot of advantage, it allows to embed all REST handlers into go-micro eco-system easy, much better than api.Request/api.Response, we heavily use this way in our web applications.

posted on 2020-09-25 14:11  Bo Schwarzstein  阅读(203)  评论()    收藏  举报