The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

kkelleher (19) [Avatar] Offline
#1
Hi John,

I attach the licensing service client for chapter 8.

I am confused that the client references zuul ... don't understand why ... but putting that aside for the moment ...

My coding attempt to reference by organization service via
"http://organizationservice/v1/organizations/{organizationId}",

, as per earlier chapter, didn't work.

Coding as
"http://xx.xx.xx.xx:8082/v1/organizations/{organizationId}"

worked but hard coding the url defeats the purpose.

Regards,
Kevin

P.S - Looking forward to reading the chapter you posted 05 Mar .. smilie

@Component
public class OrganizationRestTemplateClient {
    @Autowired
    OAuth2RestTemplate restTemplate;

    private static final Logger logger = LoggerFactory.getLogger(OrganizationRestTemplateClient.class);

    public Organization getOrganization(String organizationId){
        logger.debug("KK : In Licensing Service.getOrganization: {}", UserContext.getCorrelationId());

        ResponseEntity<Organization> restExchange =
                restTemplate.exchange(
                        //"http://zuulserver:5555/api/organization/v1/organizations/{organizationId}", // JC Code ...confused me
                		//"http://organizationservice/v1/organizations/{organizationId}",			   // KK code ... doesn't work .. why ??	
                		"http://xx.xx.xx.xx:8082/v1/organizations/{organizationId}",                    //Yea, works but hard code url defeats the purpose 
                        HttpMethod.GET,
                        null, Organization.class, organizationId);

        return restExchange.getBody();
    }
}


John C Carnell (44) [Avatar] Offline
#2
Hi kkelleher,

What you are seeing is just a hold over from Chapter 6 on Zuul. In the previous Chapter I run all my service calls through Zuul. Zuul handles all the service discovery so I just had hardcoded in the example the direct Zuul endpoint. I could have put the Zuul endpoint in my application config, but I did just to be explicit.

The call through Zuul should work fine? What issues were you running into with the direct call in Chapter 7. Did you remember to set the Auth http header because the service calls are now protected in Chapter 7 by an OAuth server.

If you can give me the specific direct URL you are trying to call I can double check it on my end.

Thanks again for posting. Feedback from the readers has helped me with the book.

Thanks,
John
kkelleher (19) [Avatar] Offline
#3
docker file
[ 2 KB ]
Hi John.

I'm trying hard to get a handler on this. As is , unfortunately, I have to change my RestTemplate client., depending on whether I am launching my microservices locally ( eclipse) or via docker.

    public Address getAddressFromEircode(String eircode){
    	
    	logger.info("About to call restTemplate exchange method");
        ResponseEntity<Address> restExchange =
                restTemplate.exchange(
                        //"http://zuulservice/api/address/v1/addresses/{eircode}", //[1] Works when all services are running locally ( i.e. launched from with eclipse) but not when docker launches services 
                		"http://addressservice:8085/v1/addresses/{eircode}",       //[2] Works when all services launched via docker.
                        HttpMethod.GET,
                        null, Address.class, eircode);

        return restExchange.getBody();
    }



I would have thought [1] above would have worked in both scenarios. [2] is just a complete hack.

IMHO it looks like the client (on docker) does not "reference" Eureka to see what "zuulservice" relates to.

I attach my docker-compose file

thanks in advance,
Kevin
John C Carnell (44) [Avatar] Offline
#4
Hi Kevin,

Do you have a github repo where I could pull down your code and take a look at it?

When you say your are running locally are, you running all of the services locally on your machine vs. when everything is running in Docker

Can you quick post what your Eureka server and Zuul server think are the registered routes.

On Eureka server can hit htttp://<<eurekaserver>:8761/eureka/apps
On Zuul server you can hit http://<<zuulserver>>:5555/routes

You are going to want to see that that the Zuul server route is good and that the Eureka server has the instance registered.

Don't feel bad. The OAUTH2/Zuul/Eureka combo has a lot of moving parts. Let me try and pull the code down locally tonight and see if I can see anything.

Thanks,
John
kkelleher (19) [Avatar] Offline
#5
eureka
[ 6 KB ]
zuul
[ 67 bytes ]
Hi John,

Thanks for your reply. Congrats on getting the book "out the door"!

Yes, when I say "running locally", I am running all of the services locally on my machine vs everything running on docker.

As requested, I post eureka and zuul server output.

I'll look into github'ing my project as requested.

Regards,
Kevin
John C Carnell (44) [Avatar] Offline
#6
Hi Kevin,

Thanks for sending me the code and I think I know whats going on. I am posting my response here.

Running all of the code locally
-----------------------------------------

1. When you run all the services locally including Zuul and Eureka. In chapter 8 I never register the Zuul server with Eureka.
(You probably could, I just havent done this. Zuul is going to be a long running service so I usually dont register it with
Eureka).

Therefore in the source code you need to point to the localhost or IP address of where
you are running your application. If you run the same code in Docker, Docker runs an internal DNS service that will resolve
to the container in question.

- You gotta a couple of choices for running locally.
1. Register the Zuul service with Eureka. Not sure, but it should work.
2. Reference the local IP address of the Zuul service in your code. Make sure you use the standard Spring RestTemplate
in this case so it does not try to look up the service against Eureka.
3. If you dont want to embedde the IP directly in the code you can always setup your local hosts file to hard map
zuulservice to your IP (as long as your IP does not change)


2. Your code with the address service works because the Address service will register itself with the Eureka service. The
RestTemplate will lookup the address service against Eureka so it will resolve to the proper IP address.

Hope that helps
sguillory6 (28) [Avatar] Offline
#7
Yes, solution 1, registering the Zuul service with Eureka works. I actually this this was an error in the book and went ahead and added it in.