Hello, I'm trying to create some "Initializer application" which: 1. Waits for first heartbeat from eureka server 2. Calls other services registered in eureka with @LoadBalanced OAuth2RestOperations

FirstDiscoveryHeartbeatInitializer is responsible for doing operations mentioned above.

Problem is that when I call HelloCaller from FirstDiscoveryHeartbeatInitializer I get exception Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://uaa-service/uaa/oauth/token": uaa-service; nested exception is java.net.UnknownHostException: uaa-service

Before that call I'm just for test calling discoveryClient. getServices() to get available services and uaa-serivce is there - so I don't know why the resttemplate isn't aware of this url.

Much more interesting is fact that when I call this commands before first heartbeat:

curl -X POST -u demo:demo localhost:7999/uaa/oauth/token -d grant_type=client_credentials
curl  -u demo:demo localhost:8083/hello -H 'Authorization: Bearer <token from prev step>'

I get the correct response and also after that initializer performs correctly. This steps are again calling HelloCaller but this time from HelloCallerController

Do I need configure something or is there any issue within my configuration?

I've also asked this question on oauth2 gitter, mr @dsyer pointed me here.

If you need more informations/... let me know

Thanks

Comment From: ryanjbaxter

Why not use RestTemplate?

You would want to add the @LoadBalanced annotation to your RestTemplate.

@LoadBalanced
private RestTemplate restTemplate;

Comment From: bilak

@ryanjbaxter I maybe don't follow your intention. Where should I use that RestTemplate? OAuth2RestOperations is still RestTemplate, isn't it?

Comment From: ryanjbaxter

I was thinking here https://github.com/bilak/gh-ribbon-eureka-oauth2-config/blob/master/initializer-service/src/main/java/com/github/bilak/cloud/caller/HelloCaller.java#L20

I could be missing something....

Comment From: bilak

If I understand it correctly, the @LoadBalanced could be used on bean definition. Then if I inject this bean to some other class and provide it's @Qualifier then it should be loadbalanced also. The problem here is more related to the fact, that this template is not working as loadbalanced when I invoke it from "backend" thread but it's working when I invoke it from within request context. I'd like to use more OAuth2RestOperations with different configurations which will be loadbalanced also.

Comment From: dsyer

The problem is that the access token URI is accessed by a non-load-balanced rest template. I thought you understood that from your comments in gitter. Anyway, I think when you make the curl call directly, you populate the OAuth2ClientContext so you no longer need the client to obtain its own access token.

When you create an OAuth2RestTemplate it is initialized with a default AccessTokenProvider. You would have to provide a custom instance that used a load balanced template to obtain the access token.

Comment From: bilak

@dsyer I know that when I execute the call from "initializer" there is no loadbalanced resttemplate. I didn't understand why it's working from "rest" and not from "backend". Now I'll try to extend ClientCredentialsAccessTokenProvider and provide LoadBalanced OAuth2RestTemplate to see how it works. Yesterday I ended up with stackoverflow.

I will post my result here.

Thanks

Comment From: bilak

fixed. The problem which I had yesterday was because I was using another OAuth2RestOperations for AccessTokenProvider.

Can we have some mention about this configuration in documentation?

Comment From: dsyer

I'm not sure yet what you did, or why it was difficult. Can you explain a bit? Update the sample?

Comment From: bilak

@dsyer as you can see in commit, 1. I've created @LoadBalanced RestTemplate 2. I've extended the ClientCredentialsAccessTokenProvider and overriden the method getRestTemplate while providing one which is loadbalanced (this maybe need some improovement) 3. configured my OAut2RestOperations to use my custom AccessTokenProvider

There could be maybe better way how to inject the template in there, but that need maybe new method in AccessTokenProvider something what will inject for example RestTemplateCustomizer

It wasn't difficult, but as I wrote, I didn't understood why it works from rest and not from backend. I thought that there is some configuration which I am missing. And as you pointed there was missing OAuth2ClientContext which need's to be requested.

Comment From: OlgaMaciaszek

This module has entered maintenance mode. This means that the Spring Cloud team will no longer be adding new features to the module. We will fix blocker bugs and security issues, and we will also consider and review small pull requests from the community.