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:

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

Switch to the quick start example directory:

  1. $ 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:

  1. $ go run server.go

Verify that the server has started normally using cURL:

  1. $ curl \
  2. --header "Content-Type: application/json" \
  3. --data '{"name": "Dubbo"}' \
  4. http://localhost:50051/greet.v1.GreetService/Greet
  5. Greeting: Hello world

Run Client

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

  1. $ go run client.go
  2. 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):

  1. func main() {
  2. ins, err := dubbo.NewInstance(
  3. dubbo.WithName("dubbo_registry_nacos_server"),
  4. dubbo.WithRegistry(
  5. registry.WithNacos(),
  6. registry.WithAddress("127.0.0.1:8848"),
  7. ),
  8. dubbo.WithProtocol(
  9. protocol.WithTriple(),
  10. protocol.WithPort(20000),
  11. ),
  12. )
  13. srv, _ := ins.NewServer()
  14. greet.RegisterGreetServiceHandler(srv, &GreetTripleServer{})
  15. if err := srv.Serve(); err != nil {
  16. logger.Error(err)
  17. }
  18. }

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.:

  1. ins, err := dubbo.NewInstance(
  2. dubbo.WithName("dubbo_registry_nacos_server"),
  3. dubbo.WithRegistry(
  4. registry.WithNacos(),
  5. registry.WithAddress("127.0.0.1:8848"),
  6. ),
  7. dubbo.WithProtocol(
  8. protocol.WithTriple(),
  9. protocol.WithPort(20000),
  10. ),
  11. )

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

About dubbo.Instance

  • When developing dubbo microservice applications, we recommend using dubbo.Instance to set up some global service governance capabilities, such as registry, protocol, application name, tracing, configuration center, etc.
  • ins.NewServer() can create multiple instances, typically needed when you want to publish multiple protocols on different ports.

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.

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.


Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)