golang micro-services framework go-micro micro application notes entry tool 2.2 micro tool of micro web

micro web

micro very powerful, this article will elaborate on the command line feature micro web

Before reading this article you might need the following knowledge base

In this paper, environment

name description
operating system win10
go version go version go1.12.7 windows/amd64
Gofatः E:\winlion\gopath
GOPROXY https://goproxy.io
G111MODULE on
Command-line tool cmder tool itself mounted Baidu

Command Line Description

All parameters following instructions micro web

NAME:
   micro web - Run the web dashboard
USAGE:
   micro web [command options] [arguments...]

OPTIONS:
   --address value    Set the web UI address e.g 0.0.0.0:8082 [%MICRO_WEB_ADDRESS%]
   --namespace value  Set the namespace used by the Web proxy e.g. com.example.web [%MICRO_WEB_NAMESPACE%]
  • address used to access the specified web address, the default port is 8082,
  • namespace is used to specify the open web support service space, com.axample.web represents all meet com.axample.web. * Application support

Typical application scenarios

Micro-channel application development platform

A company is a small start-up companies, a company's main business is doing micro letter applet now their company is only one domain name www.xxx.com, Company A uses micro web tool, you can continue iteration new application to www.xxx.comthe next domain name, such as yesterday, they developed an application demo1 , extranet users access address https://www.xxx.com/demo1/, day tomorrow, they developed an application demo2, extranet users access address https://www.xxx.com/demo2/, and finally this platform so long

service name Explanation address
com.techidea8.web.www A company's primary domain name http://www.xxx.com/www/
com.techidea8.srv.wxmp A company's micro-channel service, such as signatures, share, menu management, etc. http://www.xxx.com/wxmp/
com.techidea8.web.attach A company's resources upload service http://www.xxx.com/attach/
com.techidea8.web.pay A company's payment services http://www.xxx.com/pay/
com.techidea8.web.account A company's User Center http://www.xxx.com/account/
com.techidea8.web.article A company writing class platform http://www.xxx.com/article/
com.techidea8.web.demo1 A company's application demo1 http://www.xxx.com/demo1/
com.techidea8.web.demox A company's application demox http://www.xxx.com/demox/

a company can continue iterative business, the platform with good expansion.

Distributed Task distribution platform

Openwrite is oriented writing distribution platform and technical personnel, a user writing at the platform, can distribute to · csdn / oschina / jianshu / juejin · and other channels, we use micro web development as micro applications, follow-up can continue iterative development of new channel services com.techidea8.channel*, will not affect the existing business.

service name Explanation address
com.techidea8.web.www website homepage http://www.xxx.com/www/
com.techidea8.web.attach Pictures, resources and other upload service http://www.xxx.com/attach/
com.techidea8.web.account User Center http://www.xxx.com/account/
com.techidea8.web.article Documentation Center http://www.xxx.com/article/
com.techidea8.web.channelcsdn csdn channel applications http://www.xxx.com/channelcsdn/
com.techidea8.web.channeljuejin Nuggets channel applications http://www.xxx.com/channeljuejin/

Combat hello, world!

Traffic grooming

Suppose we need to implement similar functionality as thousands of thousand faces, the specific application scenario is as follows

  • Home Applications www, show common basic information
  • Recommended support, different users (uuid identity), displaying different content

Service Design follows

service name Explanation address
go.micro.web.www Home System http://127.0.0.1:8082/www/
go.micro.srv.recommend Recommended Service http://127.0.0.1:8082/www/guess

Get the source code and run

The important thing to say three times a
micro web source code https://idea.techidea8.com/open/idea.shtml?id=7
micro web source code https://idea.techidea8.com/open/idea.shtml?id . 7 =
Micro Web source https://idea.techidea8.com/open/idea.shtml?id=7

After the download, unzip location%GOPATH%\src\techidea8.com\microapp\doc\web

$pwd
E:\winlion\gopath\src\techidea8.com\microapp\doc\web                      
$ls                                                                      
recommend/  www/                                                                                                                             

To execute the following instruction sequence

#切换到wwww目录下
src\techidea8.com\microapp\doc\web\www>go run main.go
#切换到recommend服务目录下
src\techidea8.com\microapp\doc\web\recommend>go run main.go
#启动web支持
src\techidea8.com\microapp\doc\web\recommend>micro web --namespace=go.micro.web

Interface is as follows
Service Operation effect

Application details on www

  • Creating Application Framework
>micro new --type=web techidea8.com/microapp/doc/web/www
  • Design proto file, there will need to request a Guess Interface
#E:\winlion\gopath\src\techidea8.com\microapp\doc\web\www\proto\recommend\recommend.proto
service Recommend{
    rpc Guess(Request)returns(Response){};
}
  • Files generated by proto go
#E:\winlion\gopath\src\techidea8.com\microapp\doc\web\www\ 目录下
>protoc --proto_path=. --proto_path=E:/winlion/gopath/src --go_out=. --micro_out=. proto/recommend/recommend.proto
#
  • You may also like to call the service, the core code is as follows
//handler/handler.go
//go.micro.srv.recommend 是后端猜你喜欢服务名称
guessClient := recommend.NewRecommendService("go.micro.srv.recommend", client.DefaultClient)
    rsp, err := guessClient.Guess(context.TODO(), &recommend.Request{
        Uuid: request["uuid"].(string),//uuid是前端用户ID
    })
  • You may also like restful interface supports / www / guess
    //默认需要加上服务名前奏
    // register html handler
    service.Handle("/", http.FileServer(http.Dir("html")))
    // register call handler/www/guess  
    service.HandleFunc("/guess", handler.Guess)
  • Start a web service
$micro web --namespace=go.micro.web
#结果如下
2019/08/25 15:32:08 HTTP API Listening on [::]:8082
2019/08/25 15:32:08 Transport [http] Listening on [::]:57563
2019/08/25 15:32:08 Broker [http] Connected to [::]:57564
2019/08/25 15:32:08 Registry [mdns] Registering node: go.micro.web-32fafadb-a795-46bc-b4d8-ad9a30607a7e

About recommend recommendation service details

  • Creating Application Framework
>micro new --type=srv techidea8.com/microapp/doc/web/recommend
  • Design proto file, there will need to request a Guess Interface
#E:\winlion\gopath\src\techidea8.com\microapp\doc\web\recommend\proto\recommend\recommend.proto
service Recommend{
    rpc Guess(Request)returns(Response){};
}
  • Files generated by proto go
#E:\winlion\gopath\src\techidea8.com\microapp\doc\web\recommend\ 目录下
>protoc --proto_path=. --proto_path=E:/winlion/gopath/src --go_out=. --micro_out=. proto/recommend/recommend.proto
#
  • Implement recommendation service, the core code is as follows
//handler\recommend.go
//go.micro.srv.recommend 是后端猜你喜欢服务名称
func (e *Recommend) Guess(ctx context.Context, req *recommend.Request, rsp *recommend.Response) error {
    log.Log("Received Recommend.Call request")
    //具体推荐算法实在这里实现的,
    //推荐算法得到结果后赋值给resp.Results
    rsp.Results = "这是用户[" + req.Uuid + "]的推荐内容"
    return nil
}
  • Cancel event-related characteristics

Here we do not consider the events, so will cancel the event-related characteristics, to a subscriber directory, and then modify main.go

main.go
    // 取消事件订阅支持
    //micro.RegisterSubscriber("go.micro.srv.recommend", service.Server(), new(subscriber.Hello))
    //取消事件发布支持
    //micro.RegisterSubscriber("go.micro.srv.recommend", service.Server(), subscriber.Handler)
  • Start recommendation service
$go run main.go

Recommended Reading

Sweep micro-channel two-dimensional code to achieve the landing site address provided experience and source code

Language open source project golang go backstage management framework restgo-admin

Support touch gestures, you can slide around the calendar plugin

You have to know 18 Internet business model

Guess you like

Origin www.cnblogs.com/techidea8/p/11408377.html