Home Backend Development Golang How to build a cluster in golang

How to build a cluster in golang

Apr 25, 2023 am 10:42 AM

1. Foreword

With the development of the Internet, the concurrent performance of applications has become a topic of increasing concern. As a highly concurrent programming language, golang is becoming more and more popular among developers. It comes with GC mechanism, coroutine and channel support, which greatly reduces the complexity and development difficulty of the program.

This article will introduce how to use golang to build a simple cluster to better allocate concurrent requests and improve the performance and reliability of the program.

2. The principle of building a cluster

Before introducing the specific operations, let’s first understand the principle of building a cluster. Generally speaking, a cluster can be simply understood as multiple servers running the same or different applications. Multiple servers communicate through the network to complete functions such as load balancing and data sharing.

In golang, there is a package called net/http, which can easily build an http server. In addition to the http server, we also need to support server service discovery, load balancing and other functions in the cluster. At this time, you can use third-party components like zookeeper to implement it.

In this article, we will use etcd as the service registration center in the cluster to complete the functions of load balancing and service discovery.

3. Environment preparation

Before starting the configuration, we need to install the corresponding tools and environment.

  1. golang environment

Download and configure the golang environment on the official website. You can download the corresponding installation package from "https://golang.org/dl/" .

  1. etcd

etcd is a distributed key-value storage system open sourced by coreos company, which can easily realize functions such as load balancing and service registration. The corresponding version can be downloaded from "https://github.com/etcd-io/etcd/releases".

4. Specific operations

  1. Write http service program

First, we need to write an http service program to handle client requests. Here we can use the net/http package built into the golang system. The most basic operation is the ListenAndServe function, which is used to start the http service.

Next, we write a program to listen to a local http request and return a sentence to the client.

The code is as follows:

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Hello World")
    })

    http.ListenAndServe(":8080", nil)
}
Copy after login
  1. Configuring etcd

In etcd, we need to first create a key-value pair to register the service. In this example, we use /services/httpServer as the service path and the localhost IP address and port number 8080 as the node value.

Execute the following command under etcd client to complete the registration:

curl -L http://127.0.0.1:2379/v2/keys/services/httpServer -XPUT -d value='{"host":"localhost", "port":"8080"}'
Copy after login

Configure multiple service nodes in etcd to achieve load balancing and high availability.

  1. Writing etcd secure access module

In etcd cluster, we need to achieve more secure service access and load balancing. Here we will use the etcd_sdk package, which can be conveniently used to connect to the etcd registration center and read service nodes.

It is recommended that when writing a service program, read etcd registration information and constantly monitor registration changes to maintain synchronization with the cluster registration center.

The code is as follows:

package main

import (
    "context"
    "fmt"
    "github.com/coreos/etcd/clientv3"
    "net/http"
    "strings"
    "sync"
    "time"
)

var (
    endpoints []string
    currentConfig clientv3.Config
    etcdConfigLocker sync.Mutex
)

func getConfig()(clientv3.Config, error) {
    etcdConfigLocker.Lock()
    defer etcdConfigLocker.Unlock()

    if endpoints == nil {
        return clientv3.Config{}, fmt.Errorf("no endpoints available")
    }

    return clientv3.Config{
        Endpoints: endpoints,
        DialTimeout: 5 * time.Second,
    }, nil
}

type ServiceInfo struct {
    Key    string `json:"key"`
    Value  string `json:"value"`
}

func main() {
    endpoints = append(endpoints, "127.0.0.1:2379")
    readServices()

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        url := fmt.Sprintf("http://%s%s", getService(), r.URL.Path)
        fmt.Printf("Forward %s => %s\n", r.URL.Path, url)
        http.Redirect(w, r, url, 307)
    })
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        panic(err)
    }
    readServices()
}

func getService() string {
    config, err := getConfig()
    if err != nil {
        panic(err)
    }

    client, err := clientv3.New(config)
    if err != nil {
        panic(err)
    }

    defer client.Close()

    prefix := "services/httpServer"

    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    resp, err := client.Get(ctx, prefix, clientv3.WithPrefix())
    cancel()
    if err != nil {
        panic(err)
    }

    services := make([]*ServiceInfo, 0)
    for _, kv := range resp.Kvs {
        services = append(services, &ServiceInfo{
            Key: string(kv.Key),
            Value: string(kv.Value),
        })
    }

    if len(services) == 0 {
        panic(fmt.Errorf("no endpoint available"))
    }

    return strings.Replace(services[0].Value, "\"", "", -1)
}

func readServices() {
    go func() {
        for {
            getConfigFromEtcd()
            time.Sleep(5 * time.Second)
        }
    }()
}

func getConfigFromEtcd() {
    client, err := clientv3.New(currentConfig)
    if err != nil {
        fmt.Printf("ERROR: create etcd client failed: %s\n", err.Error())
        return
    }

    defer client.Close()

    key := "services/httpServer"
    ctx, cancel := context.WithTimeout(context.Background(), 10 * time.Second)
    resp, err := client.Get(ctx, key, clientv3.WithPrefix())
    cancel()

    if err != nil {
        fmt.Printf("ERROR: get etcd key(%s) failed: %s\n", key, err.Error())
        return
    }

    tempEndpoints := make([]string, 0, len(resp.Kvs))
    for _, itm := range resp.Kvs {
        value := string(itm.Value)
        tempEndpoints = append(tempEndpoints, value)
    }

    fmt.Printf("INFO: get endpoints from etcd(%s) success: %v\n", currentConfig.Endpoints, tempEndpoints)

    currentConfig = clientv3.Config{
        Endpoints: tempEndpoints,
        DialTimeout: 5 * time.Second,
    }
}
Copy after login

In the code, we use the clientv3 package in etcd sdk to connect to etcd registration center and obtain service node information from it. The getConfig() and getConfigFromEtcd() functions are used to read etcd registry information.

  1. Run the service program

After configuring the above steps, we can run the program. Open the terminal, switch to the project directory, and execute the following command:

go run main.go
Copy after login

After successful operation, open the browser and visit http://127.0.0.1:8080, you will see the program print out "Hello World" , indicating that the program has been successfully run.

In this example, we use the http service. In actual projects, we can also use high-performance protocols like grpc to improve program performance.

5. Summary

In this article, we introduced the principles and specific operations of using golang to build a cluster. By using etcd registration center and corresponding sdk package, we realize the registration, reading and dynamic maintenance of service nodes. This helps improve the performance and reliability of the program and leads to a better user experience.

In practical applications, we also need to pay attention to the security and fault tolerance of the program to ensure the reliability of the program.

The above is the detailed content of How to build a cluster in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Go language pack import: What is the difference between underscore and without underscore? Go language pack import: What is the difference between underscore and without underscore? Mar 03, 2025 pm 05:17 PM

This article explains Go's package import mechanisms: named imports (e.g., import "fmt") and blank imports (e.g., import _ "fmt"). Named imports make package contents accessible, while blank imports only execute t

How to convert MySQL query result List into a custom structure slice in Go language? How to convert MySQL query result List into a custom structure slice in Go language? Mar 03, 2025 pm 05:18 PM

This article details efficient conversion of MySQL query results into Go struct slices. It emphasizes using database/sql's Scan method for optimal performance, avoiding manual parsing. Best practices for struct field mapping using db tags and robus

How to implement short-term information transfer between pages in the Beego framework? How to implement short-term information transfer between pages in the Beego framework? Mar 03, 2025 pm 05:22 PM

This article explains Beego's NewFlash() function for inter-page data transfer in web applications. It focuses on using NewFlash() to display temporary messages (success, error, warning) between controllers, leveraging the session mechanism. Limita

How can I define custom type constraints for generics in Go? How can I define custom type constraints for generics in Go? Mar 10, 2025 pm 03:20 PM

This article explores Go's custom type constraints for generics. It details how interfaces define minimum type requirements for generic functions, improving type safety and code reusability. The article also discusses limitations and best practices

How do I write mock objects and stubs for testing in Go? How do I write mock objects and stubs for testing in Go? Mar 10, 2025 pm 05:38 PM

This article demonstrates creating mocks and stubs in Go for unit testing. It emphasizes using interfaces, provides examples of mock implementations, and discusses best practices like keeping mocks focused and using assertion libraries. The articl

How to write files in Go language conveniently? How to write files in Go language conveniently? Mar 03, 2025 pm 05:15 PM

This article details efficient file writing in Go, comparing os.WriteFile (suitable for small files) with os.OpenFile and buffered writes (optimal for large files). It emphasizes robust error handling, using defer, and checking for specific errors.

How do you write unit tests in Go? How do you write unit tests in Go? Mar 21, 2025 pm 06:34 PM

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

How can I use tracing tools to understand the execution flow of my Go applications? How can I use tracing tools to understand the execution flow of my Go applications? Mar 10, 2025 pm 05:36 PM

This article explores using tracing tools to analyze Go application execution flow. It discusses manual and automatic instrumentation techniques, comparing tools like Jaeger, Zipkin, and OpenTelemetry, and highlighting effective data visualization

See all articles