In the last post, I showed how to create a Eureka server. Now we'll go through the process of creating microservice and register it on Eureka.

To begin, you can create a simple project with one entity, for example, a contact list. I will not spend time on the process of this project setup, just let's assume it's a simple one capable of create and retrieve objects from database. The repository has this app already (contacts folder) and you can clone.

First, add Eureka starter to your pom

<dependency>     
 <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

With the Eureka starter, we can annotate the main class with @EnableDiscoveryClient:

@SpringBootApplication
@EnableDiscoveryClient
public class ContactsApplication {
	public static void main(String[] args) {
SpringApplication.run(ContactsApplication.class, args);
	}
}

this annotation will make the app register itself on Eureka. Note that you can also use @EnableEurekaClient if you would like to explicit register on Eureka. @EnableDiscoveryClient can also make your app connect to Consul, for example

Now we need to tell our app how to connect to Eureka server. To do that, let's go to the application.yml and fill it

eureka:
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: http://eureka:admin@127.0.0.1:8761/eureka/
  instance: 
    metadataMap:
      instanceId: ${spring.application.name}:${random.int(999)}

Explaining the lines:

registryFetchIntervalSeconds: According to documentation

Eureka client maintain a cache of the registry information. This cache is refreshed every 30 seconds by default. So again, it may take another 30s before a client decides to refresh its local cache and discover newly registered instances.

defaultZone: the address of Eureka registry.

instanceId: the Id of the instance on the registry.

Now, start the Eureka server and then start the client. It takes some time to the registry to see the services. When you see something like this in the console:

2016-06-21 22:14:55.410  INFO 987 --- [pool-3-thread-1] com.netflix.discovery.DiscoveryClient    : The response status is 200
2016-06-21 22:14:55.477  INFO 987 --- [pool-2-thread-1] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONTACTS-SERVICE/lzk:contacts-service:1376166333 - Re-registering apps/CONTACTS-SERVICE
2016-06-21 22:14:55.477  INFO 987 --- [pool-2-thread-1] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONTACTS-SERVICE/lzk:contacts-service:1376166333: registering service...
2016-06-21 22:14:55.531  INFO 987 --- [pool-2-thread-1] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CONTACTS-SERVICE/lzk:contacts-service:1376166333 - registration status: 204
2016-06-21 22:15:00.411  INFO 987 --- [pool-3-thread-1] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2016-06-21 22:15:00.412  INFO 987 --- [pool-3-thread-1] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2016-06-21 22:15:00.412  INFO 987 --- [pool-3-thread-1] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2016-06-21 22:15:00.412  INFO 987 --- [pool-3-thread-1] com.netflix.discovery.DiscoveryClient    : Application is null : false
2016-06-21 22:15:00.412  INFO 987 --- [pool-3-thread-1] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2016-06-21 22:15:00.412  INFO 987 --- [pool-3-thread-1] com.netflix.discovery.DiscoveryClient    : Application version is -1: false
2016-06-21 22:15:00.418  INFO 987 --- [pool-3-thread-1] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server

your service is registered and if you access the Eureka dashboard, you can see the service and the number of instances:

Service

The next step will be shown in future posts, as creating and API gateway to load balance calls between instances and use Hystrix and Hystrix Dashboard to monitor our API calls

Netflix OSS: A beginner's guide [pt2]

Icons created by Freepik - Flaticon