Go Chassis教程二–安装及编写服务端
1.Install go
2.Generate go mod
go mod init
3.Add go chassis
go get github.com/go-chassis/go-chassis/v2@v2.0.2
4.Use go mod
GO111MODULE=on go mod download #optional GO111MODULE=on go mod vendor
if you are facing network issue
export GOPROXY=https://goproxy.io
5.Install service-center
案例代码
type RestFulHello struct {}
func (r *RestFulHello) SayHello(b *restful.Context) {
b.Write([]byte("get user id: " + b.ReadPathParameter("userid")))
}
func (r *RestFulHello) SayHello1(b *restful.Context) {
b.Write([]byte("get user id1: " + b.ReadPathParameter("userid")))
}
func (r *RestFulHello) URLPatterns() []restful.Route {
return []restful.Route{
{Method: http.MethodGet, Path: "/sayhello/{userid}", ResourceFunc: r.SayHello},
{Method: http.MethodGet, Path: "/sayhello1/{userid}", ResourceFunc: r.SayHello1},
}
}
func main() {
// register struct
chassis.RegisterSchema("rest", &RestFulHello{})
//start all server you register in server/schemas.
if err := chassis.Init(); err != nil {
openlog.Error("Init failed. "+err.Error())
return
}
chassis.Run()
}
chassis.yaml
servicecomb: registry: address: http://127.0.0.1:30100 protocols: # what kind of server you want to launch rest: #launch a http server listenAddress: 127.0.0.1:5001
microservice.yaml
servicecomb: service: name: RESTServer # name your provider

