DEV Community

Alone Star
Alone Star

Posted on

LightESB MysqlRouteSrv v1.0.0: External Database Support with externaldb

TL;DR

  • MysqlRouteSrv@v1.0.0 enables externaldb through system.components=externaldb.
  • Service-level extdb.* config is loaded from service.config.properties and can override common defaults.
  • Dynamic datasource beans are registered as extdb-<id>-datasource and consumed by Camel SQL endpoints.
  • The built-in timer route verifies connectivity through select/insert/query/delete against testexdb.

Metadata

  • Applicable Version: MysqlRouteSrv@v1.0.0
  • Related Service: MysqlRouteSrv@v1.0.0@mysql-healthcheck-route.xml

Background and Goal

When teams deploy multiple services, database endpoints and credentials often vary by environment and by route. Hardcoding datasource names in XML routes increases change cost and risk.

externaldb addresses this by moving database target selection to service config:

  • Keep route SQL logic stable.
  • Register datasources dynamically in Camel Registry.
  • Switch route target datasource by properties instead of route rewrites.

For MysqlRouteSrv v1.0.0, this mechanism is used to validate MySQL connectivity and basic CRUD flow on a fixed schedule.

Route and Configuration Breakdown

1) Component enablement and config priority

From lightesb-camel-app/MysqlRouteSrv/v1.0.0/common.config.properties:

system.components=externaldb
Enter fullscreen mode Exit fullscreen mode

From lightesb-camel-app/MysqlRouteSrv/v1.0.0/service.config.properties:

extdb.enabled=true
extdb.default=primary
extdb.ids=primary
extdb.primary.type=mysql
extdb.primary.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
extdb.primary.driver=com.mysql.cj.jdbc.Driver
extdb.primary.username=root
extdb.primary.password=nghxni
extdb.primary.maxPoolSize=10
mysqlroute.target.datasource=primary
Enter fullscreen mode Exit fullscreen mode

Configuration model:

  • common.config.properties: common defaults
  • service.config.properties: service-level override
  • Effective priority: service-level settings override common settings

2) Dynamic Bean names in Camel Registry

After externaldb is loaded, the following beans are available:

  • extdb-<id>-datasource (for example extdb-primary-datasource)
  • extdb-default-datasource
  • extdb.config
  • extdb.route.targets

This naming contract allows route-level datasource switching with placeholders.

3) Timer route wiring with SQL component

From mysql-healthcheck-route.xml:

<from uri="timer://mysql-healthcheck?fixedRate=true&amp;period=60000"/>
<setHeader name="db.target">
    <simple>{{mysqlroute.target.datasource}}</simple>
</setHeader>
<to uri="sql:select 1 as db_ok?dataSource=#bean:extdb-{{mysqlroute.target.datasource}}-datasource&amp;outputType=SelectOne"/>
Enter fullscreen mode Exit fullscreen mode

The same datasource reference pattern is reused for:

  • insert into testexdb (...)
  • select ... from testexdb where ID=:#ID
  • delete from testexdb where ID=:#ID

This keeps SQL routes stable while allowing datasource redirection in config.

Request and Response Examples

MysqlRouteSrv v1.0.0 is a timer-driven route (HTTP.Listener=false), so runtime validation is based on startup and route logs.

1) Start the service process

..\start.bat
Enter fullscreen mode Exit fullscreen mode

Run ..\start.bat from the lightesb-camel directory to start the service, then continue with the log checks below.

2) Verify route execution logs

Expected output pattern (based on logs/mysql-healthcheck-route.log):

  • health check starts with target=primary
  • select 1 returns result=1
  • insert/query/delete against testexdb complete successfully

3) Verify datasource target switching

Change:

mysqlroute.target.datasource=primary
Enter fullscreen mode Exit fullscreen mode

to:

mysqlroute.target.datasource=archive
Enter fullscreen mode Exit fullscreen mode

Then reload and check logs again. The target=... field should follow the new datasource id when extdb.archive.* is configured.

Common Issues and Troubleshooting

1) No bean could be found in the registry

Check:

  • system.components includes externaldb
  • extdb.enabled=true
  • target id exists in extdb.ids
  • SQL URI name matches extdb-{{xxx.target.datasource}}-datasource

2) Health check runs but SQL fails

Check:

  • MySQL URL/driver/user/password are valid
  • target table testexdb exists in the selected schema
  • account has insert/select/delete permissions

3) Datasource switch does not take effect

Check:

  • the new id is fully configured as extdb.<id>.*
  • service reload has completed
  • route header db.target prints the expected id in logs

4) Unexpected default datasource behavior

Check:

  • extdb.default value
  • mysqlroute.target.datasource route-level override

If route-level override is missing, default id may be used.

Summary

MysqlRouteSrv v1.0.0 provides a practical baseline for External Database support in LightESB:

  • component-level enablement (externaldb)
  • service-level datasource model (extdb.*)
  • dynamic Camel Registry binding (extdb-<id>-datasource)
  • stable SQL routes with config-driven target selection

This pattern is a good fit for teams that need one route implementation but multiple environment-specific database targets.

Links

Top comments (0)