Spring Cloud Alibaba-SpringBoot(四)

简介

Spring Boot做为单体服务的容器,相较SSM简化了大量的配置。官方网站java

快速开发

咱们能够选择从官方在线下载ZIP包Spring Initializr ,也能够用Idea自带的Spring Initializr构建。git

开发三步走

以整合actuator为例,展现Spring Boot开发的三个步骤。访问http://localhost:8080/actuator查看是否整合成功。web

  • 加依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
复制代码
  • 写注解
不须要写注解
复制代码
  • 写配置
# actuator
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

# info
info:
  app-name: virgo-service-lock
  author: zhaozha
复制代码

配置参数的优先级

  • 1.命令行参数:java -jar XXX.jar --Test=1,yml中${Test}
  • 2.jar包外部的yml
  • 3.jar包内部的yml

开发/生产参数的切换

经过java -jar springboot-demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev指定环境进行测试 spring

  • yml
# 经过连字符的形式
# 公共部分
server:
  port: 8080

## actuator
management:
  endpoints:
    web:
      exposure:
        # 生产不建议所有放开
        include: '*'
      # /actuator -> /monitor 
      base-path: '/monitor'
  endpoint:
    health:
      show-details: always
    #优雅停机
    shutdown:
      enabled: true

# info
info:
  app-name: springboot-demo
  author: zhao

---
#开发部分
spring:
  profiles: dev
---
#生产部分
spring:
  profiles: prod
server:
  tomcat:
    max-threads: 300
    max-connections: 1000
复制代码
  • properties
# 经过配置文件命名的方式
application.properties
dev-application.properties
prod-application.properties
复制代码

优雅停机

经过actuator的shutdown便可。tomcat

#开机
java -jar springboot-demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
#停机(须要定义安全措施,直接暴露风险很大)
curl -X POST http://localhost:8080/monitor/shutdown
复制代码

项目Demo

gitee.com/dotl/spring…安全

最后

文章如有谬误之处,但愿广大读者指正,互相交流,共同提升。springboot

相关文章
相关标签/搜索