Skip to content

Commit

Permalink
命令行终端
Browse files Browse the repository at this point in the history
  • Loading branch information
=charlie committed Nov 15, 2021
1 parent 8f28866 commit 15dcc2b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ curl
--data @p.json
http://127.0.0.1:8500/v1/agent/service/register?replace-existing-checks=true **// 注销服务** curl
--request PUT
http://127.0.0.1:8500/v1/agent/service/deregister/userservice1
http://127.0.0.1:8500/v1/agent/service/deregister/userservice1

**启动服务**

go run main.go --name userservice -p 8080
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/go-kit/kit v0.12.0
github.com/google/uuid v1.1.2
github.com/gorilla/mux v1.8.0
github.com/hashicorp/consul/api v1.10.1
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
Expand Down
18 changes: 17 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
Expand All @@ -10,10 +11,25 @@ import (
"os/signal"
. "service.gomicro.test/Services"
"service.gomicro.test/util"
"strconv"
"syscall"
)

func main() {

name := flag.String("name","","服务名称")
port := flag.Int("p",0,"服务端口")
flag.Parse()

if *name == "" {
log.Fatal("请指定服务名")
}

if *port == 0 {
log.Fatal("请指定端口")
}
util.SetServiceNameAndPort(*name,*port)

user := UserService{}
endp := GenUserEndpoint(user)
serverHandler := httptransport.NewServer(endp, DecodeUserRequest, EncodeUserResponse)
Expand All @@ -29,7 +45,7 @@ func main() {
errChan := make(chan error)
go func() {
util.RegService() // 注册consul服务
err := http.ListenAndServe(":8080", router)
err := http.ListenAndServe(":"+ strconv.Itoa(*port), router)
if err != nil {
log.Println(err)
}
Expand Down
20 changes: 16 additions & 4 deletions util/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package util

import (
"fmt"
"github.com/google/uuid"
consulapi "github.com/hashicorp/consul/api"
"log"
"strconv"
)
var ConsulClient *consulapi.Client
var ServiceId string
var ServiceName string
var ServicePort int

func init () {
config := consulapi.DefaultConfig()
config.Address="192.168.1.124:8500"
Expand All @@ -14,20 +20,26 @@ func init () {
log.Fatal(err)
}
ConsulClient = client
ServiceId = "userservice" + uuid.New().String()
}

func SetServiceNameAndPort(name string,port int) {
ServiceName = name
ServicePort = port
}
func RegService() {
config := consulapi.DefaultConfig()
config.Address="192.168.1.124:8500"

reg := consulapi.AgentServiceRegistration{}
reg.ID ="userservice1"
reg.Name="userservice"
reg.ID =ServiceId
reg.Name=ServiceName
reg.Address="192.168.1.124"
reg.Port=8080
reg.Port=ServicePort
reg.Tags=[]string{"primary"}
check := consulapi.AgentServiceCheck{}
check.Interval="5s"
check.HTTP="http://192.168.1.124:8080/health"
check.HTTP="http://192.168.1.124:"+strconv.Itoa(ServicePort)+"/health"

reg.Check = &check

Expand Down

0 comments on commit 15dcc2b

Please sign in to comment.