Deploy Spring Boot application on AWS (Elastic Beanstalk) that uses RDS - Application does not start - spring

I have a SpringBoot application connected to a MySQL database. This has already been installed on AWS and entered into Spring's properties file. The connection works and I can also connect to it with my workbench. But my problem is that the application does not run on AWS. I changed the SERVER_PORT to 5000 (also in the properties) but I can't get the APP to run
The AWS log file:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.backend</groupId>
<artifactId>SYFALL</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SYFALL</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties file:
server.port = 5000
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
spring.datasource.url = jdbc:mysql://AWS_DATABASE:3306/syfall
spring.datasource.username = xxx
spring.datasource.password = xxx
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
The application was built as a JAR file.

As stated in the comments, I have my sample Spring BOOT app that uses MySQL RDS data and queries and displays data perfectly.
I am currently in the process of deploying to Elastic Beanstalk. I will update this thread with notes once done.
UPDATE
I successfully deployed this Spring BOOT app to Elastic Beanstalk and got the RDS Connection working. I packaged up the Spring BOOT app into a JAR by using mvn package. Then I deployed the JAR onto Elastic Beanstalk.
Notes:
Be sure to specify your environment variables correctly in the Elastic Beanstalk environment (as opposed to a Spring config file) -- as shown here:
The AWS Creds are needed if your app uses an AWS Service Client. I use SES in this app so I need them
Now here is the important part. You need to set the Inbound rules so that a connection from the app deployed on EB to RDS can be made. I simply used this for testing....
To take this further, you can of course use the IP Address of your app on Beanstalk in your Inbound Rule. To do so, get the IP of your Elastic Beanstalk app. Then use that IP in your inbound rule.
Make sure that the Security Group your RDS uses is also the same Security Group your EB app uses. Both RDS and EB has to be in same region.

Related

PCF Unable to use Config Server and Service Registry together in an Application

I am trying to bind the PCF Config Server and PCF Service registry in the same Application and push the app . However I get an error while the application is starting . The problem is due to the application unable to send the Hertbeat information and it fails with unable to refresh cache.
If i bind config server alone and Service registry alone it works but when both the services are bound it fails.
Any help with this is appreciated
The dependencies are as follows
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-starter-service-registry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-starter-config-client</artifactId>
</dependency>
This was resolved after gave the Service registry to take the path using the following configuration
spring.cloud.services.registrationMethod=route

Access log not created

I'm trying to enable SpringBoot's embedded Tomcat access logs. When the application starts I'm not seeing the log file at all. When I make requests to the application from browser I still get nothing. What am I missing?
This should be really straight forward. I'm wondering if there's stuff in my project that's interfering with Tomcat logging to the access_log.log file. Within the project I've done the following:
enable ssl and changed port
Via #Configuration, I created a #Bean EmbeddedServletContainerCustomizer that adds a TomcatConnectorCustomizer.
Added Spring Security to request authentication for /secure/** URL patterns.
From what I've read none of that should require anything special regarding Tomcat access logs. With my set up I expect the logging to be in my-tomcat/access_log.log at the same location I ran the java -jar command. Correct me if that's wrong.
Using...
SpringBoot 1.5.9.RELEASE
Win 7 Enterprise
application.yml
server:
port: 8443
ssl:
# 6.2 Ensure SSLEnabled is set to True for Sensitive Connectors (Not Scored)
enabled: true
key-store: classpath:keystore.p12
key-store-password: password
key-store-type: PKCS12
key-alias: tomcat
# 6.5 Ensure SSL Protocol is set to TLS for Secure Connectors (Scored)
protocol: TLS
tomcat:
basedir: my-tomcat
accesslog:
enabled: true
pattern: '%t %a "%r" %s (%D ms)'
security:
require-ssl: true
# 7.1 Application specific logging (Scored)
logging:
level.com.esd.springbootdemo: DEBUG
# 7.2 Specify file handler in logging.properties files (Scored)
file: logs/springbootdemo.log
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.esd.springboot</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-demo</name>
<description>Spring Boot security and hardening POC</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Your tomcat YAML block is currently a child of the ssl YAML block - so you need to fix the indentation

Spring Boot project in IntelliJ IDEA stops working after reboot

This is probably a newbie question, but I am not able to sort things out.
I've created a Spring Boot JAR-based Maven project using the Spring Initializr interface in IntelliJ IDEA. Its dependencies on Web and JDBC; an embedded instance of Tomcat is used to run the project.
I also have a couple of other Maven dependencies I add without problems from the relevant dialog; I also manually add the Apache Phoenix thin client JAR, that includes a copy of Logback (I am not able to get their Maven repository to work, but I don't think this is relevant to this question).
I am able to run the project flawlessly; after a system reboot (I am on Windows), a cascade of errors follows. First and foremost an exception signaling a conflict between Logback comes:
Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.slf4j.impl.Log4jLoggerFactory loaded from [...]). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.slf4j.impl.Log4jLoggerFactory.
I would not like to alter the Phoenix client JAR; I am able to follow the instructions to replace the Spring dependency on Logback with another logger (log4j2 for example), but then the embedded Tomcat fails to create the container.
To get back to work, I have to recreate the project from scratch.
Could you please point me in the right direction to pinpoint the actual problem? Thank you.
Post scriptum: I am attaching the content of my pom.xml, apart from some potentially identifying information.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>[RETRACTED]</groupId>
<artifactId>[RETRACTED]</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>[RETRACTED]</name>
<description>[RETRACTED]</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Firstly, rather than adding the apache phoenix client (or any other dependencies) manually you should add them to your maven pom.xml, so maven resolves the dependency at build time. If your project has dependencies which maven is failing to resolve this is a separate issue which you need to resolve, but adding them manually is bad practice for a number of reasons.
Once you have added the phoenix client as a dependency, you need to explicitly exclude slf4j-log4j12 and log4j as part of the dependency declaration. The XML should look something like this:
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>4.13.1-HBase-1.3</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
The reason these need excluded is that they are conflicting with the versions bundled in the spring-boot project
Hopefully this will sort your problem.

Missing AWS properties when creating AWS application

I'm struggling to figure out how to add AWS properties to my application.properties (or application.yml) file, and I'm not sure what I have set up incorrectly in STS.
I can reproduce this creating a simple AWS app using Spring Initializr. I'm adding AWS, Consul and REST because that's what the real app is using. Here's the POM it generates.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.dkist</groupId>
<artifactId>staging-service-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>staging-service-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I'm not adding any code to the application for this sample, only the code that is generated. This was a test to see if something was wrong with the app I was working on. When I try to add an application property, nothing shows for AWS. The same is true if I create a YAML file.
If I force the issue, and add it anyway STS says the property is unknown.
Compiling the app throws an exception:
Caused by: java.lang.IllegalStateException: There is not EC2 meta data available, because the application is not running in the EC2 environment. Region detection is only possible if the application is running on a EC2 instance
The app is not running on an EC2 instance, it's running locally. From what I've read I need to add the aws.region.auto if it's not running on EC2, but I can't get the app to acknowledge the property exists. Same happens with the access key and secret key.
So... after lots of tinkering and reading other posts it looks like the properties will work if you add them, even if STS doesn't recognize them.
I added
cloud:
aws:
credentials:
instanceProfile: false
region:
static: eu-west-1
stack:
auto: false
and the program will run.
The other thing that was tripping me up is the inconsistency in the paths for the properties. For example Consul properties are at
spring.cloud.consul.*
where as AWS is at
cloud.aws.*
There's no "spring" to start the AWS properties. I'm sure there is a reason for the inconsistency, I just don't know it.

Running Spring Clound Config and Eureka on same server

We are trying to create a dashboard for our application as a single point of entry/configuration. For this we will build and UI and would like to run Spring Cloud Config and Eureka on same instance. Is there any reason why we should not do this and if not is it possible?
#mvlupan, there is nothing keeping you from using together. That is one of the reasons we created #EnableEurekaServer and #EnableConfigServer.
pom.xml snippet.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
</dependencies>
Application.java snippet
#EnableEurekaServer
#EnableConfigServer
#SpringBootApplication
public class DemoconfigandeurekaserverApplication { /*...*/}
application.properties snippet (so eureka and config server don't clash).
spring.cloud.config.server.prefix=/config
For high availability in production environment there should be at least 2 instances running on different servers.
Both ConfigServer and EurekaServer should have 2 instances.
Personally I prefer to see ConfigServer and EurekaServer running as one process as #spencergibb has shown.
There are config-first or discovery-first approches on what runs first.
P.S.
I expect that your Dashboard application will be separate from ConfigServer/EurekaServer,
so its own issues would not affect business critical parts.

Resources