Spring boot embedded tomcat threadpool configuration - java

Application is built using spring boot.My application uses application.yaml for external config .But when i am trying to add below config in application.yaml , application fails to start with error as tomcat is not valid.However similar equivalent config I have tried in another application with application.propeties it works there.
server:
port:8080
tomcat:
max-threads:500
accept-count:500
max-connections:10000
min-spare-threads:500

What is the springBoot version being used in the other application ?
FYI
server.tomcat.max-threads is deprecated since Springboot 2.3, now use server.tomcat.threads.max in your Spring application.properties.
https://docs.spring.io/spring-boot/docs/2.4.0/reference/html/appendix-application-properties.html#common-application-properties

For me this seems to work in application.yaml
server:
port: 8080
tomcat:
max-threads: 500
accept-count: 500
max-connections: 10000
min-spare-threads: 500
or this in application.properties
server.port = 8080
server.tomcat.max-threads = 500
server.tomcat.accept-count = 500
server.tomcat.max-connections = 10000
server.tomcat.min-spare-threads = 500
If it doesn't work, maybe there is an error in your dependencies?

Related

Cannot load keystore from system environment

I have a simple spring boot application running on HTTPS with SSL enabled
application.properties
server.port=8443
server.ssl.key-store=${SERVER_KEYSTORE}
server.ssl.key-store-password=${SERVER_KEYSTORE_PASSWORD}
where SERVER_KEYSTORE and SERVER_KEYSTORE_PASSWORD are system env variables
I generate a jar using mvn clean package and then run the executable jar. But I get the following error
***************************
APPLICATION FAILED TO START
***************************
Description:
The Tomcat connector configured to listen on port 8444 failed to start. The port may already be in use or the connector may be misconfigured.
Action:
Verify the connector's configuration, identify and stop any process that's listening on port 8444, or configure this application to listen on another port.
I have confirmed that port 8443 is not in use by any application.
Now, when I rewrite application.properties as
server.port=8443
server.ssl.key-store=/etc/ssl.keystore
server.ssl.key-store-password=secret
The application runs fine on port 8443 with HTTPS. But I don't want to have the path and password part of my codebase.
I even tried specifying the complete path to the env file as
server.ssl.key-store=/etc/environment/${SERVER_KEYSTORE}
server.ssl.key-store-password=/etc/environment/${SERVER_KEYSTORE_PASSWORD}
It still gives the same error
Some help will be highly appreciated. Thanks
The following error clearly says that tomcat cannot stat on 8444 port. If you want to verify, open command prompt and type the command netstat -a. This will give you which are the ports already occupied. You can run in different ports like 8090, 8091 etc. Besides, if you want to know more details, start spring boot in debug mode using your ide.
The Tomcat connector configured to listen on port 8444 failed to
start. The port may already be in use or the connector may be
misconfigured.
So I guess your question is how to read env properties in Spring application.properties.
Try this : https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/maven-plugin/examples/run-env-variables.html

Spring boot application with two services on two different ports

I have a spring boot application with two services but i need to run one on port 8080 and the other on 8081. Now i'm developing with sts (Spring tool suite) and i run the application with the option "Run as spring boot app", so i don't know where change server configurations. Someone can help me?
You can't run two different services under the same spring boot application in two different ports. If you want you can move one service to another spring boot application.
But port number will not be same for both services.
you can configure SprintBoot support two ports.The common method configure is use application.properties or the application.yaml as #Madhu Bhat answer. In SprintBoot to configure another port code like this:
create a connector
int port = Integer.parseInt(probePort);
Connector httpConnector = new Connector(HTTP_PROTOCOL);
httpConnector.setPort(port);
Http11NioProtocol handler = (Http11NioProtocol) httpConnector.getProtocolHandler();
handler.setMaxThreads(10);
handler.setMinSpareThreads(4);
//handler.setAddress(InetAddress.getLocalHost());
handler.setAddress(StringTool.getInetAddress());
return httpConnector;
configure connector to
((TomcatEmbeddedServletContainerFactory) container).addAdditionalTomcatConnectors(connector);
The port can be defined on the application.properties or the application.yaml configuration file that you use.
In application.properties file, define it as below:
server.port=8090
Or in case you are using a application.yaml config, define it as below:
server:
port: 8090
If you use Docker (most common solution), you can add the port or its full address as an Environment Variable
docker-compose.yml file like this:
application1:
image: 'application1:latest'
build:
context: ./
container_name: application
environment:
- HOST-APP2=localhost:8082
ports:
- 8091:8080
application2:
image: 'application2:latest'
build:
context: ./
container_name: application
environment:
- HOST-APP1=localhost:8081
ports:
- 8092:8080
or straight in the Dockerfile while building containers
check out here : https://vsupalov.com/docker-arg-env-variable-guide/
it's a good article
you can write below line in application.properties or application.yml
server.port=8080
Sure. You can do it in the application.properties file of spring boot project via setting server.port=number for each service.

spring boot Eureka microservice discovery and registration fails in docker

I created a eureka server microservice and a normal microservice , usually when I run both the service separately using STS tool service discovery and registration works fine , but whenever i tried to launch in docker that time registration fails.
Code is below -
Eureka Server
application.properties
spring.application.name=eureka-service
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.instance.hostname=eureka-service
eureka.instance.prefer-ip-address=true
eureka.server.wait-time-in-ms-when-sync-empty=0
Dockerfile
FROM openjdk:8
ADD target/SpringEureka-1.0.jar SpringEureka-1.0.jar
EXPOSE 8761
ENTRYPOINT ["java","-jar","SpringEureka-1.0.jar"]
Normal Microservice
application.properties
server.port: 8010
spring.application.name=eureka-client
eureka.client.register-with-eureka=true
eureka.client.fetch-registry= true
eureka.client.service-url.default-zone=http://localhost:8761/eureka/
eureka.client.proxy-host=localhost
eureka.instance.prefer-ip-address=true
Dockerfile
FROM openjdk:8
ADD target/HystrixTest-1.0.jar HystrixTest-1.0.jar
EXPOSE 8010
ENTRYPOINT ["java","-jar","HystrixTest-1.0.jar"]
Are you link the HystrixTest container to Eureka Server container?
You can get more detail in here: https://docs.docker.com/network/links/
The better question if you post docker-compose file or your cmd when starting the container.
I think everything that you did is all right you just need to enable the ports using the
docker run -P 8761:8761 <your_docker_image> command.
And change also the configuration from eureka.instance.hostname=eureka-service to eureka.instance.hostname=localhost

Java: Spring boot Tomcat Access Logs are not created at Tomcat Start

I want to access tomcat logs of my service which is build in spring boot having embedded tomcat 8 in it.
I have written following code in bootstrap.yml of my service.
server:
port: 8320
context-path: /luggage-service
tomcat:
accesslog:
directory: "/var/log/tomcat"
enabled: true
pattern: "{\"Hostname\":\"%h\", \"Logical username from identd\":\"%l\", \"Remote user\":\"%u\", \"Date&Time\":\"%t\", \"HTTP Status code\":\"%s\", \"Bytes Sent\":\"%b\", \"Time taken to process the request\":\"%D\", \"Local IPAdderss\":\"%A\", \"Local port\":\"%p\", \"Time taken to Commit the Request\":\"%F\", \"Request Method:%m User SessionID\":\"%S\", \"Requested URL path\":\"%U\", \"Local ServerName\":\"%v\"}"
When i build and run my service, then a file "access_log.yyyy-mm-dd.log" is created and when i hit calls of my service,then no logs are created in log file.
Logs are created when i stop my tomcat.
What changes do i need to do in my code?
Could you try:
server.tomcat.accesslog.buffered=false

Spring Boot not obeying command line arguments

Put together a very basic Spring Boot RESTful backend with some CRUD endpoints. Trying to start it up on my Centos7 server. However I already have an application listening on its default port, 8080, so I have to configure that.
I have tried:
java -jar target/rest-1.0-SNAPSHOT.jar --server.port=8090
java -jar target/rest-1.0-SNAPSHOT.jar --Dserver.port=8090
java -jar target/rest-1.0-SNAPSHOT.jar --port=8090
java -jar target/rest-1.0-SNAPSHOT.jar --Dport=8090
My application.properties contains the line:
server.port=${port:8090}
Spring Boot still starts up the embedded Tomcat container on port 8080. It's not getting the message. What am I missing?
Edit: setting SERVER_PORT=8090 was effective - see below
Try with -Dserver.port=8090 note single -
or
server.port=xxxx in application.properties file
EDIT:-
Check your log, below line will echo in console 2015-10-16 23:13:23.082 INFO 4228 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8090 (http)
VM arg overrides application.properties
If you have a "application.yml" or "application.properties" in the same folder of jar, the jar will take preference of the external configuration file.
Doesnt make sense for me, but its true. The springboot has higher priority to external configuration file instead the internal one.
We have four different ways to change default port.
Using application.properties or application.yml
Implementing EmbeddedServletContainerCustomizer interface
Using SpringApplication class
Change port directly through command line
For more details, visit below
http://www.javainterviewpoint.com/spring-boot-change-embedded-tomcat-default-port/

Categories

Resources