Search This Blog

Tuesday, 12 July 2016

Creating a Service within IntelliJ IDEA to be used by the Service Registry for Pivotal Cloud Foundry

In this example I am going to show how to use IntelliJ IDEA 15 to create a service application from the IDE to be consumed by the Service Registry service in Pivotal Cloud Foundry (PCF). For more information on this service view the docs page below.

http://docs.pivotal.io/spring-cloud-services/service-registry/index.html

Service Registry for Pivotal Cloud Foundry® (PCF) provides your applications with an implementation of the Service Discovery pattern, one of the key tenets of a microservice-based architecture. Trying to hand-configure each client of a service or adopt some form of access convention can be difficult and prove to be brittle in production. Instead, your applications can use the Service Registry to dynamically discover and call registered services

1. Start IntelliJ IDEA and either "Create a New project" or add a "New Module" to an existing project.

2. Ensure you select "Spring Initializer" as shown below


3. Click Next

4. Describe your project or module, I normally use Maven and generate a JAR file



5. Click Next

6. At the minimum here we only need to select "Service Registry (PCF)" as shown below for the dependency. Of course you would select other options dependncies depending on what the service needed such as REST, JPA, H2 or MySQL etc


7. Click Next

8. Name your new model or project


9. Click Finish

10. Click Finish

11. Your service application must include the @EnableDiscoveryClient annotation on a configuration class. To do that we simply add the annotation to our main class as follows


Java Code
  
package pas.au.pivotal.service.hr;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import javax.annotation.PostConstruct;

@SpringBootApplication
@EnableDiscoveryClient
public class EmployeeServiceApplication
{
 @Autowired
 private EmployeeRepository employeeRepository;

 public static void main(String[] args) {
  SpringApplication.run(EmployeeServiceApplication.class, args);
 }

 @PostConstruct
 public void init()
 {
  employeeRepository.save(new Employee("pas"));
  employeeRepository.save(new Employee("lucia"));
  employeeRepository.save(new Employee("siena"));
  employeeRepository.save(new Employee("lucas"));
 }
}

12. Set the spring.application.name property in application.yml. It might be an application.properties file BUT rename it to YML as I know that works. below I not only set the application name I also set the registrationMethod to "route" which is the default and then turn off security as it is enabled by default.

spring:
  application:
    name: employee-service

cloud:
  services:
    registrationMethod: route

security:
  basic:
    enabled: false

So that's all we really need to do here. Of course we will need to add code to our service to do what it needs to do BUT all the config required to enable this service to automatically register itself with the "Service Registry" in PCF is done.

13. Before we deploy this to out PCF instance we have to be sure we have a "Service Registry" service created as shown below using the CF CLI mine is already created.


14. Create a manifest.yml file for the service to be deployed, notice how it binds to the service registry "apples-service-registery", this will ensure it automatically gets registered on deployment with the Service Registry service

---
applications:
- name: apples-employee-service
  memory: 512M
  instances: 1
  host: apples-employee-service-${random-word}
  path: ./target/EmployeeService-0.0.1-SNAPSHOT.jar
  services:
    - apples-service-registery

15. Push the service application to PCF as shown below


.....


16. Login into your PCF instance App Manager UI, in this demo I am using PWS instance run.pivotal.io and find your "Service Registry" service and click on it as shown below



17. Click on the "Manage" link as shown below


18. Verify your service is registered as shown below


More Information

http://docs.pivotal.io/spring-cloud-services/service-registry/index.html

https://docs.pivotal.io/spring-cloud-services/service-registry/resources.html

http://docs.pivotal.io/spring-cloud-services/service-registry/writing-client-applications.html

No comments: