Source code analysis of go open source project influxdb relay (1)

Posted by scifo on Sun, 29 Dec 2019 15:41:11 +0100

The address of the influxdb relay project: https://github.com/influxdata/influxdb-relay, which is mainly used as a load balancing node and writes to multiple influxdb nodes for high availability.

Don't talk about it~

1,main.go

 1 package main
 2 
 3 import (
 4     "flag"
 5     "fmt"
 6     "log"
 7     "os"
 8     "os/signal"
 9 
10     "github.com/influxdata/influxdb-relay/relay"
11 )
12 
13 var (
14     //influxdb-realy Profile parameters received at startup,the reason being that falg.String So it receives a string type parameter
15     //flag.String The first parameter of the function represents the name of the parameter at startup, the second represents the default value, and the third represents the usage description. This method returns a pointer
16     //Then start up realy It should look like this: ./influxdb-relay  -config  relay.toml  -- "-config" It's a match flag.String Function first argument, "relay.toml" yes-config Value,Here is the path of the configuration file
17     configFile = flag.String("config", "", "Configuration file to use")
18 )
19 
20 func main() {
21     //flag.Parse Is used to parse the parameters passed in at startup. You must call
22     flag.Parse()
23 
24     //If the startup parameter, the profile path is empty
25     if *configFile == "" {
26         //Log
27         fmt.Fprintln(os.Stderr, "Missing configuration file")
28         //Print required parameter prompt,That is to say, it will output flag.String Function third argument
29         flag.PrintDefaults()
30         //Sign out
31         os.Exit(1)
32     }
33     //Load profile
34     cfg, err := relay.LoadConfigFile(*configFile)
35     //LoadConfigFile The main thing to do is to deserialize the configuration file into a Config Object then return,that is cfg
36     // func LoadConfigFile(filename string) (cfg Config, err error) {
37     //    f, err := os.Open(filename)
38     //    if err != nil {
39     //        return cfg, err
40     //    }
41     //    defer f.Close()
42     //    return cfg, toml.NewDecoder(f).Decode(&cfg)
43     // }
44     if err != nil {
45         fmt.Fprintln(os.Stderr, "Problem loading config file:", err)
46     }
47     //according to Config Object creation relay assembly
48     r, err := relay.New(cfg)
49     if err != nil {
50         log.Fatal(err)
51     }
52     //Define the channel
53     sigChan := make(chan os.Signal, 1)
54     //notify For monitoring signal,If the operating system sends an interrupt signal to the current process(os.Interrupt Indicates interrupt signal)
55     signal.Notify(sigChan, os.Interrupt)
56     //If interrupt signal is received, start a process call relay Of Stop Method stop http Monitor
57     go func() {
58         <-sigChan
59         r.Stop()
60     }()
61 
62     log.Println("starting relays...")
63     //relay Function
64     r.Run()
65 }

2. Configuration file relay.toml

 1 [[http]]
 2 name = "example-http"
 3 bind-addr = "127.0.0.1:9096"
 4 output = [
 5     { name="influxdb-1", location = "http://127.0.0.1:8086/write" },
 6     { name="influxdb-2", location = "http://127.0.0.1:7086/write" },
 7 ]
 8 
 9 [[udp]]
10 name = "example-udp"
11 bind-addr = "127.0.0.1:19096"
12 read-buffer = 0 # default
13 output = [
14     { name="influxdb-1", location="127.0.0.1:8089", mtu=512 },
15     { name="influxdb-2", location="127.0.0.1:7089", mtu=1024 },
16 ]

 

Conclusion:

1. Load the configuration file relay.toml

2. Create a relay according to the configuration file, start the listening port, and receive the request

3. Write the received request to the influxdb1-2 node

 

Later, we will study how the core module relay writes the received data to multiple nodes at the same time.

Topics: Go InfluxDB github