github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/heartbeat/README.md (about)

     1  # Heartbeat
     2  
     3  A demonstration of using heartbeating with service discovery.
     4  
     5  ## Rationale
     6  
     7  Services register with service discovery on startup and deregister on shutdown. Sometimes these services may unexpectedly die or 
     8  be killed forcefully or face transient network issues. In these cases stale nodes will be left in service discovery. It would be 
     9  ideal if services were automatically removed.
    10  
    11  ## Solution
    12  
    13  Micro supports the option of a register TTL and register interval for this exact reason. TTL specifies how long a registration should 
    14  exist in discovery after which it expires and is removed. Interval is the time at which a service should re-register to preserve 
    15  it's registration in service discovery.
    16  
    17  These are options made available in go-micro and as flags in the micro toolkit
    18  
    19  ## Toolkit
    20  
    21  Run any component of the toolkit with the flags like so
    22  
    23  ```
    24  micro --register_ttl=30 --register_interval=15 api
    25  ```
    26  
    27  This example shows that we're setting a ttl of 30 seconds with a re-register interval of 15 seconds.
    28  
    29  ## Go Micro
    30  
    31  When declaring a micro service you can pass in the options as time.Duration
    32  
    33  ```
    34  service := micro.NewService(
    35  	micro.Name("com.example.srv.foo"),
    36  	micro.RegisterTTL(time.Second*30),
    37  	micro.RegisterInterval(time.Second*15),
    38  )
    39  ```