Loading
 
What's Next: View All Tutorial →

Spring boot + Spring Cloud - Service Registration - Eureka Client - Microservice Architecture - Part 2

A simple project create a Calculator add() rest controller and register this service in the eureka server   Maven Dependency:   <?xml version="1.0" encoding="UTF-8&...

Published Date: 07/09/2018  Last Modified Date: 07/09/2018    New Post



A simple project create a Calculator add() rest controller and register this service in the eureka server
 

Maven Dependency:

 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.demo</groupId>
	<artifactId>spring-boot-microservice-calculator</artifactId>
	<version>1.0</version>
	<packaging>war</packaging>

	<name>spring-boot-calculator-service</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.M8</spring-cloud.version>
	</properties>

	<dependencies>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<repositories>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>
 

application.yml

server:
  port: 8081

spring:
  application:
    name: calculator-service
    

eureka:
    client:
        service-url:
            default-zone: http://localhost:8761/eureka     

 

The application will be identifiy as a "calculator-service" in the eureka server  dashbaord


CalculatorController.java

 
package com.demo.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class CalculatorController {

	@Autowired
	private Environment environment;

	@GetMapping(path = "/add/{a}/{b}")
	public String add(@PathVariable("a") int a, @PathVariable("b") int b) {
		String port = environment.getProperty("server.port");
		return "Response from Port=" + port + " Addition result = " + (a + b);
	}

}
 

Application.java

package com.demo.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient   
public class Application {

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

}

@EnableDiscoveryClient - Register the client serive in the eureka server as defined in the application.yml


Output:


Url #1
http://localhost:8081/add/1/2
Output:
Response from Port=8081 Addition result = 3

Visit Eureka dashboard and you can see this service registered
http://localhost:8761/

Instances currently registered with Eureka

Application AMIs Availability Zones Status
       
CALCULATOR-SERVICE n/a(3) (3) UP (3) - 192.168.0.106:calculator-service:8081 , 192.168.0.106:calculator-service:8082 ,192.168.0.106:calculator-service:8083


 
Awaiting for Administrator approval




← Back to list


Related Post




×