Accessing Gateway Properties from another microservice using Spring boot - spring

I have Config Microservice , Employee Microservice , Gateway Microservice . application.yml will be loaded from Config Microservices for Employee and Gateway Microservice using the Spring.cloud.config.
i need to use one of the Gateway Property in the Employee Microservice . how to achieve this ?
Config Microservice contains below files
application.yml
server:
port: 8080
Gateway.yml
app:
access-table: true
Employee.yml
server:
port: 8081
In the Employee Microservice, Employee.yml is loaded using the Spring cloud config URI .
cloud:
config:
uri: http://localhost:8080
how can i access the access-table property from the gateway.yml in the Employee Microservices ?
i tired using name property but it overrides the other configs , i need to access only that property . is there a way?
cloud:
config:
uri: http://localhost:8080
name: Employee.yml,Gateway.yml

If you have any common properties that required in multiple micro-services then place those properties in config server application.yml.
This application.yml for all micro-services even if have any profile.

Related

Path Definition For Each Route in Cloud Gateway

I would like to add a path to each route that I define in spring.cloud.gateway.routes without defining context path, server.servlet.context-path, of each microservice.
E.g.: My gateway runs at port 9090, and my microservice runs at port 8071. The configuration for the microservice in the gateway is like this:
spring:
cloud:
gateway:
routes:
- id: auth
uri: 'http://localhost:8071'
If I hit http://localhost:9090/lorem/ipsum, I can ping the microservice, and see the response. But, I would like to ping the microservice from the gateway as http://localhost:9090/auth/lorem/ipsum.
If I define Path key in predicates, it pings http://localhost:8071/auth/lorem/ipsum, which fails, because the URL in the microservice is not /auth/lorem/ipsum but /lorem/ipsum.
spring:
cloud:
gateway:
routes:
- id: auth
uri: 'http://localhost:8071'
predicates:
- Path=/auth/**
Is there a way to define a path for each microservice or do I have to set server.servlet.context-path for each microservice to achieve what I would like to do?
Spring boot version: 2.6.1 & Spring cloud version: 2021.0.0

Spring boot admin with Eureka client and custom context path fails on health status

I have configured my spring boot application monitor-client to register to Eureka. I have a separate spring boot admin (SBA) application monitor that monitors all applications registered to Eureka.
If the context-path is not set in my application, everything is working fine. However if the context-path is set, SBA does not show correct information anymore. From the documentation it seems that I need to update the metadata properties of Eureka which I have done.
My monitor application is configured as follows:
application.properties:
spring.application.name=monitor-client
server.servlet.context-path=/monitor-client
server.port=9876
# Monitoring config
management.endpoints.web.exposure.include=*
eureka.instance.metadataMap.management.context-path=/monitor-client/actuator
My application is showing as 'offline' but I can browse all the details in the 'insight' tab. I suppose SBA correctly access the actuator endpoints but incorrectly uses the /health endpoint and I do not understand how that is possible?
If I go to the administration interface of SBA, I notice that the wrench endpoint and the health endpoint are not the same:
localhost:9876/monitor-client/actuator (wrench)
localhost:9876/actuator/health (health)
I've fiddled around with some settings but between the managment settings, the actuator settings and the eureka settings, I'm not sure which one is to be configured for this to work.
I've tried:
management.endpoints.web.base-path
eureka.instance.health-check-url-path
I'm currently using spring boot 2.1.2.RELEASE and matching version of SBA
Can you try with these configuration
server:
servlet:
context-path: /monitor-client
#management config
eureka:
instance:
health-check-url-path: /actuator/health
metadata-map:
management.context-path: /monitor-client/actuator
client:
serviceUrl:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoint:
health:
show-details: always
endpoints:
web:
base-path: /actuator
exposure:
include: "*"

Auto-configure routes with Zuul and Eureka

Through reading various books / tutorials, it appears that it is possible to auto-configure routes in Zuul when using it in combination with Eureka service discovery. That means that I don't have to explicitly add routes to Zuul's application.properties.
So I understand this correctly? Or do I still need to add routes explicitly to Zuul in order it to work as a gateway?
I would like it to automatically create routes from the application name's that are registered with Eureka. Is this possible?
(Note: I have actually tried this, but when I go to http://localhost:8762/routes I just get an error page.)
Sure. In most microservices implementations, internal microservices endpoints are not exposed outside. A set of public services will be exposed to the clients using an API gateway.
The zuul proxy internally uses the Eureka Server for service discovery.
I would like it to automatically create routes from the application name's that are registered with Eureka. Is this possible?
Sure. I will show you gateway example.
1. Create your service project (user-service)
create application.properties file
# --- Spring Config
spring:
application:
name: OVND-USER-SERVICE
# Eureka client
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URL:http://localhost:8761/eureka/}
2. Setting up Zuul project (Gateway-service)
1.#EnableZuulproxy to tell Spring Boot that this is a Zuul proxy
#SpringBootApplication
#EnableZuulProxy
#EnableDiscoveryClient
public class GatewayServiceApplication {
2.create an application.properties file
# =======================================
# Gateway-service Server Configuration
# =======================================
# --- Spring Config
spring:
application:
name: gateway-service
server:
port: ${PORT:8080}
# Eureka client
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URL:http://localhost:8761/eureka/}
zuul:
host:
routes:
## By default, all requests to user service for example will start with: "/user/"
## What will be sent to the user service is what comes after the path defined,
## So, if request is "/user/v1/user/tedkim", user service will get "/v1/user/tedkim".
user-service:
path: /user/**
service-id: OVND-USER-SERVICE
another-service:
path: /another/**
service-id: OVND-ANOTHER-SERVICE
Eureka website ( localhost:8761 )
Yes. You can integrate Zuul with Eureka and configure the routes based on application names registered in Eureka. Just add the following configuration to Zuul application:
zuul:
ignoredServices: "*"
routes:
a-service: /a-service/**
b-service: /b-service/**
c-service: /c-service/**
d-service: /d-service/**

Reading key value from consul in springboot application

I want to read my external configs like database host and port from consul.
I want to access it in my Springboot application.
So I have created a springboot application and have added cloud config for consul configuration.
I have created an bootstrap.yml where I give my consul host and port and I am able to connect.
But I am not able to fetch any key-value pair from consul.
I have posted my bootstrap.yml below asd well.
Can somebody guide me how to do that
spring:
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true
data-key: config/application/datakey
TIA
You can make use of ConsulClient and then use getKVValue method to read the kv configs you have set in consul.

Spring rest controller in management port?

When using spring-boot, how to you expose a controller at management port, the port defined by management.port?
In Spring Boot 2.x it is done using #RestControllerEndpoint annotation. For example if you define following properties in application.yml:
management:
server:
port: 8081
endpoints:
web:
base-path: "/actuator" # default value
and some controller as #RestControllerEndpoint(id="debug"), it will be mapped as
http://localhost:8081/actuator/debug/...
More info:
https://www.javadevjournal.com/spring-boot/spring-boot-actuator-custom-endpoint/
Okay, I ended up implementing
org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint
This exposes the Controller into management port rather than application port.

Resources