Go practice microservices-Service discovery

Source: Internet
Author: User
Tags docker compose

The registration of services is a very important part of MicroServices, in a single architecture application, the service calls each other, through a fixed host and port to initiate rest or RPC to invoke, but in the microservices architecture, each service is often dynamic change, Therefore, a service discovery mechanism is required to send the client's request to the dynamic service instance.

In the use of Go micro to achieve service discovery convenience, micro in the default support using Consul to do service discovery, of course it uses the plug-in mechanism (go-plugins) also support ETCD, GOSSIP, Nats and other third-party Services Registry Discovery tool. When each service starts, it registers itself on the registry, and automatically registers when exiting, so we can take a look at the relevant code snippet of Go-micro/service.go :

... func (s *service) run (exit Chan bool) {if S.opts.registerinterval <= time. Duration (0) {return}//timed to register itself t: = time. Newticker (S.opts.registerinterval) for {select {case <-t.c:err: = S.opts.server.register () if err! = Nil {log.        Log ("Service Run Server.register Error:", err)} case <-exit:t.stop () return }}}......func (S *service) Start () error {for _, fn: = range S.opts.beforestart {if err: = fn (); ER R! = Nil {return err}} If err: = S.opts.server.start (); The Err! = nil {return err}//Run () call also results in a run to be transferred here to register if err: = S.opts.server.register ();            Err! = Nil {return Err} for _, fn: = range S.opts.afterstart {if err: = FN (); Err! = Nil { Return err}} return Nil}func (S *service) Stop () error {var Gerr error for _, fn: = Range s.opts. BeforesTop {if Err: = FN (); Err! = Nil {Gerr = err}}//exit automatically de-register if err: = S.opts.server.de Register (); Err! = Nil {return err} if err: = S.opts.server.stop ();            Err! = Nil {return Err} for _, fn: = range S.opts.afterstop {if err: = FN (); Err! = Nil { Gerr = Err}} return Gerr} ...

The related use of Consul can be referred to "Consul Introduction and QuickStart", the following main to use a hotel booking example to see how go Micro Use Consul cluster to do service discovery.

The example uses a micro API that is an implementation of a MicroServices API gateway in the Micro component that provides a portal to the service, which dynamically routes to the appropriate backend service, uses it for service discovery, load balancing, Encoding and RPC-based communication.

The HTTP API provided by the Micro API is as follows:

- /[service]/[method]    # HTTP路径动态映射到services- /rpc # 通过名称和方法显示调用后端service

RPC Handler, which uses the Micro API in the example, is an alternative to the default handler that the Go-micro client forwards the request principal to the RPC request, using the Micro API and the rest mapping rule to view the document https://micro.mu/ Docs/api.html

The hotel reservation service utilizes the booking sample rewrite in the official micro/examples, the specific code = Https://github.com/yuansir/go ...

.├── README.md├── api│   └── hotel   # booking service├── data    # data│   ├── bindata.go│   ├── customers.json│   ├── locations.json│   ├── profiles.json│   └── rates.json├── docker-compose.yml # docker compose file└── srv # services    ├── auth    # auth token servce    ├── geo     # geo service    ├── profile # profile service    └── rate    # rate service

Docker-compose.yml

Version: ' 3 ' services:consul-agent-1: &consul-agent image:consul:latest Networks:-consul-cluster com Mand: "Agent-retry-join consul-server-bootstrap-client 0.0.0.0" consul-agent-2: <<: *consul-agent Consul-agen  T-3: <<: *consul-agent consul-server-1: &consul-server <<: *consul-agent command: "Agent-server -retry-join consul-server-bootstrap-client 0.0.0.0 "consul-server-2: <<: *consul-server consul-server-bootstr    AP: <<: *consul-agent Ports:-"8400:8400"-"8500:8500"-"8600:8600"-"8600:8600/UDP" Command: "Agent-server-bootstrap-expect 3-ui-client 0.0.0.0" Auth:build:./srv/auth Networks:-consul- Cluster command:--registry_address=consul-server-bootstrap:8500 Links:-Consul-server-bootstrap Geo:buil      D:./srv/geo Networks:-consul-cluster command:--registry_address=consul-server-bootstrap:8500 Links: -Consul-server-bootstrAP Profile:build:./srv/profile Networks:-consul-cluster command:--registry_address=consul-server-boots Trap:8500 Links:-Consul-server-bootstrap Rate:build:./srv/rate networks:-consul-cluster comma    Nd:--registry_address=consul-server-bootstrap:8500 Links:-Consul-server-bootstrap api:build:./api/hotel Networks:-consul-cluster command:--registry_address=consul-server-bootstrap:8500 Links:-Consul-serv Er-bootstrap-auth-geo-profile-rate micro:networks:-consul-cluster command:--re gistry_address=consul-server-bootstrap:8500 API--handler=rpc Image:microhq/micro:latest Links:-Consul-serv ER-BOOTSTRAP-API ports:-"8080:8080" Networks:consul-cluster:

Consul each data center must have at least one server, and it is recommended that you have 3 or 5 servers in a cluster. Deploying a single server will inevitably result in data loss in the event of a failure. -bootstrap-expect The option Tip Consul the number of server nodes we expect to join. Each service --registry_address is set up to register the service Discovery registry address.

docker-compose upYou can then view the status of the service through the Consul Web UI.

reprint Please specify: reproduced from Ryan is a rookie | LNMP Technology Stack Notes

If you think this article is very useful to you, why not give it a reward?

This article link address: Go practice microservices-Service discovery

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.