Simple Spring Cloud Implementation
Spring Cloud provides convenient tools for building cloud-native applications. A cloud-native system is designed for the cloud and commonly combines multiple small services into a larger system.
Architecture
This example implements service delivery with three applications:
DiscoveryServer: Registers application information and supports load balancing.GatewayServer: Serves as the entry point for requests.BackendService: Returns the actual response.
Implement DiscoveryServer
Use Netflix Eureka. Adding @EnableEurekaServer to a Spring Boot application is enough to create a Eureka server.
Create the Project
curl https://start.spring.io/starter.tgz \
-d bootVersion=2.4.5 \
-d dependencies=spring-discovery-server \
-d baseDir=spring-eureka-server \
-d artifactId=discovery-server \
-d packageName=com.devkuma.cloud.discovery.server \
-d applicationName=DiscoveryServerApplication \
-d packaging=jar \
-d javaVersion=1.8 \
-d type=gradle-project | tar -xzvf -
Implement the Source Code
package com.devkuma.cloud.discovery.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class DiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryServerApplication.class, args);
}
}
The server does not need to register itself or query the registry:
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
Open http://localhost:8761/.

Create GatewayServer
GatewayServer communicates with clients and acts as a reverse proxy for BackendService.
Create the Project
curl https://start.spring.io/starter.tgz \
-d bootVersion=2.4.5 \
-d dependencies=cloud-eureka,cloud-gateway \
-d baseDir=spring-gateway-server \
-d artifactId=gateway-server \
-d packageName=com.devkuma.cloud.gateway.server \
-d applicationName=GatewayServerApplication \
-d packaging=jar \
-d javaVersion=1.8 \
-d type=gradle-project | tar -xzvf -
Implement the Source Code
package com.devkuma.cloud.gateway.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
server:
port: 8001
spring:
application:
name: gateway-server
cloud:
gateway:
routes:
- id: backend-service
uri: lb://backend-service
predicates:
- Path=/backend/**
filters:
- RewritePath=/backend/(?<path>.*),/$\{path}
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
instance:
preferIpAddress: true
The application name is registered with Eureka. Requests to /backend/** are forwarded to BackendService.
Create BackendService
Create the Project
curl https://start.spring.io/starter.tgz \
-d bootVersion=2.4.5 \
-d dependencies=cloud-eureka \
-d baseDir=spring-backend-service \
-d artifactId=backend-service \
-d packageName=com.devkuma.cloud.backend.service \
-d applicationName=BackendServiceApplication \
-d packaging=jar \
-d javaVersion=1.8 \
-d type=gradle-project | tar -xzvf -
Implement the Source Code
package com.devkuma.cloud.backend.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class BackendServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BackendServiceApplication.class, args);
}
}
package com.devkuma.cloud.backend.service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "Hello backend service";
}
}
server:
port: 8002
spring:
application:
name: backend-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
instance:
preferIpAddress: true
Open http://localhost:8002/hello.

Finish
Start all applications and open http://localhost:8001/backend/hello.

The complete project is available here.