Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

第 5 章 Spring Boot

目录

5.1. Spring Boot Quick start
5.1.1. 创建项目
5.1.2. pom.xml
5.1.3. Controller
5.2. Springboot with Maven
5.2.1. resource
5.2.2. Maven run
5.2.3. Spring Boot maven 插件 build-image
5.2.4. 生成项目信息
5.3. SpringApplication
5.3.1. 运行 Spring boot 项目
5.3.2. @SpringBootApplication
5.3.3. 获取 Resources 目录中的静态文件
5.3.4. @EnableAutoConfiguration
5.3.5. @ComponentScan
5.3.6. @EntityScan 实体扫描
5.3.7. @EnableJpaRepositories
5.3.8. CharacterEncodingFilter
5.3.9. 隐藏 Banner
5.3.10. 实体与仓库扫描
5.3.11. 列出 Beans
5.3.12. Tomcat 端口
5.3.13. 配置项设定
5.3.14. spring.profiles.active
5.3.15. @Profile("dev") / @ActiveProfiles("dev")
5.3.16. 设置默认时区
5.4. 如何优雅停止 Springboot 运行
5.4.1. 准备工作
5.4.2. kill 命令演示
5.4.3. 容器中如何优雅关闭 Springboot
5.4.4. 写入PID文件
5.5. Properties 配置文件
5.5.1. application.properties 配置文件
5.5.2. Properties 文件
5.5.3. 参数引用
5.5.4. 默认值
5.5.5. 产生随机数
5.5.6. 多行字符串
5.5.7. 注入多值属性 arrays, list, set
5.5.8. containsProperty 读取配置文件
5.5.9. @PropertySource 注解载入 properties 文件
5.5.10. List 列表类型
5.5.11. Map类型
5.5.12. Binder
5.5.13. 加密 application.properties 中的敏感内容
5.6. Spring boot with Logging
5.6.1. 配置日志文件
5.6.2. 打印日志
5.6.3. logback 配置详解
5.6.4. Log4j2 + Gelf + Logstash
5.6.5. 日志报警
5.6.6. Spring boot with ELK(Elasticsearch + Logstash + Kibana)
5.7. Undertow
5.7.1. Maven 依赖
5.7.2. Application
5.7.3. 相关配置
5.8. Spring boot with Jetty
5.9. Spring boot with HTTP2 SSL
5.9.1. 生成自签名证书
5.9.2. application.properties 配置文件
5.9.3. 启动 Spring boot
5.9.4. restTemplate 调用实例
5.9.5. HTTP2
5.10. Spring boot with Webpage
5.10.1. Maven
5.10.2. application.properties
5.10.3. Application
5.10.4. IndexController
5.10.5. src/main/webapp/WEB-INF/jsp/index.jsp
5.10.6. 集成模板引擎
5.11. Spring boot with Velocity template
5.11.1. Maven
5.11.2. Resource
5.11.3. Application
5.11.4. RestController
5.11.5. Test
5.12. Spring boot with Thymeleaf
5.12.1. Maven
5.12.2. application.properties
5.12.3. Controller
5.12.4. HTML5 Template
5.13. Spring boot with Redis
5.13.1. Spring boot with Redis
5.13.2. Redis Pub/Sub
5.14. Spring boot with MongoDB
5.14.1. Maven
5.14.2. Application
5.14.3. MongoTemplate
5.14.4. Repository
5.15. Spring boot with MySQL
5.15.1. Maven
5.15.2. Resource
5.15.3. Application
5.15.4. JdbcTemplate
5.15.5. CrudRepository
5.16. Spring boot with Oracle
5.16.1. Maven
5.16.2. application.properties
5.16.3. Application
5.16.4. CrudRepository
5.16.5. JdbcTemplate
5.16.6. Controller
5.17. Spring boot with PostgreSQL
5.17.1. pom.xml
5.17.2. application.properties
5.17.3. Application
5.17.4. CrudRepository
5.17.5. JdbcTemplate
5.17.6. Controller
5.17.7. Test
5.18. Spring boot with Elasticsearch
5.18.1. Maven
5.18.2. Application
5.18.3. application.properties
5.18.4. Domain
5.18.5. ElasticsearchRepository
5.19. Spring boot with Elasticsearch TransportClient
5.19.1. Maven
5.19.2. Application
5.19.3. application.properties
5.19.4. ElasticsearchConfiguration
5.19.5. RestController
5.20. Spring boot with Apache Hive
5.20.1. Maven
5.20.2. application.properties
5.20.3. Configuration
5.20.4. CURD 操作实例
5.21. Spring boot with Phoenix
5.21.1. Maven
5.21.2. application.properties
5.21.3. Configuration
5.22. Spring boot with Datasource
5.22.1. Master / Slave 主从数据库数据源配置
5.22.2. 多数据源配置
5.22.3. JPA 多数据源
5.23. 连接池配置
5.23.1. org.apache.tomcat.jdbc.pool.DataSource
5.23.2. druid
5.23.3. c3p0 - JDBC3 Connection and Statement Pooling
5.23.4. dbcp2
5.23.5. bonecp
5.23.6. HikariPool
5.24. Spring boot with RabbitMQ(AMQP)
5.24.1. maven
5.24.2. RabbitMQConfig
5.24.3. 生产者
5.24.4. 消费者
5.25. Spring boot with Apache Kafka
5.25.1. 安装 kafka
5.25.2. maven
5.25.3. Spring boot Application
5.25.4. EnableKafka
5.25.5. KafkaListener
5.25.6. 测试
5.25.7. 完整的发布订阅实例
5.25.8. Spring cloud with Kafka
5.26. Spring boot with Scheduling
5.26.1. Application.java
5.26.2. Component
5.26.3. 查看日志
5.26.4. 计划任务控制开关
5.26.5. @Scheduled 详解
5.26.6. Timer 例子
5.26.7. ScheduledExecutorService 例子
5.27. Spring boot with Swagger
5.27.1. Swagger3
5.27.2. Swagger2
5.27.3. @Api()
5.27.4. @ApiOperation()
5.27.5. @ApiResponses
5.27.6. @ApiModel 实体类
5.28. Spring boot with lombok
5.28.1. @Builder
5.28.2. @Slf4j 注解
5.29. Spring boot with Docker
5.29.1. 通过 Docker 命令构建镜像
5.29.2. 通过 Maven 构建 Docker 镜像
5.29.3. [ERROR] No plugin found for prefix 'dockerfile' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/Users/neo/.m2/repository), central (https://repo.maven.apache.org/maven2)] -> [Help 1]
5.29.4. curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to localhost:8888
5.30. Spring boot with Docker stack
5.30.1. 编译 Docker 镜像
5.30.2.
5.31. Spring boot with Kubernetes
5.31.1. Kubernetes 编排脚本
5.31.2. 部署镜像
5.32. Spring boot with command line
5.32.1. Maven
5.32.2. CommandLineRunner 例子
5.32.3. ApplicationRunner 例子
5.33. Spring Boot Actuator
5.33.1. Maven 依赖
5.33.2. 与 Spring Boot Actuator 有关的配置
5.33.3. actuator 接口
5.33.4. 健康状态
5.33.5. 关机
5.33.6. info 配置信息
5.33.7. 计划任务
5.33.8. 修改 actuator 地址
5.33.9. metrics
5.33.10. 自定义监控指标
5.34. String boot with RestTemplate
5.34.1. RestTemplate Example
5.34.2. GET 操作
5.34.3. POST 操作
5.34.4. PUT 操作
5.34.5. Delete 操作
5.34.6. 上传文件
5.34.7. HTTP Auth
5.34.8. PKCS12
5.34.9. Timeout 超时设置
5.35. SpringBootTest
5.35.1. Maven 依赖
5.35.2. 测试类
5.35.3.
5.35.4. JPA 测试
5.35.5. TestRestTemplate
5.35.6. Controller单元测试
5.35.7. WebTestClient
5.36. Spring boot with Aop
5.36.1. Aspect
5.37. Spring boot with starter
5.37.1. 实现 starter
5.37.2. 引用 starter
5.38. SpringBoot Admin
5.38.1. 依赖
5.38.2. 启用 Springboot Admin
5.38.3. Nginx 跨域
5.39. Spring boot with Grafana
5.39.1. Springboot 集成 InfluxDB
5.39.2. InfluxDB
5.40. Spring Boot with Prometheus
5.40.1. Maven 依赖
5.40.2. application.properties 配置文件
5.40.3. 启动类
5.40.4. 测试
5.40.5. 控制器监控
5.40.6. 自定义埋点监控
5.41. Spring boot with Git version
5.41.1. CommonRestController 公共控制器
5.41.2. VersionRestController 测试控制器
5.41.3. 创建 .gitattributes 文件
5.42. Spring boot with Session share
5.42.1. Redis
5.42.2. 测试 Session
5.42.3. JDBC
5.42.4. Springboot 2.1
5.43. Spring boot with Caching
5.43.1. maven
5.43.2. 启用 Cache
5.43.3. 测试 Controller
5.43.4. @Cacheable 的用法
5.43.5. @CachePut 用法
5.43.6. 解决Expire 和 TTL 过期时间
5.43.7. SpEL表达式
5.44. Spring boot with Email
5.44.1. Maven
5.44.2. Resource
5.44.3. POJO
5.44.4. RestController
5.44.5. Test
5.45. Spring boot with Hessian
5.45.1. Maven
5.45.2. Application
5.45.3. HessianServiceExporter
5.45.4. Service
5.45.5. RestController
5.46. Spring boot with Async
5.46.1. 启用线程池
5.46.2. 配置线程池
5.46.3. 定义多个线程池
5.46.4. 自定义线程池
5.46.5. 设置线程名称
5.46.6. 线程池监控
5.47. Springboot with Ethereum (web3j)
5.47.1. Maven
5.47.2. application.properties
5.47.3. TestRestController
5.47.4. 测试
5.48. Java Record 新特性
5.48.1. Record 替代 POJO 类
5.48.2. Record 作为 Properties
5.48.3. Record 作为实体类
5.48.4. Record 作为 Service
5.48.5. Record 作为 Controller
5.49. Sprint boot with Redisson
5.49.1. Springboot 3.x
5.49.2. Springboot 2.1

注意以下使用 Spring boot 2

5.1. Spring Boot Quick start

5.1.1. 创建项目

		
curl https://start.spring.io/starter.tgz  \
  -d artifactId=creds-example-server \
  -d dependencies=security,web \
  -d language=java \
  -d type=maven-project \
  -d baseDir=example-server \
| tar -xzvf -		
		
		

5.1.2. pom.xml

		
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>api.netkiller.cn</groupId>
	<artifactId>api.netkiller.cn</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Skyline</name>
	<description>skylinechencf@gmail.com</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.0.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.3</version>
				<configuration>
					<source />
					<target />
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>		
		
		

5.1.3. Controller

		
package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}
		
		

测试

curl http://127.0.0.1:8080/