Installing Eureka
Standing up an instance of the Eureka service registry is easy if you haveorg.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 aspring.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.
Thanks for your interesting ideas.the information's in this blog is very much useful for me to improve my knowledge.
ReplyDeleteiOS Training in Chennai
ios training institute in chennai
JAVA Training in Chennai
Python Training in Chennai
Big data training in chennai
Selenium Training in Chennai
IOS Training in Chennai
iOS Training in T Nagar
Thank you so much for providing information on this. It was very useful.
ReplyDeleteAviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
aviation training in chennai
air hostess institute in chennai
Airport Management Training in Chennai
airport ground staff training in chennai
Thanks for delivering a good stuff, Explanation is good, Nice Article.
ReplyDeleteMicroservices Online Training
Microservices Training in Hyderabad