Advanced

While not typically needed by most, these guides show advanced ways you can configure your Go pluggable components.

Component lifetime

Pluggable components are registered by passing a “factory method” that is called for each configured Dapr component of that type associated with that socket. The method returns the instance associated with that Dapr component (whether shared or not). This allows multiple Dapr components of the same type to be configured with different sets of metadata, when component operations need to be isolated from one another, etc.

Registering multiple services

Each call to Register() binds a socket to a registered pluggable component. One of each component type (input/output binding, pub/sub, and state store) can be registered per socket.

  1. func main() {
  2. dapr.Register("service-a", dapr.WithStateStore(func() state.Store {
  3. return &components.MyDatabaseStoreComponent{}
  4. }))
  5. dapr.Register("service-a", dapr.WithOutputBinding(func() bindings.OutputBinding {
  6. return &components.MyDatabaseOutputBindingComponent{}
  7. }))
  8. dapr.Register("service-b", dapr.WithStateStore(func() state.Store {
  9. return &components.MyDatabaseStoreComponent{}
  10. }))
  11. dapr.MustRun()
  12. }

In the example above, a state store and output binding is registered with the socket service-a while another state store is registered with the socket service-b.

Configuring Multiple Components

Configuring Dapr to use the hosted components is the same as for any single component - the component YAML refers to the associated socket. For example, to configure Dapr state stores for the two components registered above (to sockets service-a and service-b), you create two configuration files, each referencing their respective socket.

  1. #
  2. # This component uses the state store associated with socket `service-a`
  3. #
  4. apiVersion: dapr.io/v1alpha1
  5. kind: Component
  6. metadata:
  7. name: state-store-a
  8. spec:
  9. type: state.service-a
  10. version: v1
  11. metadata: []
  1. #
  2. # This component uses the state store associated with socket `service-b`
  3. #
  4. apiVersion: dapr.io/v1alpha1
  5. kind: Component
  6. metadata:
  7. name: state-store-b
  8. spec:
  9. type: state.service-b
  10. version: v1
  11. metadata: []

Next steps