Spring Boot/cloud Start Mode description

Source: Internet
Author: User
Tags web hosting

Spring BOOT/CLOUDSH's Java Open source framework, Spring Cloud is more focused on service registration and service governance, in general is what we call microservices, it is important to note that spring Cloud is a spring boot-based extended version, See the official documentation for more spring boot and Spring cloud documentation!

Spring Boot reference Documentation:
Https://qbgbook.gitbooks.io??? https://docs.spring.io/spring-boot/docs/current/reference/html/

Spring Cloud Reference Documentation:
Https://springcloud.cc??? Https://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html

一.关于spring boot/spring cloud 启动方式

Since spring boot/spring cloud is usually built with a. jar suffix and the embedded web hosting container, the spring boot/spring Cloud is launched in a very simple way (in the case of Tomcat):

 java -jar xxxxxx-0.0.1-xxx.jar --spring.config.location=/path/application.properties

This way, you can start the Spring boot/spring cloud app

二.关于spring boot/spring cloud 配置生效顺序优先级

Generally in a project, there will always be a lot of environments. For example, the production environment--environment--the pre-release environment, the environment--environment, the configuration files are always different, even in the development environment, each developer's environment may be a little differently, Spring Boot provides a mechanism of priority configuration reading to help us get out of this dilemma. In general, we all know that spring boot configuration is read from its corresponding application.properties (the default profile is named: application). The configuration is actually read from the Application.propert under the resource directory under its Jar package.

Managing configurations in real-world applications is not an easy task, especially if your application needs to be deployed to multiple environments. Typically, you will need to provide a corresponding property file for each environment to configure your own database connection information, server information, and third-party service accounts.

A common application deployment will include several environments such as development, testing, and production. There is an overlay relationship between the configurations of different environments. The configuration in the test environment overrides the development environment, and the configuration in the production environment overrides the test environment, so spring Boot provides a unified way to manage the configuration of the application, allowing developers to define different priority configuration values using the property file Yaml file, environment variables, and command-line parameters.

The configuration priority order provided by Spring Boot is more complex. In order of precedence from highest to lowest, the specific list (high to Low) is as follows:

1.命令行参数(优先级最高)。2.通过 System.getProperties() 获取的 Java 系统参数。3.操作系统环境变量。4.从java:comp/env 得到的 JNDI 属性。5.通过 RandomValuePropertySource 生成的random.*属性。6.jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件,通过spring.config.location参数指定7.jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件8.jar包外部的application.properties或application.yml(不带spring.profile)配置文件9.jar包内部的application.properties或application.yml(不带spring.profile)配置文件10.应用 Java配置类,包含@Configuration注解的 Java 类,通过@PropertySource注解声明的属性文件。11.通过SpringApplication.setDefaultProperties声明的默认属性。
Application.properties and Application.yml files can be placed in four locations:

1.外置:在相对于应用程序运行目录的/congfig子目录里。

2.外置:在应用程序运行的目录里

3.内置:在config包内

4.内置:在Classpath根目录

If spring boot finds the configuration in a higher priority location, it ignores the low-priority configuration. Let's briefly talk about these priorities. This configuration priority of Spring Boot may seem complicated, but it's quite reasonable. The precedence of command-line parameters is set to the highest because it is convenient for us to quickly modify configuration parameter values in a test or production environment without having to repackage and deploy the app. The Springapplication class defaults to converting the command-line arguments that begin with "--" to the configuration parameters that can be used in the app, such as "--name=alex", which sets the configuration parameters, and "name" with the value "Alex". If this feature is not required, parsing command-line arguments can be disabled by springapplication.setaddcommandlineproperties (false).

If the app is built for spring cloud, the bootstrap.yml or bootstrap.properties file is introduced, noting that spring The BOOTSTRAP.YML configuration in the cloud will load first, that is, the configuration will be loaded before the app starts, so the BOOTSTRAP.YML configuration will be loaded at the same time as the Application.yml configuration file and bootstrap.yml configuration file, and the same bootstr AP configuration can also be configured externally, that is, in the specified configuration directory to create a new file can overwrite the BOOTSTRAP.YML configuration embedded in the jar package, note that at this time only the command line to specify the configuration file directory, such as a application configuration file is:/data/webs/a/conf/.

At this point, you need to --spring.config.location=/data/webs/A/conf/ specify the configuration file loading directory with the command line, it is important to note: Regardless of spring boot or spring cloud can change the priority of the configuration through command-line parameters!

三.关于spring boot/spring cloud 配置文件名称

MO9 Micro-Service Application project, All configuration files name is using the default name: application (regardless of suffix. Properties or. yml), using the default name uniformly, but the configuration will be combined with the Profiles attribute to differentiate the corresponding configuration, analogy: Operations configuration, business configuration, and other configurations, as follows:

Default profile: Application.yml or application.properties, note that the properties for the development environment activation are: dev and ops; the production environment activation properties are: PROD and OPS, as shown in the following code:

Development environment (DEV)

spring.profiles.active=devspring.profiles.include=ops

Production environment (PROD)

spring.profiles.active=prodspring.profiles.include=ops
通用运维配置文件:application-ops.yml或application-ops.properties;

The main configuration content is operation and maintenance configuration, analogy: Embedded Tomcat service port, management port, maximum number of connections, time-out, contxt-path and so on operational dimension configuration

开发环境业务配置文件:application-dev.yml或application-dev.properties;

The main content is the configuration of the business on the development environment, analogy: Database configuration, NoSQL configuration, MQ configuration, Interface connection configuration.

生产环境业务配置文件:application-prod.yml或application-prod.properties;

The main content is the configuration of the business on the production environment, analogy: Database configuration, NoSQL configuration, MQ configuration, Interface connection configuration.

三.关于spring boot/spring cloud 日志配置

By default, the Spring boot mode application log is output to the console, and if we need to output to a file, You need to define the following configuration parameters in Application-ops.properties or application-ops.yml: Logging.file or Logging.path of course the most straightforward way to output to a file directly at startup, You can also customize the log configuration and then configure the application in the APPLICATION-OPS.YML configuration file with the following configuration

logging.config=/path/logback-spring.xml

Note: The best way to log profiles is to end with-spring.xml files!

四.关于spring boot/spring cloud 框架版本

Since the spring boot version is updated faster, the latest version is 2.0, considering the compatibility of the test and production environment profile, it is recommended to use the following version of 2.0 in the actual development process, such as the 1.5.8 release version!

Spring Boot/cloud Start Mode description

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.