Skip to content

Commit

Permalink
fix:修复就近路由demo配置文件不对问题
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Sep 6, 2023
1 parent 5b34574 commit 50f9514
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/route/nearby/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ package main

import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -90,6 +92,7 @@ func (svr *PolarisProvider) Run() {
host = tmpHost
svr.host = tmpHost
}
svr.runWebServer()
svr.registerService()
svr.runMainLoop()
}
Expand Down Expand Up @@ -125,6 +128,31 @@ func (svr *PolarisProvider) deregisterService() {
log.Printf("deregister successfully.")
}

func (svr *PolarisProvider) runWebServer() {
http.HandleFunc("/echo", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
loc := svr.provider.SDKContext().GetValueContext().GetCurrentLocation().GetLocation()
locStr, _ := json.Marshal(loc)
msg := fmt.Sprintf("Hello, I'm RouteNearbyEchoServer Provider, MyLocInfo's : %s, host : %s:%d", string(locStr), svr.host, svr.port)
_, _ = rw.Write([]byte(msg))
})

ln, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", svr.port))
if err != nil {
log.Fatalf("[ERROR]fail to listen tcp, err is %v", err)
}

svr.port = ln.Addr().(*net.TCPAddr).Port

go func() {
log.Printf("[INFO] start http server, listen port is %v", svr.port)
if err := http.Serve(ln, nil); err != nil {
log.Fatalf("[ERROR]fail to run webServer, err is %v", err)
}
}()

}

func (svr *PolarisProvider) runMainLoop() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, []os.Signal{
Expand Down

0 comments on commit 50f9514

Please sign in to comment.