Skip to content

Commit

Permalink
搭建一个本地点击consul服务
Browse files Browse the repository at this point in the history
  • Loading branch information
=charlie committed Nov 15, 2021
1 parent 8d2d1d4 commit 2990300
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
consul 启动
服务编排的方式
sudo docker-compose -f docker/docker-compose.yml up

或者用最简单的容器,启动
docker run -d --name=cs -p 8500:8500 consul agent -server -bootsrap -ui -client 0.0.0.0
-ui 内置web 界面
-clent
-bootsrap 指定自己为leader

之间查看服务列表
http://192.168.1.124:8500/v1/agent/services

手动注册服务提交

curl \
--request PUT \
http://127.0.0.1:8500/v1/agent/service/deregister/redis1

curl \
--request PUT \
--data @p.json \
http://127.0.0.1:8500/v1/agent/service/register?replace-existing-checks=true
15 changes: 14 additions & 1 deletion Services/UserEndpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package Services

import (
"context"
"fmt"
"github.com/go-kit/kit/endpoint"
)

type UserRequest struct {
Uid int `json:"uid"`
Method string
}

type UserResponse struct {
Expand All @@ -16,7 +18,18 @@ type UserResponse struct {
func GenUserEndpoint(userService IUserService) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
r :=request.(UserRequest)
result := userService.GetName(r.Uid)
result :="nothing"
if r.Method == "GET" {
result =userService.GetName(r.Uid)
} else if r.Method =="DELETE" {
err := userService.DelUser(r.Uid)
if err != nil {
result = err.Error()
}
}else{
result = fmt.Sprintf("userId为%d的用户删除成功!",r.Uid)
}

return UserResponse{Result: result},nil
}
}
12 changes: 12 additions & 0 deletions Services/UserService.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package Services

import "errors"

type IUserService interface {
GetName(userid int) string
DelUser(userid int) error
}

type UserService struct {
Expand All @@ -13,3 +16,12 @@ func (this UserService) GetName(userid int) string {
}
return "guest"
}

func (this UserService) DelUser(userid int) error {
if userid == 101 {
return errors.New("无权限")
}
return nil
}


1 change: 1 addition & 0 deletions Services/UserTransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func DecodeUserRequest(c context.Context, r *http.Request) (interface{},error) {
uid,_ := strconv.Atoi(r.URL.Query().Get("uid"))
return UserRequest{
Uid:uid,
Method: r.Method,
},nil
}

Expand Down
11 changes: 11 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '2'

services:
consul:
image: progrium/consul:latest
ports:
- 8400:8400
- 8500:8500
- 8600:53/udp
hostname: consulserver
command: -server -bootstrap -ui-dir /ui
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module service.gomicro.test

go 1.16

require github.com/go-kit/kit v0.12.0
require (
github.com/go-kit/kit v0.12.0
github.com/gorilla/mux v1.8.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/consul/api v1.10.1/go.mod h1:XjsvQN+RJGWI2TWy1/kqaE16HrR2J/FWgkYjdZQsX9M=
Expand Down
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
httptransport "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
"net/http"
. "service.gomicro.test/Services"
)
Expand All @@ -10,5 +11,13 @@ func main() {
user := UserService{}
endp:=GenUserEndpoint(user)
serverHandler := httptransport.NewServer(endp,DecodeUserRequest,EncodeUserResponse)
http.ListenAndServe(":8080",serverHandler)
router :=mux.NewRouter()
{
router.Methods("GET","DELETE").Path(`'/user/{uid:\d+}'`).Handler(serverHandler)
router.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",router)
}
19 changes: 19 additions & 0 deletions p.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"ID": "userservice1",
"Name": "userservice",
"Tags": ["primary", "v1"],
"Address": "192.168.1.124",
"Port": 8080,
"Meta": {
"userservice_version": "4.0"
},
"EnableTagOverride": false,
"Check": {
"HTTP": "http://192.168.1.124:8080/health",
"Interval": "10s"
},
"Weights": {
"Passing": 10,
"Warning": 1
}
}

0 comments on commit 2990300

Please sign in to comment.