Dockerize vue js front end and spring boot backend and deploy on kubernetes cluster - spring-boot

I have developed spring boot backend and vue js front end I could successfully deploy the spring boot app and create cluster ip service for spring boot app but I have never work with NPM project on docker and kubernetes. I also have problem in Axios when I locally testing backend and frontend I give (localhost:backendport/endpoint) for axios and how can I adapt it to kubernetes. should I give cluster ip service name instead of localhost:port/endpoint -> clusteripservice/endpoint if so how can I externalize the configurations and how can I deploy both app.
here is Axios call
import axios from 'axios'
const API_URL = 'http://localhost:8084'
//const API_URL = '/'
class UserDataService {
retrieveAllUsers() {
return axios.get(`${API_URL}/user/getall`);
}
}
export default new UserDataService()

Idea is to use nginx as your app container and proxy pass to proxy to back-end. So you need to define location for your api, i.e. /api and proxy that.
Then if you are using axios, you would call all back-end endpoints on relative path, i.e.
axios.get('/api/v1/myApiPath')
So you do not need to worry about hostname in calls to back-end.
Also, for development you similarly use vue.js development settings to also proxy back-end via npm.
See my toy project here for details how it's done:
Deployment piece - https://github.com/taleodor/mafia-deployment
UI piece - https://github.com/taleodor/mafia-vue
Specifically nginx settings with proxy configuration - https://github.com/taleodor/mafia-vue/blob/master/nginx/default.conf (note that it's using websockets which you may remove if you're not using them). Specifically vue development server configuration - https://github.com/taleodor/mafia-vue/blob/master/vue.config.js.
Write up on CI / CD workings (unrelated to your question but maybe useful) - https://itnext.io/building-kubernetes-cicd-pipeline-with-github-actions-argocd-and-reliza-hub-e7120b9be870

Related

Forwarding non-API requests to a NextJS server

I'm trying to use Spring Boot to serve a REST API and a NextJS server to serve the frontend which calls that API. Right now, I have 2 separate servers (Spring on port 80 and NextJS on port 3000). What I want to do is forward any GET requests that the spring server receives that don't start with /api/ to the NextJS server so that the Spring server would act as a proxy for those routes. I know that you can run next export and serve the assets from Spring's static folder, but I need to use getServerSideProps in NextJS so that isn't an option. How can I do this in Spring Boot?

Spring Cloud Gateway on App engine is not routing the requests to downstream services

I am trying to deploy a simple microservice application (Spring boot) in GAE. The application has 3 services -
1. Discovery Service
2. Spring Cloud Gateway
3. Customer service
I am trying to call an API through API gateway. This works fine in local machine but getting 404 in GAE hosted application.
The URL I am using is - https://Gateway-url/customer-service/version
And corresponding YAML config is :
spring:
cloud:
gateway:
routes:
-id: customer-service
uri: https://XXXXXX.customer-service.appspot.com/
predicates:
- Path=/customer-service/**
filters:
- StripPrefix=1
The URI configured in above code snippet is - target URL of GAE service.
So When I hit the https://Gateway-url/customer-service/version url in browser, I am getting 404 error but it works if I hit the direct url https://customer-service-url/version
When I changed the API Gateway configuration in the local environment, (change uri to cloud based service uri), the issue is reproducible. So I am thinking that Gateway is not able to route the requests to App Engine Service target URL.
How can I fix this problem?
I think the problem is the url that you are using in the configuration file to route your requests.
According to google cloud documentation, to call from one service to another you should use a endpoint like this
https://VERSION_ID-dot-SERVICE_ID-dot-PROJECT_ID.REGION_ID.r.appspot.com
As yo can see here
https://cloud.google.com/appengine/docs/standard/java/communicating-between-services
And it seems that the url that you are using doesn't fit int this.
Anyway I think that in a serverless environment such as app engine the best option to perform service discovery and to route requests via gateway is using the services provided by google. In case of service discovery it is done automatically and if you want a gateway you can use google cloud endpoints that have a greate integration with app engine. Because this is the gateway intended to use with these applications

React.js and Tomcat

I have a Spring MVC Web application which runs on Apache Tomcat Server. Someone created the front-end for this application with React.js and Redux and I need to integrate it in my project, but it is opened on port 3000 and Tomcat runs on port 8080. How can I do this? I do not know any React.
You need to know what is going on here.
React is a client-side application
If it runs on port 3000, it's probably running on webpack-dev-server
React.js doesn't run on a port. The application server runs on a port. You cannot run a React application. You can only serve an application written in react. React is a library. At the end of development, all you would get is an index.html and a (or a few) bundle.js files.
In order to serve React as a resource in your Spring MVC application, have a controller that returns React's index.html. If your react application has a react router, it will take over from there (subsequent url change in browser will not trigger server-side rendering).
My personal preference is to host react as a separate application and probably use express.js as the application server instead of tomcat.
This lengthy article also touches on how to serve React's index.html using a controller. Which is basically the same as serving your JSP or webjars
If you using spring boot in application.properties write server.port=8080. And if you using Spring you will have to change port in tomcat. In order to do so
.Go to tomcat>conf folder
.Edit server.xml
.Search "Connector port"
.Replace "8080" by your port number
.Restart tomcat server.

Angular (4) CSRF different ports on same computer

I have a small spring boot app which exposes a REST-service with two methods "/upload" (POST) and "/show" (GET)
When I run my angular 2 app on my computer, http://computer.domain.com:4200, and have the spring boot rest-service on the same computer on a different port, http://computer.domain.com:8080, the CSRF-part of angular 2 doesn't wanna send the XSRF-token with the header.
If I deploy the Angular GUI within the spring boot WAR so that both the REST and GUI is accesiable via http://computer.domain.com:8080 everything that's CSRF-related then works.
I thought that Cookies and angular were 'agnostic' when it came to what port they were sent from, as long as it originated from the same server.
Am I missing some piece of config that makes angular send CSRF-headers?
Cookies/browsers may be not agnostic from the port. See this question and the related answer.
The common setup is to configure angular-cli to setup a proxy.
For example if your rest endpoint start with api, you should create a proxy.conf.json
{
"/api": {
"target": "http://computer.domain.com:8080",
"secure": false
}
}
Then update your package.json start script to:
"start": "ng serve --proxy-config proxy.conf.json",
The frontend should make its http call to http://computer.domain.com:4200/api...
Hope it helps

Angular 2, Spring boot , spring security , login form

I have a Front end application that runs on http:// localhost:4200, with angular 2 (I've used angular-cli for generating the project).
The application runs without any problems (I can get and post info from/to database using Angular 2 Http service:
getList () {
return this._http.get("http:// localhost:8080/").map(res => res.json());
}
On the other hand, I have a Back end application running with spring boot on http:// localhost:8080/ (note the ports), that provide a REST Api for my angular 2 application.
Sending requests from http://localhost:4200/ to http://localhost:8080/ works as expected.
Before I've programmed an application with spring framework & angularJS 1, spring security took care of login and security in the same app (maven project).
Now I have two separate applications that are communicating via http (RESTful Api with spring boot , and front end Api with angular 2)
How can I set my login form and where to put it, and how to configure spring security with my angular 2 application?
When you spin up the Angular Application with Angular CLI, your Angular Pages are served by NodeJs in the backend.
You have a couple of options here.
The login can be handled at NodeJS server which can in turn invoke
Spring Boot application to Authenticate and Authorize.(I think, you might need to do some tweaks like using express server instead of lite server. take a look here https://stackoverflow.com/a/37561973/6785908)
After initial development, you can copy over your AngularJS
resources to a Spring MVC (Spring Boot) server and use it to serve
your pages (as well as the rest APIs), just as you were doing
before.
Make the Angular App invoke the spring boot Service (I guess , you
are using the $http.post to submit the form, if that's the case you
can just change the URL so that it can hit spring boot app instead.)
For the production deployment, use nginx/httpd to serve the static
files (AngularJS/CSS etc) and route/proxy all the dynamic request to
the spring boot app.
I would strongly suggest the 3rd option.

Resources