Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 1 章 Spring Boot

目录

1.1. Spring Boot Quick start
1.1.1. Springboot with Maven
1.2. Spring 开发环境
1.3. SpringApplication
1.3.1. 运行 Spring boot 项目
1.3.2. @SpringBootApplication
1.4. 如何优雅停止 Springboot 运行
1.5. Springboot with Properties 配置文件
1.6. Spring boot with Logging
1.7. Springboot with Undertow / Jetty / http2
1.7.1. Spring boot with Undertow
1.7.2. Spring boot with HTTP2 SSL
1.8. Spring boot with MongoDB
1.9. Spring boot with MySQL
1.10. Spring boot with Oracle
1.11. Spring boot with PostgreSQL
1.12. Spring boot with Elasticsearch
1.13. Spring boot with Elasticsearch TransportClient
1.14. Spring boot with Apache Hive
1.15. Spring boot with Phoenix
1.16. Spring boot with Datasource
1.16.1. Master / Slave 主从数据库数据源配置
1.17. 连接池配置
1.17.1. druid
1.18. Spring boot with Redis
1.18.1. Spring boot with Redis
1.18.2. Redis Pub/Sub
1.18.3. Sprint boot with Redisson
1.19. Spring boot with Queue
1.19.1. Spring boot with RabbitMQ(AMQP)
1.19.2. Spring boot with Apache Kafka
1.20. Spring boot with Scheduling
1.20.1. 启用计划任务
1.20.2. @Scheduled 详解
1.20.3. cron 表达式
1.21. Spring boot with Swagger
1.21.1. Spring boot with Springdoc
1.21.2. Spring boot with knife4j
1.21.3. springfox
1.22. Spring boot with lombok
1.23. Spring boot with Container
1.23.1. Spring boot with Docker
1.23.2. Spring boot with Docker stack
1.23.3. Spring boot with Kubernetes
1.24. Spring boot with command line
1.25. Spring Boot Actuator
1.25.1. 与 Spring Boot Actuator 有关的配置
1.25.2. 健康状态
1.25.3. info 配置信息
1.26. SpringBootTest
1.27. Spring boot with Aop
1.28. Spring boot with starter
1.28.1. 实现 starter
1.28.2. 引用 starter
1.29. Spring boot with Monitor
1.29.1. SpringBoot Admin
1.29.2. Spring boot with Grafana
1.29.3. Spring Boot with Prometheus
1.30. Spring boot with Git version
1.31. Spring boot with Session share
1.31.1. Redis
1.32. Spring boot with Caching
1.33. Spring boot with Email
1.33.1. spring-boot-starter-mail
1.33.2. 用法详解
1.34. Spring boot with Hessian
1.35. Spring boot with Async
1.35.1. 带有返回值的异步任务
1.35.2. 默认简单线程池 SimpleAsyncTaskExecutor
1.35.3. ThreadPoolTaskExecutor 自定义线程池
1.35.4. 自定义线程池 ThreadPoolExecutor
1.36. Springboot with Ethereum (web3j)
1.37. Java Record 新特性
1.38. Spring boot with Retryable
1.38.1. @EnableResilientMethods
1.38.2. spring-retry
1.39. Springboot 接入阿里云
1.40. Spring Boot 4 + OpenTelemetry
1.41. Springboot with Koog for Java

1.1. Spring Boot Quick start

创建项目

		
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 -		
		
		

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>		
		
		

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/
		

1.1.1. Springboot with Maven

spring-boot-maven-plugin 插件

resource

将 resource 添加应用程序

			
<build>
    <resources>
        <resource>
            <directory>src/main/java/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>*.jks</exclude>
            </excludes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
</build>		
			
			

Maven run

			
$ mvn spring-boot:run
$ mvn -P prod spring-boot:run
			
			

-P 指定 Maven 的 profile,如果指定 Springboot 的 profiles 请使用 -Drun.profiles=prod

			
$ mvn spring-boot:run -Drun.profiles=prod			
			
			

打包后,使用jar包运行

			
$ mvn verify
$ mvn package
$ java -jar target/api.netkiller.cn-0.0.1-SNAPSHOT.jar
			
			

Spring Boot maven 插件 build-image

Spring Boot 构建 Docker 镜像,你不需要写 Dockerfile,plugin 帮你完成。

只需要简单的执行:

			
mvn spring-boot:build-image			
			
			

执行完成后会看到成功提示信息:

			
[INFO] Successfully buit image 'docker.io/library/demo:0.0.1-SNAPSHOT'			
			
			

运行容器测试:

			
docker run -p 8000:8080 -t demo:0.0.1-SNAPSHOT			
			
			

注意:这里映射的本机端口是8000。

			
curl http://localhost:8000/
			
			

生成项目信息

mvn spring-boot:build-info

			
neo@MacBook-Pro-Neo ~/workspace/microservice/config % mvn spring-boot:build-info