如何配置JHipster微服务向注册中心注册自定义域名
JHipster relies on Spring Cloud Netflix Eureka (via the JHipster Registry) for service discovery. To get UAA and Microservice1 to register with your custom domains instead of IP addresses, follow these step-by-step instructions:
Step 1: Set Up DNS Records First
Before modifying any service configurations, ensure your production DNS has A records pointing:
uaa.myapp.com→ IP address of the server running UAAmicroservice1.myapp.com→ IP address of the server running Microservice1
Verify all services (Registry, Gateway, UAA, Microservice1) can resolve these hostnames using ping uaa.myapp.com and ping microservice1.myapp.com from each server.
Step 2: Configure UAA for Custom Domain Registration
Open the UAA service's src/main/resources/config/application-prod.yml file and update the Eureka instance settings:
eureka: instance: # Set UAA's custom hostname hostname: uaa.myapp.com # Disable IP address preference to use the hostname instead prefer-ip-address: false # Optional: Create a clear instance ID for easier Registry identification instance-id: ${eureka.instance.hostname}:${server.port} client: service-url: # Point to your JHipster Registry's domain (replace with your actual credentials/domain) defaultZone: http://your-registry-user:your-registry-pass@jhipster-registry.myapp.com:8761/eureka/ # Optional: Add HTTPS config if using SSL in production server: port: 443 ssl: enabled: true key-store-type: PKCS12 key-store: classpath:keystore/uaa.myapp.com.p12 key-store-password: your-keystore-pass key-alias: uaa.myapp.com # Update UAA's issuer URI to use the custom domain (critical for OAuth2 token validation) spring: security: oauth2: authorizationserver: issuer: https://uaa.myapp.com/uaa
Step 3: Configure Microservice1 for Custom Domain Registration
Repeat the same logic for Microservice1 in its src/main/resources/config/application-prod.yml:
eureka: instance: hostname: microservice1.myapp.com prefer-ip-address: false instance-id: ${eureka.instance.hostname}:${server.port} client: service-url: defaultZone: http://your-registry-user:your-registry-pass@jhipster-registry.myapp.com:8761/eureka/ # Optional: Add HTTPS config if using SSL server: port: 443 ssl: enabled: true key-store-type: PKCS12 key-store: classpath:keystore/microservice1.myapp.com.p12 key-store-password: your-keystore-pass key-alias: microservice1.myapp.com
Step 4: Update Gateway Configuration (Critical for Inter-Service Communication)
To ensure the Gateway can communicate with UAA using its custom domain, update the Gateway's application-prod.yml:
jhipster: security: oauth2: client: provider: oidc: # Point to UAA's custom domain for token validation issuer-uri: https://uaa.myapp.com/uaa
Step 5: Start Services with the Production Profile
Launch each service with the production profile activated to apply your config changes:
- UAA:
java -jar uaa-<your-version>.jar --spring.profiles.active=prod - Microservice1:
java -jar microservice1-<your-version>.jar --spring.profiles.active=prod
Step 6: Verify Registration
Access your JHipster Registry UI (e.g., https://jhipster-registry.myapp.com:8761) and navigate to the Instances page. You should see:
- UAA registered with hostname
uaa.myapp.com - Microservice1 registered with hostname
microservice1.myapp.com
Additional Notes
- Firewall Rules: Ensure ports 8761 (Registry), your service ports (e.g., 8080 or 443), and OAuth2 ports are open between all services.
- Registry Security: Replace the default
admin/admincredentials in the Registry's config with strong production credentials. - Certificate Management: If using HTTPS, ensure all SSL certificates are valid and trusted across your infrastructure.
内容的提问来源于stack exchange,提问作者Colas Pomiès

