I'm making a practice project where I use the Spring Cloud Gateway, Eureka as the service registry and a service called "serviceA" to forward requests to. I have created 3 maven projects, namely the service-registry, the api gateway and the service A. This is all done using Docker for containerization and a docker compose .yml file.
(The spring cloud version I'm using is 2021.0.5)
I provide below some useful files.
application.yml of serviceA
spring:
application:
name: serviceA
server:
port: 0
eureka:
client:
serviceUrl:
defaultZone: http://service-registry:8761/eureka/
application.yml of API gateway
spring:
application:
name: gatewayapi
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: serviceA
uri: lb://serviceA
predicates:
- Path=/serviceA/**
server:
port: 8080
eureka:
client:
serviceUrl:
defaultZone: http://service-registry:8761/eureka/
application.yml of service registry
server:
port: 8761
# per evitare di registrare eureka stesso
eureka:
client:
registerWithEureka: false
fetchRegistry: false
docker compose
version: '3.7'
services:
gateway-api:
container_name: gateway-api
image: ibm-cloud-academy/gateway-api:v1
restart: always
ports:
- "8080:8080"
networks:
- back-tier
service-registry:
container_name: service-registry
image: ibm-cloud-academy/service-registry:v1
restart: always
ports:
- "8761:8761"
networks:
- back-tier
serviceA1:
container_name: serviceA
image: ibm-cloud-academy/service_a:v1
restart: always
networks:
- back-tier
networks:
back-tier:
ServiceA Controller
@RestController
public class ServiceAController {
@GetMapping(value="/")
public String serviceA() {
return "test";
}
}
When I try to make a request to service A by typing "http://localhost:8080/serviceA" I get a 404 error instead of the string "test". Requests are forwarded only if I enter the address "http://localhost:8080/SERVICEA" in the search bar.
In this case you could try to add the annotation @RequestMapping
on the ServiceAController
to map the class on a specific path.
In your case you could use @RequestMapping(value="serviceA")
since you have specified Path=/serviceA/**
in the predicates section of your route.