[toc]
继承SpringBoot
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>
<groupId>com.funtl</groupId>
<artifactId>myshop-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>dependencies</name>
</project >
模板引擎Thymeleaf
Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。
引入依赖
主要增加 spring-boot-starter-thymeleaf
和 nekohtml
这两个依赖
-
spring-boot-starter-thymeleaf
:Thymeleaf 自动配置 -
nekohtml
:允许使用非严格的 HTML 语法
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
配置 Thymeleaf
在 application.yml
中配置 Thymeleaf
spring:
thymeleaf:
cache: false # 开发时关闭缓存,不然没法看到实时页面
mode: HTML # 用非严格的 HTML
encoding: UTF-8
servlet:
content-type: text/html
Druid数据库(Mysql)
Druid 是阿里巴巴开源平台上的一个项目,整个项目由数据库连接池、插件框架和 SQL 解析器组成。
引入依赖
在 pom.xml
文件中引入druid
依赖和mysql
依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
配置 application.yml
在 application.yml
中配置数据库连接
spring:
datasource:
druid:
url: jdbc:mysql://ip:port/dbname?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: 123456
initial-size: 1
min-idle: 1
max-active: 20
test-on-borrow: true
# MySQL 8.x: com.mysql.cj.jdbc.Driver
driver-class-name: com.mysql.jdbc.Driver
数据库操作插件tk.mybatis
tk.mybatis 是在 MyBatis 框架的基础上提供了很多工具,让开发更加高效
引入依赖
在 pom.xml
文件中引入 mapper-spring-boot-starter
依赖,该依赖会自动引入 MyBaits 相关依赖
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
配置 application.yml
配置 MyBatis
mybatis:
type-aliases-package: 实体类的存放路径,如:com.funtl.hello.spring.boot.entity
mapper-locations: classpath:mapper/*.xml
创建一个通用的父级接口
主要作用是让 DAO 层的接口继承该接口,以达到使用 tk.mybatis 的目的
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* 自己的 Mapper
* 特别注意,该接口不能被扫描到,否则会出错
* <p>Title: MyMapper</p>
* <p>Description: </p>
*
* @author Demon
* @version 1.0.0
*/
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
分页插件PageHelper
在 pom.xml 文件中引入 pagehelper-spring-boot-starter 依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
服务监控Admin
Spring Boot Admin 是一套功能强大的监控管理系统。
服务端配置
添加依赖
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.1</version>
</dependency>
开启服务注解
通过 @EnableAdminServer 注解开启 Admin 功能
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer
public class AdminApplication {
public static void main(String[] args) {
SpringApplication.run(AdminApplication.class, args);
}
}
修改application.yml
spring:
application:
name: hello-spring-cloud-admin
server:
port: 8084
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: health,info
客户端配置
添加依赖
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.1</version>
</dependency>
修改application.yml
spring:
application:
name: hello-spring-cloud-admin-client
boot:
admin:
client:
url: http://localhost:8084
开启监测服务配置
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
数据缓存Redis
Redis 是用 C 语言开发的一个开源的高性能键值对(key-value)数据库。
配置依赖
添加pool2连接池以来,使用Lettuce进行连接
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
单个Redis连接配置
spring:
redis:
database: 0
port: 6379
host: 192.168.25.132
password:
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: -1ms
min-idle: 0
Redis集群连接配置(sentinel)
Redis Sentinel 是官方推荐的高可用性解决方案。它是 Redis 集群的监控管理工具,可以提供节点监控、通知、自动故障恢复和客户端配置发现服务。
spring:
redis:
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: -1ms
min-idle: 0
sentinel:
master: mymaster
nodes: 192.168.25.132:26379, 192.168.25.132:26380, 192.168.25.132:26381
Redis集群连接配置(redis-cluster)
spring:
redis:
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: -1ms
min-idle: 0
cluster:
nodes:
- 192.168.198.132:8001
- 192.168.198.132:8002
- 192.168.198.132:8003
- 192.168.198.132:8004
- 192.168.198.132:8005
- 192.168.198.132:8006
消息队列RabbitMQ
配置依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
修改application.yml
spring:
rabbitmq:
host: 192.168.25.132
port: 5672
username: rabbit
password: 123456
任务调度Quartz
Quartz 是 OpenSymphony 开源组织在 Job Scheduling 领域又一个开源项目,它可以与 J2EE 与 J2SE 应用程序相结合也可以单独使用。
配置依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
开启任务注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class HelloQuatrzApplication {
public static void main(String[] args) {
SpringApplication.run(HelloQuatrzApplication.class, args);
}
}
分布式服务框架Dubbo
Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、轻量级的开源 Java RPC 分布式服务框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
配置依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-actuator</artifactId>
<version>0.2.0</version>
</dependency>
- com.alibaba.boot:dubbo-spring-boot-actuator:0.2.0:Dubbo 的服务状态检查
服务端配置
修改application
配置
import com.alibaba.dubbo.container.Main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloDubboServiceUserProviderApplication {
public static void main(String[] args) {
SpringApplication.run(HelloDubboServiceUserProviderApplication.class, args);
// 启动 Provider 容器,注意这里的 Main 是 com.alibaba.dubbo.container 包下的
Main.main(args);
}
}
修改application.yml
# Spring boot application
spring:
application:
name: 项目名
# Dubbo Config properties
dubbo:
## Base packages to scan Dubbo Component:@com.alibaba.dubbo.config.annotation.Service
scan:
basePackages: 带有dubbo注解的包目录
## ApplicationConfig Bean
application:
id: 项目名
name: 项目名
qos-port: 22222
qos-enable: true
## ProtocolConfig Bean
protocol:
id: dubbo
name: dubbo
port: 8501
status: server
## RegistryConfig Bean
registry:
id: zookeeper
address: zookeeper://192.168.25.134:2181?backup=192.168.25.134:2182,192.168.25.134:2183
# Enables Dubbo All Endpoints
management:
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
# Dubbo Health
health:
dubbo:
status:
## StatusChecker Name defaults (default : "memory", "load" )
defaults: memory
## StatusChecker Name extras (default : empty )
extras: load,threadpool
客户端配置
修改application.yml
# Spring boot application
spring:
application:
name: 项目名
server:
port: 9090
# Dubbo Config properties
dubbo:
scan:
basePackages: 带有dubbo注解的包目录
## ApplicationConfig Bean
application:
id: 项目名
name: 项目名
## RegistryConfig Bean
registry:
id: zookeeper
address: zookeeper://192.168.25.134:2181?backup=192.168.25.134:2182,192.168.25.134:2183
# Dubbo Endpoint (default status is disable)
endpoints:
dubbo:
enabled: true
management:
server:
port: 9091
# Dubbo Health
health:
dubbo:
status:
## StatusChecker Name defaults (default : "memory", "load" )
defaults: memory
# Enables Dubbo All Endpoints
endpoint:
dubbo:
enabled: true
dubbo-shutdown:
enabled: true
dubbo-configs:
enabled: true
dubbo-services:
enabled: true
dubbo-references:
enabled: true
dubbo-properties:
enabled: true
endpoints:
web:
exposure:
include: "*"
搜索服务Solr
Solr 是一个开源搜索平台,用于构建搜索应用程序。它建立在 Lucene (全文搜索引擎)之上。
配置依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-solr</artifactId>
</dependency>
修改application.yml
spring:
data:
solr:
host: http://192.168.25.134:8983/solr/ik_core
注意,这里的host为Solr服务端的创建的搜索域访问地址
发送邮件mail
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置application.yml
spring:
mail:
host: smtp.qq.com
# 你的邮箱授权码
password:
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
# 发送邮件的邮箱地址
username:
日志管理
相关依赖
实际开发中不需要直接添加该依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifacteId>spring-boot-starter-logging</artifacted>
</dependency>
日志级别:TRANCE < DRBUG < INFO < WARN < ERROR <FATAL < OFF
配置application.yml
logging:
level:
#root 日志级别以WARN级别输出
root: warn
org:
springframework:
#springframework.web日志以DEBUG级别输出
web: debug
pattern:
# 配置控制台日志显示格式
console: "%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n"
# 配置文件中日志显示格式
file: "%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n"
# 文件输出相对路径
file:
name: D:\\springboot\\info.log
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static Logger logger = LoggerFactory.getLogger(LoginController.class);
安全管理Security
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
添加Java配置文件
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* 安全配置类
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/**").permitAll()
.anyRequest().authenticated()
.and().csrf().disable();
}
}