Home > Software engineering >  Spring DiscoveryClient in local environment
Spring DiscoveryClient in local environment

Time:12-23

Assuming I have two services: customerService and orderService. Both are Springboot applications contained in its own dockerfile.

In production they should be managed using Kubernetes. So there could be mutliple instances of each service. As the services should be able to call each other via REST I want to use ServiceDiscovery to get the destination of a service instance of the respective other type (e.g. customerService wants to get host, port, etc. of a running orderService instance).

I want to achieve this by using an injected DiscoveryClient in my Spring Boot services.

I understand how this works in production as there is the Kubernetes cluster the DiscoveryClient communicates with.

But how does this work in a local environment where there is no Kubernetes but only Docker running?

CodePudding user response:

I think you should be looking into Kubernetes services instead of using an injected Discovery client.

For Service discovery and load-balancing, you can use Services in Kubernetes. From Kubernetes documentation:

An abstract way to expose an application running on a set of Pods as a network service. With Kubernetes, you don't need to modify your application to use an unfamiliar service discovery mechanism. Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods and can load-balance across them.

By this, you can also avoid the overhead of maintaining discovery server instances and their availability.

How does this work in a local environment where there is no Kubernetes but only Docker running?

You can use spring profiles for picking this URL based on your environment.

For example, you'll have two spring configuration files (application-dev, application-prod) and in the dev file the URL for the second application would be localhost relative and in the prod file, the URL would be the DNS which you'll get as a part of Kubernetes service setup. So use the profile dev while running in local and prod while running in production(or an appropriate profile for your Kubernetes environment).

  • Related