3

I'm new to Jhipster, and wondering if it's possible to manually call a microservices from the gateway code using a RestTemplate or something else.

My first idea was to call the gateway itself... but I'm not sure it's a good idea. My second idea was to try and call the service by it's URL. My concern is that I don't want to hardcode the port of a given node. Instead, i want to use proper loadbalancing.

I've read this article https://dzone.com/articles/spring-cloud-rest-client-with-netflix-ribbon-basic, but the injection failed.

I've read somewhere else that you now need to manually add the bean declaration

@LoadBalanced
@Bean
RestTemplate restTemplate(){
    return new RestTemplate();
}

But now I'm struggling with the actual URI : what I am supposed to put as the root? (xxxxx)

final HcpVersionedhcp hcpVersionedhcp = 
            restTemplate.exchange("http://xxxxx/api/user-data/byLogin/", UserData.class);

The only configuration I have in my gateway application.yml is

ribbon:
eureka:
    enabled: true
ALansmanne
  • 271
  • 1
  • 6
  • 17

1 Answers1

6

The "xxxxx" has to be replaced with your services name. If your service is "foo", you should write http://foo/api/user....

If you are using JWT as authentication, you need to auth using a user a in JHipster, or to pass the JWT token from request when possible. However the is no best practice for JWT auth, so I would suggest to go the JHipster UAA way. In a few words, you have one more service responsible for authentication and authorization. To access your service from another service, you can use @AuthorizedFeignClient on interfaces, similar to JPA.

So you define:

@AuthorizedFeignClient(name = "xxxx")
interface XxxClient {

   @RequestMapping(value = "/api/some-entities/{id}")
   SomeEntity getSomeEntityById(Long @Path("id") id);
}

And inject it in any spring service / rest-controller like this:

@Inject
private XxxClient xxxClient;

//...

public void someAction() {
   //...
   xxxClient.getEntityById(id);
   //..
}

Which internally implement client authorization flows ...

Patrick
  • 1,717
  • 7
  • 21
  • 28
David Steiman
  • 3,055
  • 2
  • 16
  • 22