How can I reference another app in container? - spring

I have a docker compose with two applications:
app1 -spring boot web application with thymeleaf server pages.
app2 - another web application with 8080 port exposed.
I need to be able to address to app2 from ui of app1, like:
<a th:href="#{/app2/users}">Users</a>
But it's obvious that ui can't make redirection according to container alias.
Also the port is missing, so it needs to be configured.
Perhaps a reverse proxy can be used in that case, but looks like it can be solved much simpler. Is there any Spring component that can be used for routing or a gateway?

The resolved url of /app2 is http(s)://app1/app2 (depending on whether you have SSL configured), which isn't what you want.
Try an absolute url: http(s)://app2:8080/users. I'm not sure what th:href does, as I'm not familiar with thymeleaf, but if it doesn't work, just us plain html: Users

Related

(How) Can I run more than one Spring Boot application on the same server and port?

I have a Spring Boot web application that I'd like to split up into about six separate applications; one would provide the homepage and login at endpoints under "/", and the others would each claim a subdirectory path ("/subsystem1", "/subsystem2", etc...). I have a pretty clear idea how I could use JWT to pass authentication/authorization from the login app to the others.
The main reason for this is so that each subsystem can be modified or updated without shutting down the others. And organizationally, so we don't have to subject the entire app to a QA process when only one subsystem is changed.
Is it possible to set up multiple Spring Boot instances to run on the same server at the same time and the same port, with different paths/directories to their endpoints? How?
I was unable to find any duplicate question, but here are two related questions that may offer clues:
From Is there a standard way to customize the deploy path in Spring Boot? I learned that I can set the application property server.servlet.context-path to prefix the whole application with a subdirectory name (e.g. "/subsystem1"). But I still can't run two apps at the same time, even if both claim different subdirectories. Spring Boot reports "Web server failed to start. Port 8080 was already in use."
There's Multiple Spring-boot applications running on one Tomcat
but I'd prefer to use standalone Spring applications with their embedded Tomcat instances rather than the less-recommended WAR packaging and deployment to an external Tomcat container.
This one looks promising -- Deploying Multiple Spring Boot Web Applications in Single Server -- but the answers focus on whether standalone or Tomcat container deployments are better, and doesn't touch on the "how-to" question.
Acceptable answers:
If, as ekalin suggests, it is impossible to have multiple Spring Boot apps listen to the same port, here are a couple of ideas I have brainstormed (but don't know how to accomplish):
Perhaps the instances could be running on different ports but the main app (the one with the login page) could "forward" or redirect to the other apps in some way that hides their true URLs? E.g. "localhost:8080/subsystem1" would be an alias for "localhost:8081/".
Perhaps the applications could each have their own Docker containers, all running within a shared Docker network, and we use Docker somehow to map each URL path to the right app? Could this be set up with docker-compose?
We set up a proxy server of some kind that remaps URL paths to the separate applications.
You can't have more than one application listening on a port. How would the kernel which application to send the packages to?
You could run an http server such as nginx listening on 8080, and each application in a different port, and then proxy the requests based on the URL to the desired application.

Zuul proxy that discovers routes dynamically

I have a simple Zuul app that has a single route in the application.yml to route to my microservice. It's working.
However, what I'm looking for is a more dynamic solution where I can wire up routes dynamically, either through code or perhaps by POSTing to some Zuul endpoints during a build (possibly by using springfox and a swagger definition from microservices). I could not find an API for Zuul.
I'm somewhat aware of Eureka and that seems like a solution to abstract away the routing by doing discovery. However, I'm curious if there's a solution without introducing Eureka. If there's a way to wire up these routes in Zuul during a build vs. having to edit the application.yml every time.
Thanks in advance.
If you go for Eureka this will actually work ootb. Zuul as packaged in spring cloud will automatically expose every service using its name. So if you register a service called users in Eureka, Zuul will automatically create a route /users forwarding to the instances by default. That will only allow simple url structures but should solve your problem.
Please see the official documentation for details:
By convention, a service with the ID "users", will receive requests from the proxy located at /users (with the prefix stripped). The proxy uses Ribbon to locate an instance to forward to via discovery, and all requests are executed in a hystrix command, …
I'm actually editing a blog post about this exact topic (Routing and Filtering using Spring Cloud Zuul Server) but the source code has been available and working for some time now. Feel free to use it as a reference:
https://bitbucket.org/asimio/zuulserver
https://bitbucket.org/asimio/discoveryserver (in case routes are configured with serviceIds)
https://bitbucket.org/asimio/demo-config-properties/src (Zuul-Server-refreshable.yml where routes are dynamically updated).
Look at the refreshable Spring profile settings. This Zuul setup works with both, hard-coding routes url or discovered using Eureka.
It also acting as a Spring Cloud Config client so that routes could be dynamically updated via Git, which is also covered in another blog post: Refreshable Configuration using Spring Cloud Config Server, Spring Cloud Bus, RabbitMQ and Git.

Route Existing Services via ZUUL without adding routing rule

I am trying to route existing services via Spring cloud Netflix Zuul.
I have an existing service available at below url,
http://localhost:3080/query-service/getquery/1
Out of the box, with zuul I can route to the service as below,
localhost:9000/queryservice-id/queryservice/getquery/1, with "queryservice-id" as the service-id of the service when it is registered in Service Registry. The zuul port is 9000.
I do not want to change the context path and service path information when accessing the service via ZUUL.
With ZUUL, I want to access the service as below,
http://localhost:9000/query-service/getquery/1
I do want to prefix with the "serviceId". This is because I do not want to impact any existing clients of the service. Only the host and port changes, without serviceId.
I was able to accomplish it as below with ZUUL Configuration,
zuul:
routes:
query-service:
path: /query-service/**
serviceId: query-service
stripPrefix: false
With the above configuration, I am able to only use the zuul host and port, the other service specific information remains as before. Seems like stripPrefix is helping in routing without the serviceId.
But, I have a lot of services and will be adding more services too.
For every such service, I do not want to be adding a rule like that to ZUUL configuration which will mean rebuilding and recycling the ZUUL Service.
I feel there should be a simpler and better way to accomplish this, without a big effort, because the change I want to do is common to all services
Is there a way to making this change common for alll the services I want
to be routed via ZUUL.
Thanks,
As checked with the Spring Cloud Netflix team this is recommended approach.
https://github.com/spring-cloud/spring-cloud-netflix/issues/1549

Use zuul to deliver static content

I have a Zuul reverse proxy in front of my spring boot microservices, used as an API gateway.
Can the Zuul spring boot app be used to deliver static content, IE client code that calls the API gateway? I dont want to host this in a microservices or have another VM to manage for hosting content
I tried having a static folder in resources folder, but cant figure out how to map this in the application.yml responsible for routing. I dont want to route it to a NGnix or apache server, but use the embedded Tomcat of Zuul Spring boot app.
What would be the route to put in the route config, or what is the best approach for this.
Thank you
You don't have to map it in the routes config. Whatever you put in the resources/static folder will be served, as long nothing else is mapped on the same path.

Spring Cloud Sidecar is not working as expected

Based on the documentation, the #EnableSidecar annotation acts as a proxy for non-JVM applications that wish to register in Eureka. Accordingly, the configuration to be set is:
sidecar:
port: 81 <-- Port of the non-JVM application
health-uri: http://10.135.16.50:${sidecar.port}/api/health.php <-- URI of the non-JVM application's health endpoint
home-page-uri: http://10.135.16.50:${sidecar.port}/
Once the "sidecar" is up and running, we should be able to invoke one of the non-JVM endpoints through the service registry just by using the name that the "sidecar" application used to register in Eureka. So for example, if our "sidecar" application was registered in Eureka as "php-sidecar" to proxy a PHP application with an endpoint such as:
http://10.135.16.50:81/api/microservice.php
Then we should be able to invoke the following endpoint to get to the non-JVM application (assuming "sidecar" is in "localhost" and port 8080):
http://localhost:8080/php-sidecar/api/microservice.php
However, this is not working as expected. When we request the URI just above, a request to "localhost:81" is actually issued, because somehow the "sidecar" is picking up its host URI and not the home-page-uri defined as part of the sidecar's properties.
If we run the non-JVM application locally using localhost, then everything works as expected, but this is definitively not the realistic use case.
Therefore, what am I missing in my configuration to tell Spring Cloud (Zuul in this particular case) to use the non-JVM home-page-uri and not my local host URI?
Thanks for your support.
After some research, it turns out that the sidecar must always be deployed in the same host as the non-JVM application.
After some research, it turns out that the sidecar must always be deployed in the same host as the non-JVM application.
No, You can add sidecar.ip-address = 10.135.16.50 to your yaml config file
I'm using spring-boot-starter-parent version is 1.5.6.RELEASE
working fine

Resources