go-micro examples 中web & form 代码学习(web类 网站的微服务架构:micro api + micro web + web service + api service) - Go语言中文社区

go-micro examples 中web & form 代码学习(web类 网站的微服务架构:micro api + micro web + web service + api service)


以下对应 example/web 例子:

main.go 代码如下:

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/micro/go-micro/web"
)

func helloWorldHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, `<html><body><h1>Hello World</h1></body></html>`)
}

func main() {
	service := web.NewService(
		web.Name("helloworld"),
	)

	service.HandleFunc("/", helloWorldHandler)

	if err := service.Init(); err != nil {
		log.Fatal(err)
	}

	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}

运行:

2020-01-09 17:52:58.159564 I | Listening on [::]:54815

访问: localhost:54815;

--------------------------------------------

以下对应 example/form 例子:

这个例子, 展示了 如何 使用 web services 和 api services  展示 一个 form 表单,并post 提交;

//有接口有页面,有点像 web 页面应用 ; //当然 api services  是 很通用的。

目录结构,如下:

web/main.go 提供  页面服务

api/proto/api.proto 代码如下:

syntax = "proto3";

import "github.com/micro/go-micro/api/proto/api.proto";

service Form {
    //常规 form
    rpc Submit(go.api.Request) returns (go.api.Response) {};

    //multipart form
    rpc Multipart(go.api.Request) returns (go.api.Response) {};
}

bogon:proto zhaozhiliang$  protoc --proto_path=$GOPATH/src:. --micro_out=. --go_out=. api.proto

api/main.go 代码如下:

package main

import (
	"bytes"
	"context"
	"fmt"
	proto "form/api/proto"
	"github.com/micro/go-micro"
	api "github.com/micro/go-micro/api/proto"
	"log"
	"mime"
	"mime/multipart"
	"strings"
)

type Form struct{}

func (f *Form) Submit(ctx context.Context, req *api.Request, rsp *api.Response) error {
	fmt.Println("received Submit Request")
	rsp.Body = fmt.Sprintf("got your value %+v", req.Post)

	return nil
}

func (f *Form) Multipart(ctx context.Context, req *api.Request, rsp *api.Response) error {
	fmt.Println("received Multipart Submit Request")

	ct := strings.Join(req.Header["Content-Type"].Values, ",")
	mt, p, err := mime.ParseMediaType(ct)
	if err != nil {
		return err
	}

	if !strings.HasPrefix(mt, "multipart/") {
		return fmt.Errorf("%v does not contain multipart", mt)
	}

	r := multipart.NewReader(bytes.NewReader([]byte(req.Body)), p["boundary"])
	form, err := r.ReadForm(32 << 20)

	if err != nil {
		return err
	}

	rsp.Body = fmt.Sprintf("got your values %+v", form)
	return nil
}

func main() {
	service := micro.NewService(
		micro.Name("go.micro.api.form"),
	)

	service.Init()

	proto.RegisterFormHandler(service.Server(), new(Form))

	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}

web/main.go 代码如下:

package main

import (
	"fmt"
	"github.com/micro/go-micro/web"
	"log"
	"net/http"
)

func index(w http.ResponseWriter, r *http.Request) {
	log.Println("received a web service!")
	fmt.Fprint(w, `<html>
<body>
<h1>This is a regular form</h1>
<form action="http://192.168.1.129:8080/form/submit" method="POST">
<input type="text" id="thing" name="thing" />
<button>submit</button>
</form>
<h1>This is a multipart form</h1>
<form action="http://192.168.1.129:8080/form/multipart" method="POST" enctype="multipart/form-data">
<input type="text" id="thing" name="thing" />
<button>submit</button>
</form>
</body>
</html>
`)
}

func main() {
	service := web.NewService(
		web.Name("go.micro.web.form"),
	)

	service.Init()
	service.HandleFunc("/", index)
	service.Run()
}

运行:

运行 api  网关 ,handler=api

micro api --handler=api

[root@liang form]# micro api --handler=api
2020-01-10 19:00:09.026984 I | [api] Registering API Request Handler at /
2020-01-10 19:00:09.030256 I | [api] HTTP API Listening on [::]:8080
2020-01-10 19:00:09.030538 I | [api] Transport [http] Listening on [::]:16917
2020-01-10 19:00:09.030740 I | [api] Broker [http] Connected to [::]:33890
2020-01-10 19:00:09.035881 I | [api] Registry [mdns] Registering node: go.micro.api-3123f62e-53d7-4069-ba4d-f1b2c95b8c10

[root@liang mnt]# micro list services
go.micro.api
go.micro.http.broker
[root@liang mnt]# 

运行 micro web  

[root@liang ~]# micro web
2020-01-10 19:02:32.852125 I | [web] HTTP API Listening on [::]:8082
2020-01-10 19:02:32.852256 I | [web] Transport [http] Listening on [::]:2482
2020-01-10 19:02:32.852356 I | [web] Broker [http] Connected to [::]:56416
2020-01-10 19:02:32.852632 I | [web] Registry [mdns] Registering node: go.micro.web-4a62f24f-b081-4dda-a99f-dee47a3e69bd

[root@liang mnt]# micro list services
go.micro.api
go.micro.http.broker
go.micro.web
[root@liang mnt]# 

运行 api service  /   api/main.go

[root@liang form]# go run api/main.go
2020-01-10 19:04:53.965230 I | Transport [http] Listening on [::]:13042
2020-01-10 19:04:53.965342 I | Broker [http] Connected to [::]:59072
2020-01-10 19:04:53.966033 I | Registry [mdns] Registering node: go.micro.api.form-43924b09-31ff-487e-a8f3-e052d4ff7734

[root@liang mnt]# micro list services
go.micro.api
go.micro.api.form
go.micro.http.broker
go.micro.web
[root@liang mnt]# 

运行 web service / web/main.go

[root@liang form]# go run web/main.go
2020-01-10 19:05:53.742502 I | Listening on [::]:55204

[root@liang mnt]# micro list services
go.micro.api
go.micro.api.form
go.micro.http.broker
go.micro.web
go.micro.web.form
[root@liang mnt]# 

浏览器访问:

http://192.168.1.129:8082/form

架构图:

go.mod 文件如下:

module form

go 1.13

require github.com/micro/go-micro v1.18.0 // indirect

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u013862108/article/details/103932024
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-15 21:41:43
  • 阅读 ( 1001 )
  • 分类:架构

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢