Eureka server
It is really easy to use Eureka with Spring Boot. The first thing you need to start is obviously the server:
- Go to https://start.spring.io/, add Eureka Server to the list of dependencies and generate the project.
- Import the project in your IDE
- Open EurekaApplication.java and add @EnableEurekaServer. It should look like this:
| @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } } |
- Add the following lines to application.yml to configure the server: (I prefer YAML, but it is also possible to use application.properties which is provided by the project from the Spring Initializer)
| server: port: 8761 spring: application: name: eureka eureka: client: registerWithEureka: false fetchRegistry: false |
The properties registerWithEureka: false and fetchRegistry: false prevent that the application acts as a Eureka client and server at the same time.
Eureka client
The next step is to create an application that registers with the service registry. Go to https://start.spring.io/ and create another application. This time add Eureka Discovery as a dependency:
After importing the project in your IDE, add @EnableEurekaClient in the Main class:
| @SpringBootApplication @EnableEurekaClient public class TestAppApplication { public static void main(String[] args) { SpringApplication.run(TestAppApplication.class, args); } } |
In application.yml only the application name and the default zone need to be set. The name will be used to register and the defaultZone is the URL of the Eureka server. For a local setup this will be http://127.0.0.1:8761/eureka/.
| spring: application: name: testApp eureka: client: serviceUrl: defaultZone: http://127.0.0.1:8761/eureka/ |
If you start both applications, the logs show that the testApp registers its instance:
Log output of testApp:
| DiscoveryClient_TESTAPP/172.16.177.147:testApp: registering service... DiscoveryClient_TESTAPP/172.16.177.147:testApp - registration status: 204 |
Log output of Eureka :
| Registered instance TESTAPP/172.16.177.147:testApp with status UP |
Eureka comes with a built-in web UI where it shows all registered services. Go to http://127.0.0.1:8761 to see the registered instance of testApp:
Now that the local setup is running, let’s take it to the cloud!
Cloud setup
The simplest way to deploy an application to Cloud Foundry is to upload the Jar file directly in Cloud Foundry’s web frontend. But this has some disadvantages. The configuration must be done manually and probably the biggest disadvantage: It is not automated!
| --- applications: - name: eureka memory: 512MB host: eureka-test-service path: target/eureka-0.0.1-SNAPSHOT.jar |
- First build the jar: mvn clean install
- Then push it to Cloud Foundry: cf push
Congratulations, you have deployed your Eureka server to Cloud Foundry! To verify this, go to http://eureka-test-service.cfapps.io and have a look at the web UI of Eureka.
Adding clients
A service registry without clients is pointless. Therefore the next step is to configure our testApp to use Eureka in the cloud. The easiest way would be to set the default zone to http://eureka-test-service.cfapps.io/eureka/, but this would bind the application to this single instance. If the URL changes you have to recompile the application. Instead of storing environment specific configuration in the application, the environment should provide the URL (see http://12factor.net/config). An easy way to provide the URL via an environment variable is a user provided service.
User provided service
| cf cups eureka -p '{"url": "http://eureka-test-service.cfapps.io/eureka/"}' |
To enable an application to use a service in Cloud Foundry you have to bind the service to the application.
Cloud profile
If the service is bound to our application it will provide the Eureka URL in the environment variable vcap.services.eureka.credentials.url. To use this in the testApp add a cloud specific spring profile to application.yml and set the defaultZone:
| --- spring: profiles: cloud eureka: instance: nonSecurePort: 80 hostname: ${vcap.application.uris[0]} client: service-url: defaultZone: ${vcap.services.eureka.credentials.url} |
The hostname is set to vcap.application.uris[0]. This variable is provided by Cloud Foundry and contains the public URL of an application. This URL will be used to register with Eureka. Other services can then retrieve this URL from Eureka to connect to testApp.
Manifest
Just as the server project has a manifest.yml, so the testApp will have one:
- The active spring profile is selected with the environment variable spring.profiles.active: cloud which can be set in the env attribute.
- The service eureka is bound to this application in the services block.
The complete manifest.yml should look like this:
| --- applications: - name: testApp memory: 512MB host: testApp-service path: target/testApp-0.0.1-SNAPSHOT.jar env: spring.profiles.active: cloud services: - eureka |
Deploy it to Cloud Foundry
- As usual first build the jar: mvn clean install
- Now push it to Cloud Foundry via CLI: cf push
The logs show the registration of the Eureka client with the Eureka server.
| DiscoveryClient_TESTAPP/6b42392c-a1a5-473d-7513-8ff9e53cfc41: registering service... DiscoveryClient_TESTAPP/6b42392c-a1a5-473d-7513-8ff9e53cfc41 - registration status: 204 |
On the UI http://eureka-test-service.cfapps.io you can see that TESTAPP is registered.
That’s it! Within a few simple steps you have created a service registry with Spring Boot. First a Eureka server and client for a local setup, then configured both to be cloud ready and deployed them to Cloud Foundry.
No comments:
Post a Comment