1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-03-17 20:28:06 +02:00

add http demo; http client can call http server; http client can call rpc server (#2149)

This commit is contained in:
orange-jacky 2021-04-09 01:31:15 +08:00 committed by GitHub
parent e7a7e3a050
commit 6ae252b892
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 520 additions and 161 deletions

View File

@ -15,31 +15,20 @@ replace k8s.io/component-base => k8s.io/component-base v0.0.0-20190708175518-244
replace google.golang.org/grpc => google.golang.org/grpc v1.24.0
require (
github.com/99designs/gqlgen v0.10.1
github.com/asim/go-micro/plugins/config/encoder/toml/v3 v3.0.0-20210120210110-dc8236ec05ed
github.com/asim/go-micro/plugins/config/source/configmap/v3 v3.0.0-20210120210110-dc8236ec05ed
github.com/asim/go-micro/plugins/config/source/grpc/v3 v3.0.0-20210120210110-dc8236ec05ed
github.com/asim/go-micro/plugins/registry/etcd/v3 v3.0.0-20210120210110-dc8236ec05ed
github.com/asim/go-micro/plugins/registry/kubernetes/v3 v3.0.0-20210120210110-dc8236ec05ed
github.com/asim/go-micro/plugins/selector/static/v3 v3.0.0-20210120210110-dc8236ec05ed
github.com/asim/go-micro/plugins/wrapper/select/roundrobin/v3 v3.0.0-20210120210110-dc8236ec05ed
github.com/asim/go-micro/plugins/wrapper/select/shard/v3 v3.0.0-20210120210110-dc8236ec05ed
github.com/asim/go-micro/v3 v3.0.0-20210120135431-d94936f6c97c
github.com/astaxie/beego v1.12.0
github.com/emicklei/go-restful v2.11.1+incompatible
github.com/gin-gonic/gin v1.4.0
github.com/asim/go-micro/plugins/client/http/v3 v3.0.0-20210403073940-e7a7e3a05092
github.com/asim/go-micro/plugins/config/encoder/toml/v3 v3.0.0-20210403073940-e7a7e3a05092
github.com/asim/go-micro/plugins/config/source/grpc/v3 v3.0.0-20210403073940-e7a7e3a05092
github.com/asim/go-micro/plugins/server/http/v3 v3.0.0-20210403073940-e7a7e3a05092
github.com/asim/go-micro/plugins/wrapper/select/roundrobin/v3 v3.0.0-20210403073940-e7a7e3a05092
github.com/asim/go-micro/plugins/wrapper/select/shard/v3 v3.0.0-20210403073940-e7a7e3a05092
github.com/asim/go-micro/v3 v3.5.0
github.com/gin-gonic/gin v1.6.3
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/protobuf v1.4.2
github.com/google/uuid v1.1.1
github.com/gorilla/rpc v1.2.0
github.com/gorilla/websocket v1.4.1
github.com/grpc-ecosystem/grpc-gateway v1.12.1
github.com/hailocab/go-geoindex v0.0.0-20160127134810-64631bfe9711
github.com/golang/protobuf v1.5.2
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/micro/cli/v2 v2.1.2
github.com/pborman/uuid v1.2.0
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
github.com/vektah/gqlparser v1.2.0
golang.org/x/net v0.0.0-20200707034311-ab3426394381
google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1
google.golang.org/grpc v1.26.0
github.com/pborman/uuid v1.2.1
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4
google.golang.org/genproto v0.0.0-20210406143921-e86de6bf7a46
google.golang.org/grpc v1.36.1
)

File diff suppressed because it is too large Load Diff

37
examples/http/README.md Normal file
View File

@ -0,0 +1,37 @@
# http server and http client demo
An example http application
## Contents
- **srv** - a http server as server of go-mirco service
- **cli** - a http client that call http server
- **rpcli** - a http client that call rpc server
## Run Service
Start http server
```shell
go run srv/main.go
```
## Client
Call http client
```shell
go run cli/main.go
```
## Run rpc Service
Start greeter service
```shell
go run ../greeter/srv/main.go
```
## Client
http client call rpc service
```shell
go run rpccli/main.go
```

30
examples/http/cli/main.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"context"
"log"
httpClient "github.com/asim/go-micro/plugins/client/http/v3"
"github.com/asim/go-micro/v3/client"
"github.com/asim/go-micro/v3/registry"
"github.com/asim/go-micro/v3/selector"
)
func main() {
CallHttpServer()
}
func CallHttpServer() {
r := registry.NewRegistry()
s := selector.NewSelector(selector.Registry(r))
// new client
c := httpClient.NewClient(client.Selector(s))
// create request/response
request := c.NewRequest("demo-http", "/demo", "", client.WithContentType("application/json"))
response := new(map[string]interface{})
// call service
err := c.Call(context.Background(), request, response)
log.Printf("err:%v response:%#v\n", err, response)
}

View File

@ -0,0 +1,34 @@
package main
import (
"context"
"log"
hello "github.com/asim/go-micro/examples/v3/greeter/srv/proto/hello"
"github.com/asim/go-micro/v3"
"github.com/asim/go-micro/v3/client"
"github.com/asim/go-micro/v3/registry"
)
func main() {
CallGrpcByHttp()
}
func CallGrpcByHttp() {
// create a new service
service := micro.NewService(
micro.Registry(registry.NewRegistry()),
)
// parse command line flags
service.Init()
c := service.Client()
req := &hello.Request{Name: "call grpc server by http client"}
// create request/response
request := client.NewRequest("go.micro.srv.greeter", "Say.Hello", req)
response := new(hello.Response)
// call service
err := c.Call(context.Background(), request, response)
log.Printf("err:%v response:%#v\n", err, response)
}

59
examples/http/srv/main.go Normal file
View File

@ -0,0 +1,59 @@
package main
import (
"log"
httpServer "github.com/asim/go-micro/plugins/server/http/v3"
"github.com/asim/go-micro/v3"
"github.com/asim/go-micro/v3/registry"
"github.com/asim/go-micro/v3/server"
"github.com/gin-gonic/gin"
)
const (
SERVER_NAME = "demo-http" // server name
)
func main() {
srv := httpServer.NewServer(
server.Name(SERVER_NAME),
server.Address(":8080"),
)
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(gin.Recovery())
// register router
demo := newDemo()
demo.InitRouter(router)
hd := srv.NewHandler(router)
if err := srv.Handle(hd); err != nil {
log.Fatalln(err)
}
service := micro.NewService(
micro.Server(srv),
micro.Registry(registry.NewRegistry()),
)
service.Init()
service.Run()
}
//demo
type demo struct{}
func newDemo() *demo {
return &demo{}
}
func (a *demo) InitRouter(router *gin.Engine) {
router.POST("/demo", a.demo)
}
func (a *demo) demo(c *gin.Context) {
c.JSON(200, gin.H{"msg": "call go-micro v3 http server success"})
}