5.服務注冊與發現Consul,簡學API,手動注冊和刪除服務


package main

import (
httptransport "github.com/go-kit/kit/transport/http"
mymux "github.com/gorilla/mux"
"gomicro/Services"
"net/http"
)

func main() {
    user := Services.UserService{}
    endp := Services.GenUserEnPoint(user)

    serverHandler := httptransport.NewServer(endp, Services.DecodeUserRequest, Services.EncodeUserResponse) //使用go kit創建server傳入我們之前定義的兩個解析函數

    r := mymux.NewRouter()
    //r.Handle(`/user/{uid:\d+}`, serverHandler) //這種寫法支持多種請求方式
    r.Methods("GET", "DELETE").Path(`/user/{uid:\d+}`).Handler(serverHandler) //這種寫法僅支持Get,限定只能Get請求
    r.Methods("GET").Path("/health").HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
        writer.Header().Set("Content-type", "application/json")
        writer.Write([]byte(`{"status":"ok"}`))
    })
    http.ListenAndServe(":8080", r)

}






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM