freemarker iterating sequence of compound object - freemarker

I have a sequence coming from yaml data model like this:
project:
artifactId: foo
groupId: org.foo
profiles:
- dev:
baseurl: localhost
baseport: 8080
- uat:
baseurl: foo.com
baseport: 8443
- prod:
baseurl: foo.com
baseport: 8444
in my template I can see that it is a sequence:
<#if map.project.profiles?is_sequence>YES</#if>
I've tried to iterate like this:
<#list map.project.profiles as n>
${n?index}
</#list>
but how can I reach element baseurl? ${n?index} works well, but ${n.baseurl} or ${map.project.profiles[n].baseurl} doesn't.
thx
Zamek

It's the YAML that doesn't mean what you mean. When you have an item like this:
- dev:
baseurl: localhost
baseport: 8080
that creates an element in a list, but that element will be a Map that contains a singe entry, which entry has key dev, and a value that's another nested Map, which contains the keys baseurl and baseport. So, you had to write n.dev.baseurl, n.prod.baseurl, etc., but of course that would be a pain.
So, you want a YAML like this:
project:
artifactId: foo
groupId: org.foo
profiles:
dev:
baseurl: localhost
baseport: 8080
uat:
baseurl: foo.com
baseport: 8443
prod:
baseurl: foo.com
baseport: 8444
or like this:
project:
artifactId: foo
groupId: org.foo
profiles:
- server: dev
baseurl: localhost
baseport: 8080
- server: uat
baseurl: foo.com
baseport: 8443
- server: prod
baseurl: foo.com
baseport: 8444

Related

How to set the servers property of openapi with Apache Camel?

I am trying to setup a openapi specification and publish the API with Apache Camel and Spring. I tried using restConfiguration, adding the property in the application.yaml, and using the #OpenApiProperty on the app. Everytime the generated yaml reads:
- servers
- url: ""
platform:
# not used
urlrewrite:
enabled: false
token: ${LOCAL_TOKEN:}
apim:
token_ep: ${x/token}
client:
username: ${apim_client_username:}
password: ${apim_client_password:}
consumerKey: ${apim_client_id:}
consumerSecret: ${apim_client_secret:}
endpointsOrchestrated:
server: myServer
emr-endpoint: xxxxxxxx
triggerName: ${PLATFORM_MODULE_ID}
env: dev
domain: ${PLATFORM_MODULE_DOMAIN}
openapi:
title: My Sample api
version: 1.0.0
camel:
dataformat:
jackson:
auto-discover-object-mapper: true
springboot:
tracing: false
rest:
host: myhost.com
port: 8080
spring:
application:
name: aaa
profiles:
active: ${ENV:local}
server:
servlet:
context-path: /
port: ${TOMCAT_PORT:8080}
host: localhost
# MAX HTTP THREADS
tomcat:
threads:
max: ${MAIN_HTTP_THREADS:200}

how to fix swagger-ui path

There was a problem with the Swagger document path.
build.gradle
implementation("org.springdoc:springdoc-openapi-ui:1.6.8")
application.properties
server.servlet.context-path: /poo
REQ : http://localhost:port/poo/swagger-ui/index.html
-> Failed to load remote configuration.
REQ : http://localhost:port/poo//swagger-ui/index.html
-> OK
Can be Ingress, try this
Change this
spec:
rules:
- http:
paths:
- path: /my-context/my-path/swagger
pathType: ImplementationSpecific
backend:
service:
name: my-service #.ns-config.svc.cluster.local
port:
number: 8080
For this
spec:
rules:
- http:
paths:
- backend:
service:
name: my-service #.ns-config.svc.cluster.local
port:
number: 8080
path: /my-context/my-path/swagger
pathType: ImplementationSpecific
Might check this comment describing the best approach to using spring-doc behind custom context-path or proxy.

Specify which plugins to use on a route in Kong dbless mode

In dbless mode on kong we can specify which routes to be added on plugin using this declration:
plugins:
- name: jwt
route: <route>
config:
secret_is_base64: false
But I need to specify which plugins this route is going to use with something like:
route:
- name: getUser
paths:
- /user
plugins: [<plugin1>, <plugin2>]
Any way to do this?
I dont want to update all the plugin declarations everytime I add a new API on a service.
You can declare your plugins like this:
services:
- connect_timeout: 60000
host: alb.host.dev
name: service1
routes:
- hosts:
- api.host.com
- www.host.com
name: my_host
methods:
- GET
paths:
- /user
plugins:
- name: plugin1
config:
redis_database: 0
redis_host: redis.dev
redis_password: null
redis_port: 6379
redis_timeout: 2000
- name: plugin2
config:
foo: 1
bar: 2

Problem with unknown ports on Spring Eureka Client registration

Well, a have tried ALL tutorials, ALL answer on Stackoverflow. But I still face the same error:
On Heroku, Eureka Clients are receiving random ports that I don't know where they come from.
And these random ports are different from Heroku Environment variable $PORT. I know that $PORT is the container Dyno port, not the host port xxxxx.herokuapp.com that the out side world can access. Again, I don't know where these ports come from!!!
The link clients in the Eureka Dashboard stay like this:
hdfb2324-8jfw-83ud-dkdf-9ej90jefj201.qwer.dyno.rt.heroku.com:inight-ws-gateway:${RANDOM PORT}
When I click, go to https://inight-ws-gateway.herokuapp.com:${RANDOM PORT}/actuator/info. I take the Timeout because the correct port is 443, Https default.
I have 3 applications, 1 Eureka Server and 2 Eureka Clients (Zuul Gateway and a trivial Rest API).
Every works ok when I run in my localhost, but when I deploy in Heroku, it's happen.
Eureka Server application.yaml:
server:
port: ${PORT}
spring:
application:
name: '#project.artifactId#' # inight-ws-discovery
version: '#project.version#'
eureka:
client:
fetch-registry: false
register-with-eureka: false
management:
endpoints:
enabled-by-default: false
web:
exposure:
include:
- health
- info
endpoint:
health:
enabled: true
info:
enabled: true
Eureka Client (Zuul gateway) application.yaml:
server:
port: ${PORT}
spring:
application:
name: '#project.artifactId#' # inight-ws-gateway
version: '#project.version#'
eureka:
instance:
hostname: ${INIGHT_EUREKA_INSTANCE_HOSTNAME} # inight-ws-gateway.herokuapp.com
client:
enabled: true
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: ${INIGHT_EUREKA_CLIENT_SERVICE_URL_DEFAULTZONE} # https://inight-ws-discovery.herokuapp.com/eureka/
healthcheck:
enabled: true
zuul:
sensitive-headers:
- Cookie
management:
endpoints:
enabled-by-default: false
web:
exposure:
include:
- health
- info
- routes
endpoint:
health:
enabled: true
info:
enabled: true
routes:
enabled: true
jwt:
config:
privateKey: ${INIGHT_JWT_CONFIG_PRIVATEKEY}
Eureka Client (Rest API) application.yaml:
server:
port: ${PORT}
spring:
application:
name: '#project.artifactId#' # inight-ws-auth
version: '#project.version#'
datasource:
hikari:
schema: inight
jdbc-url: ${INIGHT_DATASOURCE_JDBC_URL}
username: ${INIGHT_DATASOURCE_USERNAME}
password: ${INIGHT_DATASOURCE_PASSWORD}
driver-class-name: ${INIGHT_DATASOURCE_DRIVER_CLASS_NAME}
jpa:
show-sql: ${INIGHT_JPA_SHOW_SQL}
hibernate:
ddl-auto: ${INIGHT_JPA_HIBERNATE_DDL_AUTO}
eureka:
instance:
hostname: ${INIGHT_EUREKA_INSTANCE_HOSTNAME} inight-ws-auth.herokuapp.com
client:
enabled: true
service-url:
defaultZone: ${INIGHT_EUREKA_CLIENT_SERVICE_URL_DEFAULTZONE} https://inight-ws-discovery.herokuapp.com/eureka/
healthcheck:
enabled: true
management:
endpoints:
enabled-by-default: false
web:
exposure:
include:
- health
- info
endpoint:
health:
enabled: true
info:
enabled: true
jwt:
config:
privateKey: ${INIGHT_JWT_CONFIG_PRIVATEKEY}
swagger.basePackage: br.com.bz.inight.auth.controller
my fault!!!!!
I have a couple of microservices on Heroku:
Eureka Server
Zuul Gateway
Some app services
The link on Eureka Server Dashboard, doesn't matter. Whats matter is the https://GATEWAY.herokuapp.com/actuator/routes and the <port enabled="true">80</port> on https://EUREKA_SERVER.herokuapp.com/eureka/apps.
But for this happen (<port enabled="true">80</port> on app services) you MUST ,EXPLICITLY, declare in application.yaml
eureka:
instance:
non-secure-port: 80
hostname: ${EUREKA_CLIENT_INSTANCE_HOSTNAME} # herokuapp_name.herokuapp.com
If you read the documentation, you will see that the property eureka.instance.non-secure-port has the default value 80. I don't know why, but, if you don't declare it, the <port enabled="true">80</port> are not set up on microservices applications on Eureka Server.
Check the tutorial https://blog.heroku.com/managing_your_microservices_on_heroku_with_netflix_s_eureka.
Well, these are my architecture application.yaml:
coffee-eureka-server application.yaml
spring:
application:
name: '#project.artifactId#'
server:
port: ${PORT}
eureka:
instance:
hostname: ${EUREKA_CLIENT_INSTANCE_HOSTNAME} # herokuapp_name.herokuapp.com
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: ${EUREKA_SERVER_URI}/eureka/ # in this case, same value of eureka.instance.hostname, but with https:// prefix, of course
coffee-zuul-gateway application.yaml
spring:
application:
name: '#project.artifactId#'
server:
port: ${PORT}
eureka:
instance:
hostname: ${EUREKA_CLIENT_INSTANCE_HOSTNAME} # herokuapp_name.herokuapp.com
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: ${EUREKA_SERVER_URI}/eureka/
management:
endpoints:
enabled-by-default: true
web:
exposure:
include:
- routes
- info
- health
coffee-simple-ms application.yaml
spring:
application:
name: '#project.artifactId#'
server:
port: ${PORT}
eureka:
instance:
non-secure-port: 80 # <<<<<<<<< HERE
hostname: ${EUREKA_CLIENT_INSTANCE_HOSTNAME} # herokuapp_name.herokuapp.com
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: ${EUREKA_SERVER_URI}/eureka/
When I click, go to https://inight-ws-gateway.herokuapp.com:${RANDOM PORT}/actuator/info. I take the Timeout because the correct port is
443, Https default.
When you are hosting on localhost the link would be: http://localhost:${PORT}/actuator/info
On Heroku this link is: https://inight-ws-gateway.herokuapp.com/actuator/info
The difference:
It is using http secure, https
The port number is "gone"
The hostname inight-ws-gateway.herokuapp.com is automatically translated into a ${IP_ADDRESS}:${PORT}.
You will need to adjust your source code for the https URL generation.
Your web application will run on a port different from the usual http/https port. It does not have to be 80, 8080 or 443. It can be any port. And that port is specified by Heroku through $PORT.

Spring Cloud Gateway : disable default routes

I'm using spring cloud to manage my microservices.
For security reasons, for one specific microservice (name it ms_secure), I want to use custom route choose a specific microservice version depending on client IP.
My gateway config looks like this:
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: ms_secure_v1
uri: lb://ms_secure_v1
predicates:
- Path=/ms_secure/**
filters:
- RewritePath=/ms_secure/(?<segment>.*), /$\{segment}
- name: <my filter>
args:
xForwardedForHeaderName: X-Forwarded-For
hosts:
- <IP1>
- <IP2>
- id: ms_secure
uri: lb://ms_secure_v2
predicates:
- Path=/ms_secure/**
filters:
- RewritePath=/ms_secure/(?<segment>.*), /$\{segment}
- name: <my filter>
args:
xForwardedForHeaderName: X-Forwarded-For
hosts:
- <IP3>
- <IP4>
When when requesting /ms_secure:
IP1 and IP2 will be redirected to ms_secure_v1
IP3 and IP4 will be redirected to ms_secure_v2
My problem is that all my clients will also be able to access directly ms_secure_v1 or ms_secure_v2 by using the default routes:
http:///ms_secure_v1/...
http:///ms_secure_v2/...
I tried to disable these routes by using SetStatus GatewayFilter:
- id: setstatusstring_route
uri: lb://ms-gateway
predicates:
- Path=/ms_secure_v**
filters:
- SetStatus=403
But this route is not matched.
Is there a way to disable these default routes in spring gateway?
The following creates routes in gateway based on services registered:
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
Set it to false (which is the default), if you don't want this.

Resources