一、idea创建spring boot项目

点击展开查看-IDEA创建spring boot项目

image
image
image
image
image
image
image

二、spring boot配置文件

先改一下 spring boot配置文件的后缀从application.properties 改成application.yml

1、设置spring boot端口

server:
  port: 10086

2、设置spring boot随机端口

我们访问某个微服务的时候,可以从注册中心获取到对应的IP及端口号
所以动态扩容的时候,可以使用随机端口启动项目

server:
  port: ${random.int(10000,65535)} #随机端口

3、设置服务根路径(servlet.context-path)

现在有一个hello接口代码如下:

@RestController
@RequestMapping("/test")
public class TestCollection {

    @RequestMapping("hello")
    public String getValue(){
        return "hello world";
    }
}

如果配置端口是:10086

如果根路径配置如下:要访问hello接口,请求地址就是:http://127.0.0.1:10086/test/hello

  servlet:
    context-path: /  #设置服务的根路径

如果根路径配置如下:要访问hello接口,请求地址就是:http://127.0.0.1:10086/hello_world/test/hello

  servlet:
    context-path: /hello_world   #设置服务的根路径

4、Spring Boot 更换 Banner

只需要在src/main/resources路径下新建一个banner.txt文件,banner.txt中填写好需要打印的字符串内容即可。
banner.txt的内容可以访问这个网站【 https://www.bootschool.net/ascii-art 】然后粘贴到banner.txt
下面是程序员常用的banner.txt内容可以直接使用:

////////////////////////////////////////////////////////////////////
//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O  =  /O                              //
//                      ____/`---'____                           //
//                    .'  \|     |//  `.                         //
//                   /  \|||  :  |||//                          //
//                  /  _||||| -:- |||||-                         //
//                  |   | \  -  /// |   |                       //
//                  | _|  ''---/''  |   |                       //
//                    .-__  `-`  ___/-. /                       //
//                ___`. .'  /--.--  `. . ___                     //
//              ."" '<  `.____<|>_/___.'  >'"".                  //
//            | | :  `- `.;` _ /`;.`/ - ` : | |                 //
//               `-.   _ __ /__ _/   .-` /  /                 //
//      ========`-.____`-.________/___.-`____.-'========         //
//                           `=---='                              //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
//            佛祖保佑       永不宕机     永无BUG                    //
////////////////////////////////////////////////////////////////////
      0000000        0000000        000000             000       0000000000      000         000
    000     000    000     00     000    000           000     000        000    000         000
    00       000  000       00   000                   000    000          000   000         000
    00       000  000      000   00  0000              000    000                000         000
    000     0000   000    0000   0000    000           000   000                 000         000
      000000 000     00000  00   000      000          000    000                000         000
             00            000   00       000          000    000          00    000         000
    000     000    00      00     00      00    000    000     0000      0000     000       000
      0000000       00000000       00000000     000    000       0000000000        0000000000

三、spring boot多环境配置,按照spring.profiles.active来读取不同的配置文件

我们在yml的配置文件中,使用spring.profiles.active来读取不同的配置环境
image
项目启动时,日志会输出【The following profiles are active: dev】:

2021-07-25 23:12:55.325  INFO 28167 --- [  restartedMain] c.e.s.SpringbootHelloWorldApplication    : The following profiles are active: dev

当我把这个项目mvn install 后打成 hello.jar,通过java命令启动项目,可以在启动的时候读取不同环境的配置文件,命令如下:

1、命令一(亲测有效~):

java -jar hello.jar --spring.profiles.active=prod

下面是效果截图:
image

2、命令二(亲测无效~):

java -jar hello.jar --Dspring.profiles.active=prod

第二个是多了个大写的D --Dxxx=xxx

java程序启动参数-D含义详解

-D<名称>=<值> : set a system property 设置系统属性。

我看网上不少文章都是带大写的D的,估计是以前的spring boot版本支持吧

下图命令二 输出的结果,看的出来配置没生效
image


我已经把上述例子的代码放到gitee了,代码传送门,项目是 springboot-hello_world


内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/x-kq/p/15058005.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程

3636 0元 限免