In this post, I will demonstrate three simple ways to read properties files into spring boot applications,including the @Value ,@Configuration and @PropertySource annotations.
2. Environments
springboot
java 1.8+
3. Way 1: Read from application.properties by @Value annotation
Add these properties to your default configuration file:
Use @Value like this:
Run the code ,we get this:
4. Way 2: Read from application.properties by @ConfigurationProperties annotation
Instead of @Value annotion, you can use @ConfigurationProperties, which can read some specific prefixed properties from your application.properties to your class properties.
Add these properties to your spring boot application.properties:
Add a class which use @ConfigurationProperties like this:
Then print properties like this:
Run the code ,we get this:
5. Way 3: Read from your custom xxx.properties by @ProperySource annotation
By reading previous sections, you can see that by using @Value or @ConfigurationProperties, it’s easy to read properties in springboot’s default application.properties, but what should we do if we want read properties from a custom properties file?
According to this document,Spring provides @PropertySource annotation to read custom file properties.
Create a new file named sysconfig.properties in src/main/resources/ , add these properties to it:
Create a new class to use the @PropertySource like this:
Print the custom properties like this:
Run the code ,we get this:
6. Conclusion
You can use @Value ,@Configuration to read default application.properties or use the @PropertySource in your custom properties file in SpringBoot apps.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem.
So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by
email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
In this post, I will demonstrate how to resolve the 502 bad gateway error when using proxy_pass in Nginx.
2. Environment
Nginx 1+
3. The nginx.conf
We can define an upstream to reverse proxy local Tomcat services running on localhost as follows:
The service layout:
When a user navigates to http://xxx.com/myservice, the request will be reverse proxied to the Tomcat instances running on localhost (ports 8081 and 8082).
4. The problem
When your business grows, the number of concurrent HTTP requests may surge in a short period, as a result, your users might encounter an error like this when attempting to access your services:
5. How to resolve this 502 bad gateway problem?
5.1 Why did this happen?
Default HTTP 1.0 connections and requests:
You can see that every request create a new connection,so your server would suffer from the surge requests.
The HTTP 1.1 connections and requests:
You can see that there are only a few connections between the nginx and local service, many of them are reused between HTTP requests.
5.2 Try to use HTTP Keep-Alive feature
Change your server settings in nginx.conf, try to activate HTTP 1.1 keep-alive features like this:
The explanation:
proxy_HTTP_version 1.1 indicates the use of HTTP 1.1, as the keep-alive feature is a default feature in HTTP 1.1 protocol.
proxy_set_header Connection "" is used to clear the client’s connection headers and to utilize the HTTP/1.1 header.
5.3 Set the maximum number of idle Keep-Alive connections for the upstream server
Change your upstream settings like this:
And,
6. The final nginx.conf
Here is the final nginx.conf file content:
6. Summary
You should always try to use the keep-alive feature of HTTP, it will help you to obtain good user experience and good revenue too.
In this post, I will demo how to install wrk, a web url performance test tool on linux/unix/mac systems.
2. The solution
2.1 How to install wrk?
On linux systems, to ensures that your package manager (APT) has the most up-to-date information about available packages, versions, and dependencies, we should update the local cache first:
Then we install the wrk tool using apt install command:
You can see that we got an error E: Unable to locate package wrk, why?
And wrk can only run on Unix-like systems. Such as linux, mac, solaris, etc. It can only be compiled on these systems.
So we can install it from source:
Download wrk source code:
Then build it:
Then we can use the binary:
2.2 Verify the installation of wrk
It works.
3. Summary
In this post, I demonstrated how to install wrk , a great performance test tool . That’s it, thanks for your reading.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem.
So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by
email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
This post will show you how to solve ‘BPF’ object has no attribute ‘get_syscall_fnname’ when run bpf program in linux ?
Problem
When you run a python bpf_program in linux, you run this command:
For example, if our bpf program’s name is example.py:
The example.py content is:
You get this error:
The error AttributeError: 'BPF' object has no attribute 'get_syscall_fnname' indicates that the BPF class from the bcc module does not have a method named get_syscall_fnname.
Environment
You check your os version by this command:
Python version:
Solution: Install the bcc dependencies
According to python bcc documents, you should install the libbcc and python bcc into system.
what is libbcc?
and what is python-bcc:
After all done, you can run the python bpf script again:
It works!
Final Words + More Resources
My intention with this article was to help others who might be considering solving such problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
When build springboot app as war file, sometime we encounter this exception:
The error shows that :
it needs a webxml attribute to construct the WAR file
Or you should provide a WEB-INF/web.xml to build the WAR file
2. Environments
spring boot 1.x and 2.x
3. The solution
3.1 The original pom.xml
Before solve , we can look at the original pom.xml
We use the spring-boot-maven-plugin to execute the goal repackage with exec classifier.
The classifier, which uses exec here, indicating that the generated jar file will have a -EXEC suffix.
In summary, this configuration uses spring-boot-maven-plugin plug-in to reintegrate the project and generate a executable jar file with the -EXEC suffix.
But, we want to WAR file to be generated, not a executable jar file!
So , we need to change the pom.xml to generate a WAR.
3.2 The right pom.xml
We should add maven-war-plugin to build springboot as a WAR file:
Please pay attention to the version of the maven-war-plugin, it must be 3.0.0+, or else you must add as follows:
Hope it helps for you.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: [email protected].
Here are also the most important links from this article along with some further resources that will help you in this scope: