Viraj
Viraj

Reputation: 5381

How to communicate between two services in k8s using spring cloud

I have spring boot app which I use spring-cloud-kubernetes dependencies. This is deployed in K8s. I have implemented service discovery and I have @DiscoveryClient which gives me service ids k8s namespace.My Problem is I want to do a rest call to one of this found services ( which have multiple pods running). How to do this ? Do I have to use Ribbon Client ?

My Code is

@RestController
public class HelloController {

@Autowired
private DiscoveryClient discoveryClient;


@RequestMapping("/services")
public List<String> services() {
    log.info("/services - Request Received " + new Date());
    List<String> services = this.discoveryClient.getServices();
    log.info("Found services " + services.toString());
    for (String service : services) {
        // TODO call to this service
        List<ServiceInstance> instances = discoveryClient.getInstances(service);
        for (ServiceInstance instance : instances) {
            log.info("Service ID >> " + service + " : Instance >> " + getStringVal(instance));

        }
    }

    return services;
}

In service instances I can find host and port to call, but I want to call to service so that some load balancing mechanism calls to actual pod instance.

Upvotes: 0

Views: 333

Answers (1)

Arghya Sadhu
Arghya Sadhu

Reputation: 44569

You need to use Spring Cloud Kubernetes Ribbon to call the host and port which will go to the kubernetes service and you get the load balancing provided by Kubernetes service and Kube Proxy.

Upvotes: 2

Related Questions