
Lesson Description
The "Building a Microservice Client App" Lesson is part of the full, Enterprise Java with Spring Boot course featured in this preview video. Here's what you'd learn in this lesson:
Josh builds a REST client to consume the services. The client will utilize the load-balanced builder. This means the load balancing will occur in the client as it's accessing the downstream services.
Transcript from the "Building a Microservice Client App" Lesson
[00:00:00]
>> Josh Long: Okay, so let's actually build a client and kind of see what I'm talking about here. So I've got two services, both of which are discoverable via the service registry and both of which are drawing their config from the config server. Let's go back and build a client.
[00:00:13]
Okay, Start that spring IO, build a client. We're gonna use Spring Cloud Gateway, which is a micro proxy. We're going to use the web support, we're going to use the config client and we use the discovery client support for talking to the service registry. Okay, here we go.
[00:00:29]
And then we go back to the downloads directory. Uao client.zip, okay, application properties, ports, sorry, Config server. Config is there. And what we're going to do, the first thing we're going to do is going to create a REST client that is load balanced automatically, right? Using the Spring cloud discovery client.
[00:00:54]
So I'll say REST client builders. Notice that I'm not defining the REST client, I'm defining the builder, which is the prototype for the REST client. Then once I've done that, I can inject that REST client. I can create a REST client that uses that load balanced builder like so.
[00:01:10]
So add controller, add response body, load balanced controller. Okay, hello, string hello. So I'm going to say return, I'm going to use the REST client. Add that to the constructor, add the builder build. I'll say this REST client.get.uri, and notice that I'm doing a http service. A logical name.
[00:01:44]
This is not the host name or DNS. This is the logical name as it's registered in the service registry. Where's my service registry? I've got too many things going on here. Close that. The logical name is called service. So I use that in the Java code here when I make the call, hello.retrieve.string.class, okay, sorry, retreat.
[00:02:13]
This one. Body. Okay, so let's try that out. That should be on port. I don't know what port this will run on. 9090 I think. Okay, 8081, great. So we go to localhost 8081. Hello. See what's happening there. I'm making the request from my client. It is using a REST client.
[00:02:46]
It's load balanced, it's talking to the downstream services. But first it's going to the service registry and load balancing across the two of them. See that? That's all happening in the client in the Java code that's doing the call. You can configure the algorithm that you use for that.
[00:03:04]
This is an abstraction. It's called load balancer, right? You can plug in your own implementation, but we have other implementations.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops