Spring-Boot apps installed as Service in linux cannot share postgresql database - java

I have two different runnable Spring-Boot applications that share the same local postgresql database schema. If I start one while the other is running, the previous one crashes and stops immediatelly.
Both applications are runnable Spring-Boot Thymeleaf web applications. Application1 generates content and saves it into the database. Application2 reads it from the same database and generates export files.
Each application connects itself to the database with different credentials. User1 has the ownership of the tables and all privileges. User2 has all privileges on all the tables.
Both are also configured to run in different ports.
Application1 - application.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/database-name
username: application1-user
password: application1-password
server:
port: 8000
Application2 - application.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/database-name
username: application2-user
password: application2-password
server:
port: 8002
Each one of them is owned in the file system by user1 and user2 respectively.
When I run each of them in different terminals with their specific user through the command line they start gracefully, access the tables inside the database and work as expected.
Application1 - terminal 1
sudo su user1
java -jar application1
Application2 - terminal 2
sudo su user2
java -jar application2
They are both succcessfully installed as a Service on a Linux Debian server. Both of them can start individually as Service flawlessly. But when I run one as a Service while the other is already running as a Service, the active one crashes and stops completely.
sudo service application1 start
sudo service application1 status *(active)*
sudo service application2 start
sudo service application2 status *(active, application2 stops immediatelly)*
When the first application crashes and stops, no new line is written on the log, so I cannot know the reason why it stops or read any exception that might be thrown. However, my guess is that it has something to do with Linux and not with the applications themselves.
This is the only information I could find out from the Service log.
sudo service application1 status
(...)
Aug 06 07:02:07 server application1.jar[18990]: /var/apps/application1/application1.jar: line 214: 19001 Killed "$javaexe" "${arguments[#]}"
Aug 06 07:02:07 server systemd[1]: application1.service: main process exited, code=exited, status=137/n/a
Aug 06 07:02:07 server systemd[1]: Unit application1.service entered failed state.
This is the code I wrote inside the systemctl files.
Application1 - application1.service
[Unit]
Description=application1
After=syslog.target
[Service]
User=user1
ExecStart=/var/apps/application1/application1.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
Application2 - application2.service
[Unit]
Description=application2
After=syslog.target
[Service]
User=user2
ExecStart=/var/apps/application2/application2.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
How can I achieve running both at the same time as a Service? Is there something I am missing?
Thank you very much in advance for your time.

Based on the information you provided, it could just be that they are both launched with the default port, thus conflicting ?
add this to one application:
server.port = 8081

Related

SpringBoot2.2.1 deployment on cloud server always cannot run report error "Web server failed to start. Port 9090 was already in use."

I am new to SpringBoot and java.I have build a SpringBoot2 app,I want to deploy it in a cloud server. I have specify that port in Application-online.properties like
server.port=9090 but when I use IDEA MAVEN clean and package command to build a jar then copy to cloud server.
I use
nohup java -jar -Dserver.port=9090 -Dspring.profiles.active=online ./mall-0.0.1-SNAPSHOT.jar > /root/imooc_mall/mall.log 2>&1
then quickly the program exit. the mall.log report :
***************************
APPLICATION FAILED TO START
***************************
Description:
Web server failed to start. Port 9090 was already in use.
Action:
Identify and stop the process that's listening on port 9090 or configure this application to listen on another port.
[06:25 23:31:32.595] [INFO] [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] - Shutting down ExecutorService 'applicationTaskExecutor'
Noticably, 9090 is the second port I use, before that I use 8081 meet same error. Just like whatever I use any port,it always in use. And I have confirm that the 9090 and 8081 is not in use use netstat -tulpn and lsof -i :8081/9090 . I don't know what's the problem.Hope someone can give some ideals.Thanks advance!
The Environment are Centos7 and SpringBoot2.2.1, Java1.8,and Tomcat is SpringBoot in-build version
Another thing is that I have successed in starting the springboot app at first,But I use kill -9 to stop the program. After that the above errors occurs and never success .I wonder if that have any bad effect on the question.
But I am sure the port is not in use.
I still use SpringBoot 2.2.1.But not package it with built-in Tomcat.Instead, I install Tomcat server on the server. And Packaging SpringBoot application as war package. And I finally successed in running the application.

Dockerized Mac/Java app can't talk to localhost

MacOS + Docker (Version 17.12.0-ce-mac49 (21995)) here. I am trying to Dockerize an existing Spring Boot app. Here's my Dockerfile:
FROM openjdk:8
RUN mkdir /opt/myapp
ADD build/libs/myapp.jar /opt/myapp
ADD application.yml /opt/myapp
ADD logback.groovy /opt/myapp
WORKDIR /opt/myapp
EXPOSE 9200
ENTRYPOINT ["java", "-Dspring.config=.", "-jar", "myapp.jar"]
Here's my Spring Boot application.yml config file. As you can see it expects Docker to inject environment variables from an env file:
logging:
config: 'logback.groovy'
server:
port: 9200
error:
whitelabel:
enabled: true
spring:
cache:
type: none
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${DB_HOST}:3306/myapp_db?useSSL=false&nullNamePatternMatchesAll=true
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
testWhileIdle: true
validationQuery: SELECT 1
jpa:
show-sql: false
hibernate:
ddl-auto: none
naming:
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
properties:
hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.cache.use_second_level_cache: false
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: false
hibernate.hbm2ddl.auto: validate
myapp:
detailsMode: ${DETAILS_MODE}
tokenExpiryDays:
alert: 5
jwtInfo:
secret: ${JWT_SECRET}
expiry: ${JWT_EXPIRY}
topics:
adminAlerts: admin-alerts
Here's my myapp-local.env file:
DB_HOST=localhost
DB_USERNAME=root
DB_PASSWORD=
DETAILS_MODE=Terse
JWT_SECRET=12345==
JWT_EXPIRY=86400000
It's worth noting that above in the env file, I have tried localhost, 127.0.0.1 and 172.17.0.1 and all of them produce identical errors below.
Then I build the container:
docker build -t myapp .
Success! Then I run the container:
docker run -it -p 9200:9200 --net="host" --env-file myapp-local.env --name myapp myapp
...and I watch as the container quickly dies with MySQL connection-related exceptions (can't connect to the MySQL machine running locally). I can confirm that the Spring Boot app has no problem connecting to MySQL when it runs as an executable ("fat") jar outside of Docker, and I can confirm that the local MySQL instance is up and running and is perfectly healthy.
Unable to connect to database. }com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:590)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:57)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1606)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:633)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:347)
When I turn TRACE-level logging on, I see it is trying to connect to:
url=jdbc:mysql://localhost:3306/myapp?useSSL=false&nullNamePatternMatchesAll=true
So it does look like Docker is properly injecting the env file's vars into the Spring YAML-based config. So this doesn't feel like a config issue, moreover an isse with the container speaking to the MySQL port running on the Docker host.
Can anybody see where I'm going awry?
Accessing the host machine from within a container is not recommended. Usually it can be solved by wrapping service you need into a container and accessing it via container name.
There is no solution, there are only workarounds, you can use one of them:
On Mac you can access the host services using docker.for.mac.host.internal DNS name.
You need to set environment variable like this:
DB_HOST=docker.for.mac.host.internal
And refer to the DB_HOST from your connection string.
For more details see the documentation:
From 17.12 onwards our recommendation is to connect to the special
Mac-only DNS name docker.for.mac.host.internal, which resolves to the
internal IP address used by the host.
Note: Having --net="host" doesn't let you reach the host machine via localhost. localhost always points to local machine, but in case if it is invoked from within a container it points to the container itself.
So basically Docker app is not in the same network as the host you're running it from and that's why you can't access MySQL by pointing to localhost (because this is another network from Docker's point of view).
What you could try is to run docker with --net="host" option and then it will share the network with its host.
You can find better explanation on this issue in this topic From inside of a Docker container, how do I connect to the localhost of the machine?

LDAP configuration for mongo throws permission denied

I'm fairly new to MongoDB and LDAP. I'm trying to use LDAP to authenticate users to mongo. these are the steps I have done so far.
Created a saslauthd.conf file inside /etc folder which contains the following line:
ldap_servers: ldap://com.myldap.server
ldap_use_sasl: yes
ldap_mech: DIGEST-MD5
ldap_auth_method: fastbind
created a muxdir inside /var/run/saslauthd which now looks like /var/run/saslauthd/mux
set the permission to 755 using sudo chmod 755 /var/run/saslauthd
Modified the /etc/sysconfig/saslauthd to have the following
MECH=ldap
Uncommented the line on the same file which says:
DAEMONOPTS=--user saslauth
Now when i tried to test the authentication mechanism using the following command:
testsaslauthd -u username -p password -f /var/run/saslauthd/mux
I'm getting the following message:
connect(): Permission Denied
my work is based on this and this
Could anyone point out what i'm missing here? thanks in advance.
UPDATE:
I tried the test command with sudo like below:
sudo testsaslauthd -u username -p password -f /var/run/saslauthd/mux
And I'm getting the following:
connect() : Connection refused
Thanks for your question. I've enjoyed setting up my environment to try to reproduce the error. You'll be glad to heard that I don't think it's a difficult problem to overcome. However, I've probably spent more time than I desired setting up MongoDB, cyrus-sasl-md5, settings permissions, etc. when nothing is actually related with your problem, at least at a first glance.
Your problem (and I'm 90% sure) is either your saslauthd daemon is not running or it's not properly configured. Let's take a look at the following:
Check the service status. The output of service saslauthd status should be similar to mine, pasted below. Note some key values such as the location of the init script, /etc/init.d/saslauthd/ in my case; and the socket, /var/run/saslauthd/mux, the same file location you need to put in testsaslauthd [...] -f /var/run/saslauthd/mux command.
root#hectorvp-pc:~# service saslauthd status
● saslauthd.service - LSB: saslauthd startup script
Loaded: loaded (/etc/init.d/saslauthd)
Active: active (running) since Tue 2016-04-26 12:04:59 BST; 1s ago
Docs: man:systemd-sysv-generator(8)
Process: 11569 ExecStop=/etc/init.d/saslauthd stop (code=exited, status=0/SUCCESS)
Process: 11586 ExecStart=/etc/init.d/saslauthd start (code=exited, status=0/SUCCESS)
Memory: 2.0M
CGroup: /system.slice/saslauthd.service
├─11606 /usr/sbin/saslauthd -a ldap -c -m /var/run/saslauthd -n 5
├─11607 /usr/sbin/saslauthd -a ldap -c -m /var/run/saslauthd -n 5
├─11608 /usr/sbin/saslauthd -a ldap -c -m /var/run/saslauthd -n 5
├─11609 /usr/sbin/saslauthd -a ldap -c -m /var/run/saslauthd -n 5
└─11610 /usr/sbin/saslauthd -a ldap -c -m /var/run/saslauthd -n 5
Apr 26 12:04:59 hectorvp-pc systemd[1]: Starting LSB: saslauthd startup script...
Apr 26 12:04:59 hectorvp-pc saslauthd[11586]: * Starting SASL Authentication Daemon saslauthd
Apr 26 12:04:59 hectorvp-pc saslauthd[11606]: detach_tty : master pid is: 11606
Apr 26 12:04:59 hectorvp-pc saslauthd[11606]: ipc_init : listening on socket: /var/run/saslauthd/mux
Apr 26 12:04:59 hectorvp-pc systemd[1]: Started LSB: saslauthd startup script.
Apr 26 12:04:59 hectorvp-pc saslauthd[11586]: ...done.
If the service is not running, just start it with service saslauthd start and check the status again (service saslauthd status) to check any possible upstream error.
It's also likely your ldap server is not running or missconfigured. You can take a look to the service status as above (service slapd status).
Please, try this and tell us about the outcome.
EDIT (26/04/2016): From the conversation in the comments of this answer, I've extracted some more steps. Please, apologize for the extensive conversation below the answer, its summarized here:
Debug saslauthd service: As indicated here, this service uses the system logs. In my case (Ubuntu) those logs are in /var/log/syslog but they might be in /var/log/messages in your case. At least by default. Look at this logs at the time you try to start the service and see if you see any error message that might give you some further insights about what the problem is.
The error appearing in /var/logs/messages was: could not bind to socket : /var/run/saslauthd/mux , bind: address already in use.
We checked the mux socket using the file command: file /var/run/saslauthd/mux and the output said it was a directory. It should be a socket. Then we removed it and restarted the service. Now the service works.

Where to see console log in Openshift?

Recently, I've deployed my JSP project into Openshift server. Now my wish is to see the Console log.
Suppose, if I print System.out.println("Message"); into my JSP project, how do I see that message printed into Console log in Openshift server?
EDITED:
DL is deprecated, please use Fiddle
rajendra # http://code-programmersplace.rhcloud.com/
(uuid: 54d19a5be0b8cd9bf9000082)
-------------------------------------------------
Domain: programmersplace
Created: Feb 04 9:34 AM
Gears: 1 (defaults to small)
Git URL: ssh://54d19a5be0b8cd9bf9000082#code-programmersplace.rhcloud.com/~/gi
t/rajendra.git/
SSH: 54d19a5be0b8cd9bf9000082#code-programmersplace.rhcloud.com
Deployment: auto (on git push)
jbossews-2.0 (Tomcat 7 (JBoss EWS 2.0))
---------------------------------------
Gears: 1 small
You have access to 1 application.
C:\Users\rajendra>
The first thing you need is to connect via SSH to your application on OpenShift. If the name of your app is awesome, run the following command:
rhc ssh -a awesome
If you've forgotten the name of your application, execute rhc apps in order to see your current apps. See the lines with something similar to hereisthename # http://... or .../~/git/hereisthename.git/.
Once you're connected via SSH, you can see the log using the tail command:
Tomcat 7 (JBoss EWS 2.0)
tail -f -n 100 app-root/logs/jbossews.log
JBoss Application Server 7
tail -f -n 100 app-root/logs/jbossas.log
The OpenShift Client Tools are required. See Installing the OpenShift Client Tools. See also Getting Started with OpenShift Online.
RELATED: rhc ssh [No system SSH available] error

h2 cluster with file based database

I have set up an h2 cluster but cannot connect via the console or using a datasource all I get is this:
IO Exception: "java.io.IOException: The filename, directory name, or volume label syntax is incorrect"; "E:/baseDirDefinedInServerConnection/myDB,localhost:1112/myDB" [90031-176] 90031/90031 (Help)
I have configured 2 servers thus:
java -cp h2-1.3.167.jar org.h2.tools.Server -tcp -tcpPort 1111 -tcpAllowOthers -baseDir E:\myBaseDir
at tcp://myIp:1111 (others can connect)
java -cp h2-1.3.167.jar org.h2.tools.Server -tcp -tcpPort 1112 -tcpAllowOthers -baseDir E:\myBaseDir\server
at tcp://myIp:1112 (others can connect)
So you see I have one database in a directory (this has been created) and another database in another directory. Both are up and running.
I have run the cluster tool thus:
java -cp h2-1.3.167.jar org.h2.tools.CreateCluster -urlSource jdbc:h2:tcp://localhost:1111/myDB -urlTarget jdbc:h2
:tcp://localhost:1112/myDB -user username -password pass -serverList localhost:1111,localhost:1112
And it all looks good. If I try to connect thorugh the console without the cluster list I get this message, which proves we are in clustered mode, which is good:
Clustering error - database currently runs in cluster mode, server list: 'localhost:1111,localhost:1112'" [
I have checked the permissions on the directories and all has read/write access.
Yes this is a windows machine.
Using H2 version:
Bundle-Vendor: H2 Group
Bundle-Version: 1.3.167
Any ideas what I might have done wrong?
Thanks for reading.
Guess you already found out that one should connect like this
jdbc:h2:tcp://localhost:1111,localhost:1112/myDB

Categories