搞不了了
This commit is contained in:
parent
86eaafa004
commit
3663392192
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Launch Package",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "/home/suguo/project/go-micro-demo/webClient.go"
|
||||
}
|
||||
]
|
||||
}
|
||||
1
go.mod
1
go.mod
|
|
@ -4,6 +4,7 @@ go 1.21.4
|
|||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/micro/plugins/v5/client/http v1.0.2
|
||||
github.com/micro/plugins/v5/registry/consul v1.0.2
|
||||
go-micro.dev/v5 v5.3.0
|
||||
)
|
||||
|
|
|
|||
2
go.sum
2
go.sum
|
|
@ -127,6 +127,8 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
|
|||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
github.com/micro/plugins/v5/client/http v1.0.2 h1:4P5zP97eiK4fSGOzwtECgzCSpzp6UDGz21zn+QAX+EE=
|
||||
github.com/micro/plugins/v5/client/http v1.0.2/go.mod h1:2KPjs6e7jEHN8UUY6TfSRsXZ9BQqJC5ItnpvZZTcLww=
|
||||
github.com/micro/plugins/v5/registry/consul v1.0.2 h1:8XhH+R73RY3/FFjCtfkGPWdHTWvQB93e0rC/qJ97MAE=
|
||||
github.com/micro/plugins/v5/registry/consul v1.0.2/go.mod h1:j4/2mpBmIvMxBw34/RPDpgOYjtmw6iNluxUctN1JNVY=
|
||||
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package handler
|
|||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func HelloWorld(c *gin.Context) {
|
||||
c.Header("Content-Type", "application/json")
|
||||
c.JSON(200, gin.H{
|
||||
"data": "hello world",
|
||||
})
|
||||
|
|
|
|||
33
webClient.go
33
webClient.go
|
|
@ -1,32 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/micro/plugins/v5/client/http"
|
||||
"github.com/micro/plugins/v5/registry/consul"
|
||||
"go-micro.dev/v5/client"
|
||||
"go-micro.dev/v5/registry"
|
||||
"go-micro.dev/v5/selector"
|
||||
)
|
||||
|
||||
func callAPI(s selector.Selector) {
|
||||
my := http.NewClient(
|
||||
client.Selector(s),
|
||||
client.ContentType("application/json"),
|
||||
)
|
||||
|
||||
req := my.NewRequest("hello", `/v1/hello`, map[string]string{})
|
||||
var resp map[string]interface{}
|
||||
if err := my.Call(context.Background(), req, &resp); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(resp)
|
||||
}
|
||||
|
||||
func main() {
|
||||
consulReg := consul.NewRegistry(
|
||||
registry.Addrs("127.0.0.1:8500"),
|
||||
)
|
||||
|
||||
srv, err := consulReg.GetService("hello")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
mySelector := selector.NewSelector(
|
||||
selector.Registry(consulReg),
|
||||
selector.SetStrategy(selector.RoundRobin),
|
||||
)
|
||||
|
||||
for i := 0; i < 100000; i++ {
|
||||
srv, _ := consulReg.GetService("hello")
|
||||
next := selector.Random(srv)
|
||||
node, err := next()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
node, _ := next()
|
||||
fmt.Println(node.Id, node.Address)
|
||||
|
||||
callAPI(mySelector)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
BIN
webService
BIN
webService
Binary file not shown.
|
|
@ -23,7 +23,7 @@ func main() {
|
|||
service := web.NewService(
|
||||
web.Name("hello"),
|
||||
web.Handler(ginRouter),
|
||||
web.RegisterTTL(time.Second*5),
|
||||
// web.RegisterTTL(time.Second*5),
|
||||
web.RegisterInterval(time.Second),
|
||||
web.Address(":8080"),
|
||||
web.Registry(consulReg),
|
||||
|
|
|
|||
Loading…
Reference in New Issue