Developing Microservices

Dubbo-go Quick Start

This example demonstrates how to develop microservice applications using dubbo-go, adding core microservice capabilities such as service discovery, load balancing, and traffic control to the application.

Prerequisites

In this example, we will continue to use Protobuf to develop the microservice application. Please refer to Develop RPC Server and RPC Client to learn how to install necessary plugins like protoc and protoc-gen-go-triple.

Quick Run Example

Download Example Source Code

We maintain a series of dubbo-go usage examples in the apache/dubbo-go-samples repository to help users quickly learn how to use dubbo-go.

You can download the example zip package and extract it, or clone the repository:

$ git clone --depth 1 https://github.com/apache/dubbo-go-samples

Switch to the quick start example directory:

$ cd dubbo-go-samples/registry/nacos

Start Nacos

Since the example application enables service discovery and uses Nacos as the registry, you need to start the registry before running the example. Please refer to Nacos Local Installation for quick installation and startup instructions for Nacos.

Run Server

In the go-server/cmd example directory:

Run the following command to start the server:

$ go run server.go

Verify that the server has started normally using cURL:

$ curl \
    --header "Content-Type: application/json" \
    --data '{"name": "Dubbo"}' \
    http://localhost:50051/greet.v1.GreetService/Greet

Greeting: Hello world

Run Client

Open a new terminal and run the following command to start the client:

$ go run client.go

Greeting: Hello world

This is a complete flow of a dubbo-go microservice application.

Source Code Explanation

Regarding the source code of the dubbo-go-samples/registry/nacos example, including service definitions, code generation, and server/client startup, it is similar to the previous rpc server & rpc client discussion. Please click the link above for specific interpretations.

The biggest difference in developing microservices is that the application includes configuration for the registry, as shown below (taking server.go as an example):

func main() {
	ins, err := dubbo.NewInstance(
		dubbo.WithName("dubbo_registry_nacos_server"),
		dubbo.WithRegistry(
			registry.WithNacos(),
			registry.WithAddress("127.0.0.1:8848"),
		),
		dubbo.WithProtocol(
			protocol.WithTriple(),
			protocol.WithPort(20000),
		),
	)

	srv, _ := ins.NewServer()

	greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{})

	if err := srv.Serve(); err != nil {
		logger.Error(err)
	}
}

Compared to when developing RPC services where we directly declare server.NewServer(...), here we use dubbo.NewInstance(...) to initialize several globally shared microservice components, including application name, registry, protocol, etc.:

ins, err := dubbo.NewInstance(
	dubbo.WithName("dubbo_registry_nacos_server"),
	dubbo.WithRegistry(
		registry.WithNacos(),
		registry.WithAddress("127.0.0.1:8848"),
	),
	dubbo.WithProtocol(
		protocol.WithTriple(),
		protocol.WithPort(20000),
	),
)

Then, we create ins.NewServer() and register services for the server instance greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{}), and finally start the process with srv.Serve().

If you want to add more service governance capabilities to the application, please refer to the following content:

More Content

Service Discovery and Load Balancing

More about using service discovery with Nacos, Zookeeper, etc., and configuring load balancing strategies.

Traffic Control

Learn how to achieve proportional traffic distribution, canary releases, adjust timeout settings, traffic grayscale, and service degradation.

Monitoring Service Status

Enable metrics collection and visualize application, service, and example statuses via Prometheus and Grafana.

Full Link Tracing

Enable OpenTelemetry full link tracing.

Gateway HTTP Access

How to use gateway products like Higress, Nginx, etc., to connect front-end HTTP traffic (northbound traffic) to the back-end dubbo-go microservices cluster.

Distributed Transactions

Use Apache Seata as a distributed transaction solution to address distributed data consistency issues.


Last modified September 30, 2024: Translate (e96744e441)