能否将完整URI映射至不同服务?——基于AKS的GraphQL微服务路径式URI配置问询
This setup is totally achievable—you don’t need subdomains or a monolithic codebase to make it work. Here’s a step-by-step breakdown of how to implement path-based routing for your federated GraphQL gateway and microservices on Azure Kubernetes Service:
1. Configure Your Federated Gateway to Use Internal Cluster Traffic
First, skip routing external traffic between your gateway and microservices—it’s inefficient and adds unnecessary latency. Instead, have the gateway communicate directly with your microservices via their internal Kubernetes Service names within the cluster.
For example, if you’re using Apollo Gateway, your setup might look like this:
const { ApolloServer } = require('@apollo/server'); const { startStandaloneServer } = require('@apollo/server/standalone'); const { ApolloGateway, IntrospectAndCompose } = require('@apollo/gateway'); const gateway = new ApolloGateway({ supergraphSdl: new IntrospectAndCompose({ // Point to internal Kubernetes Service URLs, not external paths subgraphs: [ { name: 'customer', url: 'http://customer-service.default.svc.cluster.local/graphql' }, { name: 'order', url: 'http://order-service.default.svc.cluster.local/graphql' }, ], }), }); const server = new ApolloServer({ gateway }); const { url } = await startStandaloneServer(server, { listen: { port: 4000 }, });
This keeps internal traffic within the cluster—faster, more secure, and avoids hitting public endpoints unnecessarily.
2. Set Up Path-Based Routing with AKS Ingress
Use an NGINX Ingress controller (the standard choice for AKS) to route external traffic as you want:
- Root path (
/) → Federated gateway /customer→ Customer microservice’s GraphQL endpoint/order→ Order microservice’s GraphQL endpoint
Here’s an example Ingress YAML configuration:
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: graphql-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: /graphql nginx.ingress.kubernetes.io/ssl-redirect: "true" spec: tls: - hosts: - graph.xyzcorp.com secretName: graph-tls-secret # Your TLS certificate secret rules: - host: graph.xyzcorp.com http: paths: - path: / pathType: Prefix backend: service: name: gateway-service # Your gateway's Kubernetes Service port: number: 4000 - path: /customer pathType: Prefix backend: service: name: customer-service # Your customer microservice's Service port: number: 4001 - path: /order pathType: Prefix backend: service: name: order-service # Your order microservice's Service port: number: 4002
- The
rewrite-target: /graphqlannotation ensures that when a user hitshttps://graph.xyzcorp.com/customer, the request is rewritten to the microservice’s default/graphqlendpoint (adjust this if your microservices listen on a different path). - Don’t forget to install the NGINX Ingress controller on your AKS cluster first—you can do this via Azure CLI or Helm.
3. Tweak Microservice Endpoints (If Needed)
If your microservices are configured to listen on /graphql (the common default), the rewrite rule above will handle mapping external paths to internal endpoints. If you want microservices to listen directly on the root path (so /customer maps to /), just remove the rewrite-target annotation for those paths.
Example of a microservice listening on root:
const server = new ApolloServer({ typeDefs, resolvers }); const { url } = await startStandaloneServer(server, { listen: { port: 4001, path: '/' }, });
4. Production-Focused Tips
- CORS Configuration: If clients will call microservice paths directly (not just through the gateway), configure CORS on each microservice to allow
https://graph.xyzcorp.comas an origin. - Centralized Auth: Implement authentication at the gateway level (e.g., OAuth2 or API keys) and pass valid tokens to microservices via request headers—keeps auth logic unified.
- Monitoring: Use AKS’s built-in Azure Monitor or open-source tools like Prometheus/Grafana to track traffic to each path and microservice performance.
- Rate Limiting: Apply rate limits at the Ingress or gateway level to protect your microservices from excessive traffic.
This approach keeps your microservices independent, avoids subdomain sprawl, and gives you a clean, unified domain for all your GraphQL traffic.
内容的提问来源于stack exchange,提问作者michael

