非Lagom静态项目使用service-locator-dns无法解析,如何解决?
Great question! The issue you're hitting makes total sense—service-locator-dns is built to handle Lagom services out of the box, so it applies its name and SRV translation logic to all service lookups, including your statically defined non-Lagom services. Let's walk through practical, actionable solutions:
1. Custom Name Translator to Bypass Non-Lagom Services
The most flexible approach is to implement a custom ServiceNameTranslator that skips translation for your external/non-Lagom services. Here's how to set it up:
Step 1: Build the Custom Translator
Create a class that wraps the default translator, but checks if the target service is part of your Lagom ecosystem (use a naming convention like -service suffix, or a hardcoded whitelist of your Lagom services). For non-Lagom services, return the original name without any SRV-friendly transformation.
import com.lightbend.lagom.scaladsl.api.ServiceNameTranslator import scala.concurrent.Future class FilteredServiceNameTranslator(defaultTranslator: ServiceNameTranslator) extends ServiceNameTranslator { // Adjust this list to match your actual Lagom service names private val lagomServiceWhitelist = Set("user-service", "order-service", "inventory-service") override def translate(serviceName: String): Future[String] = { if (lagomServiceWhitelist.contains(serviceName)) { // Let the default translator handle Lagom services as intended defaultTranslator.translate(serviceName) } else { // Return the original name for non-Lagom services to skip SRV processing Future.successful(serviceName) } } }
Step 2: Register the Translator in Configuration
Update your application.conf to use this custom translator instead of the default:
lagom.service-locator.dns.name-translator = "com.yourcompany.FilteredServiceNameTranslator"
This ensures only your Lagom services go through the SRV translation pipeline, while non-Lagom services are resolved using standard DNS A/AAAA records (exactly what you need for statically defined external services).
2. Service-Specific Locator Configuration
If you only have a small number of non-Lagom services, you can bypass service-locator-dns entirely for those by defining a direct, static service locator per service. Add this to your application.conf:
lagom.service-locator.service-specific { # Replace "external-payment-api" with your non-Lagom service name external-payment-api { implementation = "com.lightbend.lagom.scaladsl.client.SimpleServiceLocator" simple-service-locator.uri = "https://api.payment-provider.com" } }
This tells Lagom to use a fixed URI for that specific service, skipping all DNS/SRV logic entirely. It's a quick, code-free fix for isolated external services.
3. Track Your GitHub Issue
Since you've already opened an issue with the service-locator-dns maintainers, keep an eye on that thread—they might add built-in support for excluding services (like a blacklist config) in a future release. For now, though, the custom translator or service-specific configurations are reliable, immediate workarounds.
Final Recommendation
If you have multiple non-Lagom services, go with the custom ServiceNameTranslator—it keeps your configuration clean and scales well. For just one or two external services, the service-specific locator is faster to implement.
内容的提问来源于stack exchange,提问作者Chandan Pasunoori

