Fluentd
Search…
Plugin Helper: Service Discovery
The service_discovery plugin helper provides users with the service discovery functionality.
Example:
1
require 'fluent/plugin/output'
2
3
module Fluent::Plugin
4
class ExampleOutput < Output
5
Fluent::Plugin.register_output('example', self)
6
7
# 1. Load service_discovery helper
8
helpers :service_discovery
9
10
def configure(conf)
11
super
12
13
# 2. Create and start service discovery manager
14
service_discovery_configure(
15
:out_example_service_discovery_watcher,
16
static_default_service_directive: 'server'
17
)
18
end
19
20
def write(chunk)
21
# 3. Select service to send data
22
service_discovery_select_service do |node|
23
send_data(node, chunk)
24
end
25
end
26
27
def send_data(node, chunk)
28
# Send data
29
end
30
end
31
end
Copied!
NOTE: The launched plugin itself is managed by its plugin helper which stops it automatically. No need to stop it in the stop method.

Methods

service_discovery_configure(title, static_default_service_directive: nil, load_balancer: nil, custom_build_method: nil, interval: 3)

Since v1.13.0, discovery_manager is almost automatically configured only by calling this method.

Parameters

    title: Thread name. Must be unique. (required)
    static_default_service_directive: The directive name of each service when "static" service discovery is enabled in default.
    load_balancer: object which has two methods #rebalance and #select_service.
    custom_build_method: The custom method to build the service.
    interval: Time interval for updating target service.

service_discovery_create_manager(title, configurations:, load_balancer: nil, custom_build_method: nil, interval: 3)

This method creates service_discovery_manager.

Parameters

    title: Thread name. Must be unique. (required)
    configurations: Configuration of target service. (required)
    load_balancer: Balancing load to target servers. (default: Round-Robin)
    custom_build_method: The custom method to build the service.
    interval: Time interval for updating target service.

discovery_manager

It manages service discovery functionalities such as updating target services and selecting target services. It provides the select_service method that returns a target service to send data.

Plugins using service_discovery

Migration guide from service_discovery_create_manager to more simpler helper method

Here is the guide to migrate to newer API which is available since v1.13.0.
Example:
1
require 'fluent/plugin/output'
2
3
module Fluent::Plugin
4
class ExampleOutput < Output
5
Fluent::Plugin.register_output('example', self)
6
7
helpers :service_discovery
8
9
def configure(conf)
10
super
11
12
# 1. Remove the following code which parse 'service_discovery' section by yourself
13
#
14
# config = conf.elements(name: 'service_discovery').map do |s|
15
# { type: :static, conf: s }
16
# end
17
18
# 2. Remove the following code and use service_discovery_configure
19
#
20
# service_discovery_create_manager(
21
# :out_example_service_discovery_watcher,
22
# configurations: config,
23
# )
24
service_discovery_configure(
25
:out_example_service_discovery_watcher,
26
static_default_service_directive: 'server'
27
)
28
end
29
30
def write(chunk)
31
# 3. Remove the following code and use service_discovery_select_service to select service
32
#
33
# @discovery_manager.select_service do |node|
34
# send_data(node, chunk)
35
# end
36
service_discovery_select_service do |node|
37
send_data(node, chunk)
38
end
39
end
40
41
def send_data(node, chunk)
42
# Send data
43
end
44
end
45
end
Copied!
You can also use helper methods such as service_discovery_services or service_discovery_rebalance instead of @discovery_manager.services or @discovery_manager.rebalance.
If this article is incorrect or outdated, or omits critical information, please let us know. Fluentd is an open-source project under Cloud Native Computing Foundation (CNCF). All components are available under the Apache 2 License.
Last modified 4mo ago