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

第 26 章 Spring Boot

目录

26.1. Spring Boot Quick start
26.1.1. Springboot with Maven
26.2. Spring 开发环境
26.3. SpringApplication
26.3.1. 运行 Spring boot 项目
26.3.2. @SpringBootApplication
26.4. 如何优雅停止 Springboot 运行
26.5. Springboot with Properties 配置文件
26.6. Spring boot with Logging
26.7. Springboot with Undertow / Jetty / http2
26.7.1. Spring boot with Undertow
26.7.2. Spring boot with HTTP2 SSL
26.8. Spring boot with MongoDB
26.9. Spring boot with MySQL
26.10. Spring boot with Oracle
26.11. Spring boot with PostgreSQL
26.12. Spring boot with Elasticsearch
26.13. Spring boot with Elasticsearch TransportClient
26.14. Spring boot with Apache Hive
26.15. Spring boot with Phoenix
26.16. Spring boot with Datasource
26.16.1. Master / Slave 主从数据库数据源配置
26.17. 连接池配置
26.17.1. druid
26.18. Spring boot with Redis
26.18.1. Spring boot with Redis
26.18.2. Redis Pub/Sub
26.18.3. Sprint boot with Redisson
26.19. Spring boot with Queue
26.19.1. Spring boot with RabbitMQ(AMQP)
26.19.2. Spring boot with Apache Kafka
26.20. Spring boot with Scheduling
26.20.1. 启用计划任务
26.20.2. @Scheduled 详解
26.20.3. cron 表达式
26.21. Spring boot with Swagger
26.21.1. Spring boot with Springdoc
26.21.2. Spring boot with knife4j
26.21.3. springfox
26.22. Spring boot with lombok
26.23. Spring boot with Container
26.23.1. Spring boot with Docker
26.23.2. Spring boot with Docker stack
26.23.3. Spring boot with Kubernetes
26.24. Spring boot with command line
26.25. Spring Boot Actuator
26.25.1. 与 Spring Boot Actuator 有关的配置
26.25.2. 健康状态
26.25.3. info 配置信息
26.26. SpringBootTest
26.27. Spring boot with Aop
26.28. Spring boot with starter
26.28.1. 实现 starter
26.28.2. 引用 starter
26.29. Spring boot with Monitor
26.29.1. SpringBoot Admin
26.29.2. Spring boot with Grafana
26.29.3. Spring Boot with Prometheus
26.30. Spring boot with Git version
26.31. Spring boot with Session share
26.31.1. Redis
26.32. Spring boot with Caching
26.33. Spring boot with Email
26.33.1. spring-boot-starter-mail
26.33.2. 用法详解
26.34. Spring boot with Hessian
26.35. Spring boot with Async
26.35.1. 带有返回值的异步任务
26.35.2. 默认简单线程池 SimpleAsyncTaskExecutor
26.35.3. ThreadPoolTaskExecutor 自定义线程池
26.35.4. 自定义线程池 ThreadPoolExecutor
26.36. Springboot with Ethereum (web3j)
26.37. Java Record 新特性
26.38. Spring boot with Retryable
26.38.1. @EnableResilientMethods
26.38.2. spring-retry
26.39. Springboot 接入阿里云
26.40. Spring Boot 4 + OpenTelemetry
26.41. Springboot with Koog for Java

26.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/
		

26.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