Golang Hot Load Configuration Practice

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Objective

Production environment on the output of high log level is not conducive to positioning, you will encounter such a scenario, we change the configuration to modify the log level, and then restart to expect to locate the problem, however, the problem does not appear after restarting, so the hot load configuration is necessary to add in.

Practice

Directly on the code:

 PackageMainImport("Encoding/json"    "FMT"    "Io/ioutil"    "OS"    "Os/signal"    "Syscall"    "Time")typeConfigstruct{Modestring ' JSON: ' Mode '}var(config *config)funcLoadconfig () {f, err: = Ioutil. ReadFile ("Config.json")ifErr! =Nil{FMT. Println ("[test_sigusr] Load config:", err)} err = json. Unmarshal (f, &config)ifErr! =Nil{FMT. Println ("[test_sigusr] Para config failed:", err)}}funcInit () {loadconfig () fmt. Println ("[test_sigusr] Load config; Mode: ", CONFIG. Mode) S: = Make(ChanOs. Signal,1) signal. Notify (S, Syscall. SIGUSR2)Go func() { for{<-s loadconfig () fmt. Println ("[test_sigusr] ReLoad config; Mode: ", CONFIG. Mode)}} ()}funcMain () {Q: = Make(ChanOs. Signal,1) signal. Notify (q, Syscall. SIGINT) for{Select{ Case<-q:fmt. Println ("[TEST_SIGUSR]---ibye----") OS. Exit(1)default:} fmt. Println ("[TEST_SIGUSR]---waiting to reload----") time. Sleep (time. Second *2)    }}

The file directory is as follows:

Wenhao-ma: Test_sigusr wenhao$ Lltotal4944-RW- R--R--  1Wenhao Staff -  One  A  -: theConfig.Json-RWXR-XR- x  1Wenhao Staff2520352  One  A  -: ASigusr-RW- R--R--  1Wenhao Staff973  One  A  -: -Sigusr.Go
wenhao-MA:test_sigusr wenhao$ cat config.json{"Mode":"Debug"}

After the compilation starts, modify config.json The median Mode , kill -USR2 $(pidof sigusr)
Results:

Wenhao-ma:test_sigusr wenhao$./sigusr [test_sigusr] Load config; Mode:  Debug[test_sigusr] ---Waiting  to Reload----[test_sigusr] ---Waiting  to Reload----[test_sigusr] ReLoad config; Mode:  ERROR

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.