How to let your spring boot pick up external property file

You may already see that ./config/application.properties can override the default spring boot application.properties settings. But when you deploy your app to tomcat and with many other web app together, then you need to have unique properties files foe each app and also make external property file be picked up by web app.

So let us do something like this:

  1. Change in your java main code to let your app use a unique properties file instead of application.properties.
@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        System.setProperty("spring.config.name", "myapp");
        SpringApplication.run(MyApplication.class, args);
    }
}

For war version of tomcat:

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyApplication.class).properties("spring.config.name: myapp");
    }

}

Then name your properties file like: myapp.properties, instead of the default name.

2. Now let us also enable the external property file to override the default myapp.properties.

in Spring boot, it is just ./config/myapp.properties

For tomcat version, we need to add env variable to do this. Set a spring_config_location environment variable pointing to the folder that contains your myapp.properties file. In the case of Tomcat you can do this by adding the following line to your <TOMCAT_HOME>/bin/setenv.sh file (create the file if missing):

export spring_config_location=<TOMCAT_HOME>/conf/

Place the myapp.properties properties file in that folder <TOMCAT_HOME>/conf/.

So every app will looking into the conf folder to look for their unique properties file to load.

 

Refer to:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

 

 

Advertisements

Java native memory tools and usage related

 

https://dzone.com/articles/java-memory-and-cpu-monitoring-tools-and-technique

 

https://www.ibm.com/developerworks/library/j-nativememory-aix/

https://stackoverflow.com/questions/12797560/command-line-tool-to-find-java-heap-size-and-memory-used-linux

https://dzone.com/articles/java-memory-and-cpu-monitoring-tools-and-technique

 

 

https://stackoverflow.com/questions/2756798/java-native-memory-usage

 

 




java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'

Issue: An irrecoverable stack overflow has occurred.

Issue:

An irrecoverable stack overflow has occurred. Please check if any of your loaded .so files has enabled executable stack (see man page execstack(8))

Reason:

This issue happens when I call a c++ So lib from java by JNA. This mostly caused by input a big chunk of data into the function interface of the C++ function. There is a limit of the Thread Stack Size defined for java’s thread. Normally is 512K-1024K.

 

And by change the Xss size you could change the stack size. but you should be careful on this when you have many threads need to open.

 

 

Refer http://xmlandmore.blogspot.com/2014/09/jdk-8-thread-stack-size-tuning.html

 

How to create doc for C++/JAVA code in eclipse

The common tools that we used in the eclipse is deoxygen

http://www.stack.nl/~dimitri/doxygen/manual/install.html

The good point for this one is that it can be used in both JAVA and C/C++ project. So for misc projects, this one maybe the best choice. In fact, it is really hard to find a workable free UML tool in Eclipse C++ to turn your code into the class hierarchy diagram. But this one can give this feature plus the auto index the docs.

Usage for it also is very easy.

1. Just install it in my ubuntu.

sudo apt-get install doxygen

2. And then in the eclipse install one plugin for doxygen: Eclox

3. Under the eclipse project create a folder of doc.

4. New -> other file-> others….. to create a doxyfile under this folder. Then you can use context menu to edit this file and make your selection for configuration for doxygen.

5: Common configurations summary here:

Input of source files folder (h, cpp, java etc.)

output folder

HTML with search and frame selected

Diagrams: use dot tool from the GraphViz….

Extraction mode: all entities and with crosss reference source-code…

6. Just use context menu “build documentation”, then you will get html/latex/rtf etc version docs for your code.

7. Then find the index.html in the created doc html folder to read the docs.

 

 

 

 

 

Swagger for Spring Boot How To

This is the note about how to make the swagger work together with Spring Boot. Swagger here is used as great tool to automatically create the Restful API html docs and testing API web UI.

1. Add swagger2 and UI to POM.XML

<dependency>
 <groupId>io.springfox</groupId>
 <artifactId>springfox-swagger2</artifactId>
 <version>2.6.1</version>
 <scope>compile</scope>
 </dependency>

<dependency>
 <groupId>io.springfox</groupId>
 <artifactId>springfox-swagger-ui</artifactId>
 <version>2.6.1</version>
 <scope>compile</scope>
 </dependency>

2. Create a class for spring boot configuration for swagger:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static springfox.documentation.builders.PathSelectors.regex;

@EnableSwagger2
@Configuration
public class SwaggerConfig {

@Bean
 public Docket productApi() {
 return new Docket(DocumentationType.SWAGGER_2).select()
 .apis(RequestHandlerSelectors.basePackage("com.wayneshare.springboot.swaggerexample"))
 .paths(regex("/rest.*")).build().apiInfo(metaInfo());
 }

private ApiInfo metaInfo() {

ApiInfo apiInfo = new ApiInfo("WayneShare RestfulAPI", "Enable knowledge sharing ...", "1.0",
 "Terms of Service", new Contact("WayneShare", "https://www.wayneshare.com", "zhou_xiaowei@yahoo.com"),
 "", "");

return apiInfo;
 }
}

3. Then you can start run the application and goes to this URL http://localhost:8080/swagger-ui.html

You will see the swagger API html docs and can test over there.

4.  Try to change this part of code in config to see changes on the UI, then you will know the meta data reflecting on UI.

ApiInfo apiInfo = new ApiInfo("WayneShare RestfulAPI", "Enable knowledge sharing ...", "1.0",
 "Terms of Service", new Contact("WayneShare", "https://www.wayneshare.com", "zhou_xiaowei@yahoo.com"),
 "", "");

5. If you want to change the grouping name and description for one controller, then use @Api for you controller:

@RestController
@RequestMapping("/rest/account")
@Api(tags = "API 02", description = "Account info management")
public class AccountController {

6. Check these annotations too in case you want to change some meta on UI  and even the http return code desc. @ApiModelProperty @ApiOperation @ApiResponses

@ApiOperation(value = "Returns Authentication Result")
 @ApiResponses(value = { @ApiResponse(code = 200, message = "Successful Authentication") })

7. You can also run the testing for each API. Here is the final result I have done for a sample project:

Screenshot from 2018-04-27 10-41-10

Enjoy it and credit goes two these two links

https://github.com/TechPrimers/spring-boot-swagger-example

https://github.com/indrabasak/swagger-deepdive/wiki/Renaming-and-Sorting-REST-Controllers

Spring boot actuator – system monitoring and management

I am using the 1.5.10 as the example, detailed docs can see at here:

https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/html/production-ready.html

1. Just add starter-actuator to maven

<!– for logging and minitoring =============================== –>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2. Restart and suppose my app running at the 8080 port, you can see the actuator works at http://localhost:8080/health

{
  "status" : "UP",
  "diskSpace" : {
    "status" : "UP",
    "total" : 217631211520,
    "free" : 20183724032,
    "threshold" : 10485760
  },
  "rabbit" : {
    "status" : "UP",
    "version" : "3.5.7"
  },
  "db" : {
    "status" : "UP",
    "database" : "MySQL",
    "hello" : 1
  }
}

actuator gives you a lot data about your system, such as metrics, health, configuration, http trace ….FULL list can see from here ID List

3. Some ID will be blocked by spring security if your system using it. So you have to authenticate and get the token or get the role to access them. But if system is behind a firewall, you can disable the security for actuator. Let us change the actuator to running in a different port and using new path root to access it. Add these into the application.properties file:

management.port = 9091
management.address = 127.0.0.1
management.security.enabled=false
management.context-path=/manage
endpoints.jmx.enabled=false

So we can access http://localhost:9091/manage/health  now to get data.

4. Based on actuator feature, you can monitor and manage the system performance and health,  logging, and many others.  And use actuator API to process data by your self.

5. There are some GUI feature has been done based on the actuator already. like this one:

spring-boot-admin

Try to use it for your system!