In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how the service registration of the micro services framework Go-Micro integration Nacos is. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Related background knowledge 1. Go-Micro
Go Micro is a basic framework for building micro-services based on Go language, which provides the core components needed for distributed development, including RPC and event-driven communication.
Its design philosophy is a "pluggable" plug-in architecture, and its core focuses on providing low-level interface definitions and basic tools that are compatible with various implementations. For example, Go Micro defaults to service discovery through consul, communicates through HTTP protocol, and codec through protobuf and json, so that you can start quickly based on these out-of-the-box components, but if necessary, you can also replace the default components with other components that conform to the underlying interface definition, such as service discovery through nacos, etcd or zookeeper This is also the advantage of plug-in architecture: no need to modify any underlying code to achieve the replacement of upper-level components.
1) Overview of Micro
Micro is a micro-service toolkit that includes:
API
Provide and route HTTP requests to the API gateways of the corresponding microservices. It acts as a single entry for microservice access, and converting HTTP requests to RPC and forwarding them to the appropriate service can also be used as a reverse proxy.
Web
UI is the web version of go-micro that allows interactive access to the environment through UI. In the future, it will also be a way to aggregate micro-Web services. It contains a proxy method for Web applications. Route / [name] to the appropriate service through the registry. Web UI prefixes "go.micro.web." (configurable) add to the name, look for it in the registry, and then reverse proxy.
Sidecar
The HTTP interface version of go-micro, which is a way to integrate non-Go applications into the microenvironment.
Bot
Hubot-style bot, located in your micro-service platform, can interact with each other through Slack,HipChat,XMPP, etc. It provides the functionality of CLI through message passing. You can add other commands to automate common operational tasks.
CLI
A direct command-line interface to interact with your microservice provides a way to observe and interact with the runtime environment.
2) Go-Micro components
Plug-in RPC framework for writing micro-services in Go. It provides libraries for service discovery, client-side load balancing, coding, synchronous and asynchronous communication. Go-micro is a separate library that can be used independently of other toolkits.
Go-micro is a component-based framework, and each basic function is an interface, which is easy to expand. At the same time, the components are layered, and the upper layer provides services based on the functions of the lower layer, which constitutes the go-micro framework as a whole. The components of the go-micro framework are:
Registry
Provide service discovery mechanism: resolve service name to service address. Currently supported registries are consul, etcd, zookeeper, dns, gossip and so on.
Selector
The selector provides a load balancing mechanism through selection. When the client makes a request to the server, it first queries the registry of the service. This usually returns a list of running nodes that represent the service. The selector selects one of these nodes for the query. Multiple calls to the selector will allow the use of the balancing algorithm. The current method is circular method, random hash and blacklist.
Broker
Pluggable interfaces for publish and subscribe, asynchronous communication between services based on message middleware, using http by default, and message middleware such as Nats, Kafka, RabbitMQ and http (for development) are usually used online.
Transport
A pluggable interface that transmits messages through point-to-point. The current implementations are http,rabbitmq and nats. By providing this abstraction, transportation can be replaced seamlessly.
Codec
Encoding / decoding of messages between services.
Plugins
Provides the micro/go-plugins plug-in for go-micro.
Server
The server is the interface for building the running micro-service. It provides a way to provide RPC requests. This component is based on the above Registry/Selector/Transport/Broker component and provides a unified service request entry.
Client
Provide a method of making RPC query to access the client of the microservice. It combines registry, selector, agent and transport. It also provides retry, timeout, usage context, and so on. Similar to the Server component, it also realizes the functions of lookup service, load balancing, synchronous communication, asynchronous message and so on through the Registry/Selector/Transport/Broker component.
2. Nacos
Nacos is a platform for dynamic service discovery, configuration management and service management that is easier to build cloud native applications. Nacos is an open source implementation of Nacos, which is born out of Alibaba's internal ConfigServer and Diamond. Having experienced the test of the peak flow of Singles' Day and the super-large-scale capacity of Alibaba's economy, Alibaba's soft-load team has ten years of experience in this field, and has a good guarantee in terms of stability and functionality.
An overview of the Nacos logical architecture and its components:
Quick start 1. Go-Micro server 1) install protoc
Protobuf, short for Protocol Buffers, is a data description language developed by Google and opened to the outside world in 2008. When Protobuf was just open source, its positioning was similar to data description languages such as XML and JSON. It generated code with tools and serialized structured data. We need to generate server-side code using protoc.
O get github.com/golang/protobuf/protoc-gen-go2) install Go-Micro/v2go install github.com/micro/micro/v23) create a new golang project (server)
Create a proto folder under the root of the project to store protobuf files
Create a greeter.proto file under the proto folder
The contents of the document are as follows:
A service called Greeter is defined in the server using the protobuf file, which has a Hello method that receives HelloRequest and returns HelloResponse.
Syntax = "proto3"; package helloworld;service Greeter {rpc Hello (HelloRequest) returns (HelloResponse) {} message HelloRequest {string name = 1;} message HelloResponse {string greeting = 2;}
Generate the corresponding Go code
Protoc-micro_out=. -- go_out=. Proto/greeter.proto
In the proto directory, two files, pb.go and pb.micro.go, are generated
Create a server.go and run it
Package mainimport ("context" helloworld "go-micro-nacos-demo1/proto"github.com/micro/go-micro/v2"github.com/micro/go-micro/v2/logger"github.com/micro/go-micro/v2/registry" nacos "github.com/micro/go-plugins/registry/nacos/v2") type Helloworld struct {} / / Call is a single request handler called via client.Call or the generated client codefunc (e * Helloworld) Hello (ctx context.Context Req * helloworld.HelloRequest, rsp * helloworld.HelloResponse) error {logger.Info ("Received Helloworld.Call request") return nil} func main () {addrs: = make ([] string 1) addrs [0] = "console.nacos.io:80" registry: = nacos.NewRegistry (func (options * registry.Options) {options.Addrs = addrs}) service: = micro.NewService (/ / Set service name micro.Name ("my.micro.service"), / / Set service registry micro.Registry (registry),) helloworld.RegisterGreeterHandler (service.Server (), new (Helloworld)) service.Run ()
You can see the successful registration of my.micro.service in Nacos console
2. Go-Micro client
Create a client.go (for demonstration purposes, this article created a cient.go under the same project).
Package mainimport ("context"fmt" helloworld "go-micro-nacos-demo1/proto"github.com/micro/go-micro/v2"github.com/micro/go-micro/v2/registry" nacos "github.com/micro/go-plugins/registry/nacos/v2") const serverName = "my.micro.service" func main () {addrs: = make ([] string 1) addrs [0] = "console.nacos.io:80" r: = nacos.NewRegistry (func (options * registry.Options) {options.Addrs = addrs}) / / define the service You can pass in other optional parameters service: = micro.NewService (micro.Name ("my.micro.service.client"), micro.Registry (r)) / / create a new client greeter: = helloworld.NewGreeterService (serverName, service.Client ()) / / call greeter rsp, err: = greeter.Hello (context.TODO ()) & helloworld.HelloRequest {Name: "John"}) if err! = nil {fmt.Println (err)} / / get all services fmt.Println (registry.ListServices ()) / / get a service services, err: = registry.GetService (serverName) if err! = nil {fmt.Println (err)} / / listening service watch Err: = registry.Watch () fmt.Println (services) / / print response request fmt.Println (rsp.Greeting) go service.Run () for {result, err: = watch.Next () if len (result.Action) > 0 {fmt.Println (result, err)}
Run the client, and you can see in nacos console that the client service is also registered with nacos.
The call log is printed in the console of server.go.
3. Go-Micro Integration Nacos function description 1) server.go
Server: use go-micro to create a server-side Demo and register with nacos.
Registry: = nacos.NewRegistry (func (options * registry.Options) {options.Addrs = addrs}) service: = micro.NewService (/ / Set service name micro.Name ("my.micro.service"), / / Set service registry micro.Registry (registry),) service.Run (2) client.go
Create a client Demo using go-micro and register to nacos:
R: = nacos.NewRegistry (func (options * registry.Options) {options.Addrs = addrs}) service: = micro.NewService (micro.Name ("my.micro.service.client"), micro.Registry (r))
The client calls:
/ / create a new client greeter: = helloworld.NewGreeterService (serverName, service.Client ()) / / call greeter rsp, err: = greeter.Hello (context.TODO (), & helloworld.HelloRequest {Name: "John"})
Query the list of services:
Services,err:=registry.ListServices ()
Get a service:
Service, err: = registry.GetService (serverName)
Subscription service:
Watch, err: = registry.Watch () for {result, err: = watch.Next () if len (result.Action) > 0 {fmt.Println (result, err)}} on the micro service framework Go-Micro integration Nacos service registration is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.