How to I do a RESTFUL API via kubernetes service name

5/8/2020

I have this method to get userinfo. I know that if I use a valid url (i.e. http://url.to.access)

my getuserinfo() is working.

Is there a way for me to use the kubernetes service name to call a GET request with my speing boot application? (i.e. <pod-name>.<namespace>.svc)

userInfoUrl comes from property.yaml:

  @Value("${auth.userinfoUrl}")
  private String userInfoUrl;

Method to get userinfo:

 public ResponseEntity<String> getUserInfo(String accessToken) {

    if (!StringUtils.hasText(accessToken)) {
      throw new InvalidAccessTokenException("Invalid accessToken.");
    }

    // Set authorization header with access token
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Bearer " + accessToken);

    HttpEntity<String> entity = new HttpEntity<>(headers);

    // Does url param tagging based on param given.
    UriComponentsBuilder builder =
        UriComponentsBuilder.fromHttpUrl(userInfoUrl);

    // Call auth server to get user info.
    return restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
  }
-- shadow
java
kubernetes
spring-boot

1 Answer

5/8/2020

Create a ClusterIP type service in-front of the pods responsible for userinfo application. Then you can either use servicename.namespacename.svc.cluster.local from any namespace or just servicename from same namespace to access it in the spring boot application.

-- Arghya Sadhu
Source: StackOverflow