Skip to main content

How to Installing and Deploying Eureka service registry

Installing Eureka

Standing up an instance of the Eureka service registry is easy if you have org.springframework.boot:spring-cloud-starter-eureka-server on your classpath.
package registry;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}
My nominal src/main/resources/application.yml looks like this these days.
server:
  port: ${PORT:8761}

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    server:
      waitTimeInMsWhenSyncEmpty: 0
The service’s port is defaulted to the well-known 8761 if Cloud Foundry’s VCAP_APPLICATION_PORT environment variable isn’t available. The rest of the configuration simply tells this instance to not register itself with the Eureka instance it finds, because that instance is.. itself. If you run it locally, you can point a browser to http://localhost:8761 and monitor the registry from there.

Deploying Eureka

Spring Cloud will startup a Eureka instance with its Spring Boot auto-configuration. There are a couple of things to consider when deploying Eureka. First, you should always use a highly-available configuration in production. The Spring Cloud Eureka sample shows how to deploy it in a highly-available configuration.

Clients need to know where to find the Eureka instance. If you have DNS then that might be one option, if you’re not polluting too large a global namespace. If you’re running in a Platform-as-a-Service and embracing 12-Factor app style applications then backing service credentials are configuration, and live external to the application, often exposed as environment variables. You can get the effect of having a Eureka service right now, though, by using Cloud Foundry’s cf CLI to create a user-provided service.
cf cups eureka-service -p '{"uri":"http://host-of-your-eureka-setup"}'
Point host-of-your-eureka-setup to a well-known host for your highly-available Eureka setup. I suspect we’ll soon see a way to create Eureka as a backing service in the same way you might a PostgreSQL or ElasticSearch instance on Pivotal Cloud Foundry.

Now that Eureka is up and running, let’s use it to connect some services to each other!

Speak for Yourself

Spring Cloud-based services have a spring.application.name property. It’s used to pull down configuration from the Configuration server, to identify the service to Eureka, and is referenceable in numerous other contexts when building Spring Cloud-based applications. This value typically lives in src/main/resources/bootstrap.(yml,properties), which is picked up earlier in the initialization than the normal src/main/resources/application.(yml,properties). A service with org.springframework.cloud:spring-cloud-starter-eureka on the classpath will be registered with the Eureka registry by its spring.application.name.
The src/main/resources/boostrap.yml file for each of my services looks like this, where my-service is the service name that changes from service to service:
spring:
  application:
    name: my-service
Spring Cloud uses the information in bootstrap.yml at service startup to discover the Eureka service registry and register the service and its spring.application.name, host, port, etc. You might wonder about that first bit. Spring Cloud attempts to look for it at a well-known address (http://127.0.0.1:), but you can change that. Here’s my src/main/resources/application.yml for a nominal Spring Cloud microservice, though there’s no reason this couldn’t live in the Spring Cloud configuration server. There may be many instances identifying themselves as my-service; Eureka will append the process’ information to a list of registrations for the same ID.
eureka:
  client:
    serviceUrl:
      defaultZone: ${vcap.services.eureka-service.credentials.uri:http://127.0.0.1:8761}/eureka/

---
spring:
  profiles: cloud
eureka:
  instance:
    hostname: ${APPLICATION_DOMAIN}
    nonSecurePort: 80
In this configuration, the Spring Cloud Eureka client knows to connect to the Eureka instance running on localhost if Cloud Foundry’s VCAP_SERVICES environment variable doesn’t exist or contain valid credentials.

The bit of configuration under the --- delimiter is for when the application is run under the cloud Spring profile. It’s easy to set a profile using the SPRING_PROFILES_ACTIVE environment variable. You can configure Cloud Foundry environment variables in your manifest.yml or, on Cloud Foundry Lattice, your Docker file.

The cloud profile specific configuration specifically tells the Eureka client how to register the service in the discovered Eureka registry. I do this because my services don’t use fixed DNS. APPLICATION_DOMAIN is an environment variable I set in my deploy scripts that tells a service what its externally referenceable URI is.

Comments

Post a Comment

Popular posts from this blog

Java Currency Formatter Solution

Given a  double-precision  number,  , denoting an amount of money, use the  NumberFormat  class'  getCurrencyInstance  method to convert   into the US, Indian, Chinese, and French currency formats. Then print the formatted values as follows: US: formattedPayment India: formattedPayment China: formattedPayment France: formattedPayment where   is   formatted according to the appropriate  Locale 's currency. Note:  India does not have a built-in Locale, so you must  construct one  where the language is  en  (i.e., English). Input Format A single double-precision number denoting  . Constraints Output Format On the first line, print  US: u  where   is   formatted for US currency.  On the second line, print  India: i  where   is   formatted for Indian currency.  On the third line...

Java Loops II print each element of our series as a single line of space-separated values.

We use the integers  ,  , and   to create the following series: You are given   queries in the form of  ,  , and  . For each query, print the series corresponding to the given  ,  , and   values as a single line of   space-separated integers. Input Format The first line contains an integer,  , denoting the number of queries.  Each line   of the   subsequent lines contains three space-separated integers describing the respective  ,  , and   values for that query. Constraints Output Format For each query, print the corresponding series on a new line. Each series must be printed in order as a single line of   space-separated integers. Sample Input 2 0 2 10 5 3 5 Sample Output 2 6 14 30 62 126 254 510 1022 2046 8 14 26 50 98 Explanation We have two queries: We use  ...

Java Static Initializer Block

Static initialization blocks are executed when the class is loaded, and you can initialize static variables in those blocks. It's time to test your knowledge of  Static initialization blocks . You can read about it  here. You are given a class  Solution  with a  main  method. Complete the given code so that it outputs the area of a parallelogram with breadth   and height  . You should read the variables from the standard input. If   or    , the output should be  "java.lang.Exception: Breadth and height must be positive"  without quotes. Input Format There are two lines of input. The first line contains  : the breadth of the parallelogram. The next line contains  : the height of the parallelogram. Constraints Output Format If both values are greater than zero, then the  main  method must output the area of the  parallelogram . Otherwise, pri...