Deploying Spring Boot app on AWS Elastic Beanstalk — getting 502 error - java

I know, there are a lot of such quesions and answers here, but none of them helped me. I try to install my simple Spring Boot app on AWS Elastic Beanstalk. There is a limitation: it must use .jsp templates, so it's .war, not .jar. If I use predefined Tomcat env on Elastic Beanstalk, everything works fine, but my goal is to use Java env.
When I install my app, its status is getting Degraded and I become 502 when trying to reach the url.
Locally everything works perfect.
I changed port in application.properties to 5000, but it didn't help. I found a recommendation to use a security rule for inbound for TCP Port 5000 "0.0.0.0/32" but it didn't help eather.
Here are my pom.xml dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.properties:
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
server.port=5000
Main Java class:
#SpringBootApplication
public class DemowebApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemowebApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemowebApplication.class, args);
}
}
AWS error logs:
2019/05/07 21:04:45 [error] 7827#0: *19 connect() failed (111: Connection refused) while connecting to upstream, client: 91.232.158.8, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "my-endpoint-tst.us-east-2.elasticbeanstalk.com"
2019/05/07 21:04:46 [error] 7827#0: *19 connect() failed (111: Connection refused) while connecting to upstream, client: 91.232.158.8, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:5000/favicon.ico", host: "my-endpoint-tst.us-east-2.elasticbeanstalk.com", referrer: "my-endpoint-tst.us-east-2.elasticbeanstalk.com/"
I suspect that problem is with my Tomcat configuration, but all my experiments with it didn't help yet.

I had a similar issue with this. I would load the JAR up to Elastic Beanstalk and when I would try to access the URL, I would get the 502 Apache error with the degraded status. Locally, I was set to port 8080 but when I tried to access it through my URL I was getting the 502. My problem was:
1) I was not exporting and uploading the fat .JAR file from my IDE when I was exporting it. I know you said you cant use .JAR but just make sure when you are exporting your project, you are also exporting all the dependencies from Maven with it.
2) This might be a bigger help than the first part of this answer but even when I was putting my port in the application.properties, it wasn't pulling through to the Elastic Beanstalk instance. What I successfully ended up doing to solve this issue was going into the management console in AWS, going to my Beanstalk instance, and going to the configuration tab.
On the configuration tab there is a section Software and in that subsection, go to the Environment Properties. In the Name section of the environment properties, enter SERVER_PORT and for the value section, enter 5000. Click apply and let the instance restart.

Related

How do I use an older Keycloak client with Quarkus 2.13, Keycloak 19 client is not backwards compatible with v18 server

Would like to ask how to override the Keycloak version used by the Quarkus 2.13 BOM to Keycloak 18? My quarkus service could not connect to our Keycloak servers which still use v18. I have lost the error message in my logs but it said something about client secret is invalid when using Keycloak v19 client, but when downgrading to v18, there were no issues.
This PR 27387 in Quarkus bumped the Keycloak version and it broke compatibility. I have tried overriding the property <keycloak.version>18.0.2</keycloak.version> in POM but it doesn't work, it keeps using the same dependency org.keycloak:keycloak-core:19.0.2 and I even tried overriding the dependency quarkus-keycloak-authorization like so
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-keycloak-authorization</artifactId>
<version>2.12.3.Final</version> <!-- last quarkus version that used keycloak 18 -->
</dependency>
but it's still the same thing? I'm using a managed Keycloak server - Cloud IAM (Free tier) so any server upgrades wouldn't be easy. Or are there any alternative configurations I can use with Keycloak to make the client backward compatible, the error I'm encountering is the following during application startup
2022-10-18 06:50:14,785 ERROR [io.qua.run.Application] (Quarkus Main Thread) Failed to start application (with profile prod):
org.keycloak.authorization.client.util.HttpResponseException: Unexpected response from server:
401 / Unauthorized / Response from server: {"error":"unauthorized_client","error_description":"Invalid client secret"}
at org.keycloak.authorization.client.util.HttpMethod.execute(HttpMethod.java:95)
at org.keycloak.authorization.client.util.HttpMethodResponse$2.execute(HttpMethodResponse.java:50)
I was able to resolve the issue by overriding quarkus bom in pom.xml through <dependencyManagement/> like so, the keycloak pom should be above quarkus for it to override
<dependencyManagement>
<dependencies>
<!-- retain compatibility with keycloak 18 -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-keycloak-authorization</artifactId>
<!-- last quarkus version that used keycloak 18 -->
<version>2.12.3.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependencies>
</dependencyManagement>
I get a warning during mvn compile though, but as per this Issue #1577, it is intended
[io.quarkus.maven.MavenDevModeLauncher$Builder] io.quarkus:quarkus-bootstrap-maven-
resolver::jar classpath entry C:\Users\Vince\.m2\repository\io\quarkus\quarkus-
bootstrap-maven-resolver\2.13.2.Final\quarkus-bootstrap-maven-resolver-2.13.2.
Final.jar was overriden with C:\Users\Vince\.m2\repository\io\quarkus\
quarkus-bootstrap-maven-resolver\2.12.3.Final\quarkus-bootstrap-maven-resolver-
2.12.3.Final.jar
Hope it can help others that were encountering the same issue, and take a quick read on the PR above, overriding the BOM versions can cause nasty unintended side effects.

SQL Server/Spring Boot: PKIX path building failed: unable to find valid certification path to requested target

I install SQL Server on my local computer and embedd it into my Spring Boot application. After starting Tomcat I get the following error:
'PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target'. ClientConnectionId:85411829-6853-4fdb-9373-b4c93e1d5e8f
I know that this error is well documenteted. I followed many guides and read much about it, but all advices I found did not fix my issue.
What I had done:
Download a random SSL-certificate from a website and add it to the
cacert file in the Java directory (descriped here).
Configure Spring Boot for SQL Server (descriped here)
Configure SSL Encryption for SQL Server (descriped here)
No one of these advices fixed the error. The only thing I realize is that if I set spring.jpa.hibernate.ddl-auto in my application.properties to none the program shows the error message, but it did not abort running.
The application.properties looks like this:
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=Car
spring.datasource.username=admin
spring.datasource.password=password123
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
server.port=8443
server.ssl.key-alias=selfsigned_localhost_sslserver
server.ssl.key-password=changeit
server.ssl.key-store=classpath:ssl-server.jks
server.ssl.key-store-provider=SUN
server.ssl.key-store-type=JKS
My dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
In SQL Server I create a database with tables and data in it.
Did someone of you have an further advice how to fix this error?
I faced the same issue with spring boot 2.7.4
and it Seems from the comments you're using driver 10.2.X
it turns out that since 2.7.0 the JDBC Driver 10.2 for SQL Server is used
instead of 9.4.1.jre8 for 2.6.x
So you've 1 of 2 solutions that worked for me:
1. Use the older version of mssql-jdbc driver
<properties>
<mssql-jdbc.version>9.4.1.jre8</mssql-jdbc.version>
</properties>
2. Or ask the driver to just trust the whatever the Sql server certiticate is
you can do so by adding this to the connection string:
jdbc:sqlserver://hOSt:pORt;databaseName=dbName;encrypt=true;trustServerCertificate=true

Spring boot and Zuul proxy DeferringLoadBalancerExchangeFilterFunction Error

I have a simple app that uses Netflix Zuul as an API gateway
I added the Zuul dependency in the pom.xml file as follows:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
and #EnableZuulProxy for the main class of the app
The problem is that whenever I try to run the API, It fails to start and shows in the console:
Consider defining a bean of type
'org.springframework.cloud.client.loadbalancer.reactive.DeferringLoadBalancerExchangeFilterFunction'
in your configuration.
I couldn't solve the issue, what's the problem?
The issue was solved when I added the following dependency in pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
and as a result, spring also asked me to add this to my app configuration:
spring.main.web-application-type=reactive
Then my API ran successfully without failing or throwing any exception

ForwardedHeaderFilter no longer support IPv6

After upgrading spring boot version to 2.4.9, the spring boot application works fine only for ipv4 but throws an error for ipv6. The app is deployed on google cloud run service. Current versions:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-dependencies</artifactId>
<version>2.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Error:
java.lang.IllegalArgumentException: Invalid IPv4 address: 2409:4042:2319:a971:c4db:4661:939c:443b
at org.springframework.web.util.UriComponentsBuilder.parseForwardedFor(UriComponentsBuilder.java:363)
at org.springframework.web.filter.ForwardedHeaderFilter$ForwardedHeaderExtractingRequest.<init>(ForwardedHeaderFilter.java:246)
at org.springframework.web.filter.ForwardedHeaderFilter.doFilterInternal(ForwardedHeaderFilter.java:149)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:190)
I was able to find a similar issue but did not find any solution.
https://github.com/spring-projects/spring-framework/issues/26748
The line in the code that throws the error
https://github.com/spring-projects/spring-framework/blob/6c68419073b43fb29114a3af4e402c729084ed84/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java#L363
The issue is due to google cloud run service not respecting RFC7239 standards and not due to spring boot. Cloud run fails to enclose IPv6 addresses with quotes and square brackets(as per RFC7239 standards) due to which spring interprets it as an IPv4 address and fails while parsing port number from the address.
This is very strange that google cloud fails to follow the standard. As of today, the Google cloud team says they are working on the fix but ETA is not guaranteed.
You can track the status here:
https://issuetracker.google.com/issues/184230536?pli=1

Use pivotal cloud foundry rabbitmq and mysql service using vcap service for spring data jpa application

I am able to use RABBITMQ and MYSQLSERVICES which is on pivotal.While binding services I am able to get the Credentials and using that credentials in my application.properties for spring data jpa project.
But this configuration that I am using is hard-coded in application.Properties To Make this configuration dynamically I came to know that we can use vcap services provided by pivotal.
So want to use run-time credentials for rabbimq and mysql.
My Code is below for reference.
File: application.propeties
rabbitmq.host=hostname
rabbitmq.virtual-host=vhost
rabbitmq.username=username
rabbitmq.password=password
rabbit.mainqueue=queue name
rabbit.errorqueue=erro queue name
spring.datasource.url=jdbc:mysql://hostname:postno
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
server.port=8000
The below is the repository file
package com.redistomysql.consumer.repo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface tblemployee_personal_infoRepository extends JpaRepository<tblemployee_personal_info, Long> {
}
Any help would be appreciated.
The link for reference **http://www.java-allandsundry.com/2016/05/approaches-to-binding-spring-boot.html**
Set this configuration in application-cloud.yml for Mysql
---
spring:
datasource:
url: ${vcap.services.mydb.credentials.jdbcUrl}
username: ${vcap.services.mydb.credentials.username}
password: ${vcap.services.mydb.credentials.password}
The config for rabbitMq:
System.getEnv("VCAP_SERVICES")
The dependencies in pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-spring-service-connector</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
<!-- If you intend to deploy the app on Cloud Foundry, add the following -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-cloudfoundry-connector</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-heroku-connector</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>
**The manifest.yml**
---
applications:
- name: redistomysql-consumer
path: target/redistomysql-consumer-0.0.1-SNAPSHOT.jar
memory: 1024M
env:
JAVA_OPTS: -Djava.security.egd=file:/dev/./urandom
SPRING_PROFILES_ACTIVE: cloud
services:
- es-mysql-db
- es-consumer-rabbitmq-service
buildpack: https://github.com/cloudfoundry/java-buildpack.git
env:
JBP_CONFIG_SPRING_AUTO_RECONFIGURATION: '{enabled: false}'

Categories