Service Discovery with Eureka server
Service Discovery?
What is service discovery? Think of it as a way to quickly find out who someone is and where they live. It’s kind of like a phone book - except people register themselves and who they are. And people check on them constantly. And state if they are healthy or not. And kick them out if they are not. And all of these phone books talk to one another. And it all happens every couple of seconds.
The basics are simple: I’ve got a ton of microservices that do X, Y and Z. X needs to talk to Y and Y needs to talk to Z. Now I can manually configure each service to talk to the address of X, Y and Z but it’s a huge hassle. It’s also a nightmare once you get into have A to Z services running and B talk to C-F and G talks to A-D. Then, we scale out and ever service has 1-6 different instances running which equates to 26*(3.5) give or take a few instances.
Services on startup will register themselves with a centralized service registry. These registries can federate with each other allowing themselves to share the information they have with one another. When one of our services spins up it says that I am service A and o exist at 127.0.0.1:56000. Service B follows up and registers at 56001 and so on and so forth to service Z at 56025. You can go ahead and replace 127.0.0.1 with all of the various IPs/domain name entries that exist within your individual cloud provider (myservice-Z.azurewebsites.net anyone?). I now get a service registration in Eureka that states Z exists at 127.0.0.1:56025. Now when A needs to talk to Z I can simply fetch the lookup for Z in Eureka and I will get back 127.0.0.1:56025. I then attach on the rest of my URL and off we go!
Eureka Server by Netflix
Eureka isn’t just an awesome TV show - it’s also a part of the Netflix OSS and part of Springboot. For those in that exist in the dotnet realm, we also have Steeltoe that allows us to easily integrate in with this software suite.
Eureka was originally developed by Netflix to register the microservices that were being ran within AWS. It is basically responsible for an entire generation to be able to Netflix and chill - or at least identify where to get your account info and where to start pulling your movie stream from. You can view all of the features that exist for this over on the Netflix OSS eureka page.
Spring Boot and Eureka
Spring boot gives us direct adn easy access to the Netflix OSS suite of tools. Basically we have the options within the Spring boot initilizer that we can find within the dependencies to create out own Eureka Server. We can also use these dependencies to set up our Spring Boot services to enable the Eureka Client in order to automatically register themselves with the Eureka Server we set up.
In the dependencies simply search for Eureka:
- Eureka Discovery Client
- Eureka Server
- Service Registry(PCF)
So what are these 3 items? The Discovery client is what you would use within your own services to allow them to self-register and find other services that exist within your microservices environment. The Eureka Server will set up a simple application that will provide you a basic Eureka Server code base to run. So let’s do that real quick. You can see that it will add a spring-cloud-netflix Eureka Server dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Now this only adds in the dependencies for the Eureka Server. Let’s get into the actual code required to enable the Eureka Server to run.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
So we have added 2 lines of code to the default Spring boot application. We added @EnableEurekaServer and we added the import statement to pull in the package for the Eureka Server. The rest of the details come down to the configuration we need to do in our bootstrap.yml and our application.yml files. First our bootstrap.yml:
spring:
application:
name: eureka
cloud:
config:
uri: ${CONFIG_SERVER_URL:http://localhost:8888}
Here we are simply setting up the application name and the centralized configuration server that we would pull configurations from. The configuration server also default to our localhost if no config server is configured. In our application.yml file we get more into the setup of Eureka:
server:
port: 8167
eureka:
environment: ${REGION_NAME}
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
The server:port configuration sets up the listening port for our Eureka Server. Everything under Eureka sets up the configurations for our Eureka server. We set up and the environment which we are executing in. We also go and set up the client within the server to not register itself with Eureka and not fetch the registry. These settings will be more used for the clients that sync up with Eureka rather than the Eureka Server itself.
We can see a lot of the configurations available under the Netflix OSS page on configuring Eureka. Once you have everything set up and running all you will need to do is run the following and navigate to localhost:8167.
.\mvnw springboot:run