基本的学习流程请查看 泥瓦匠的blog
http://www.bysocket.com/?cat=72
他的各个小节的例子
https://github.com/JeffLi1993/springboot-learning-example
sms-server
POM.XML,由于项目主要是用来发短信服务的,所以其他依赖包加上也无妨,不影响
打war包的方式如下面所示
<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>com.xialeme</groupId>
<artifactId>sms-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 部署到tomcat中需要使用 -->
<packaging>war</packaging>
<properties>
<start-class>com.Application</start-class><!-- main文件路径 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories><!--ali 代码库 -->
<repository>
<id>maven-ali</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
<!-- spring boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- 部署到tomcat中需要使用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- <scope>provided</scope> -->
</dependency>
<!-- spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入 spring-boot-starter-actuator 监控需要-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 引入 spring-boot-starter-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<!-- alibaba fastjson httpclient -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<!-- jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<skipTests>true</skipTests>
<!-- 打成war包时名子 -->
<warName>sms-server</warName>
<!-- mave时启用的main路径(因为其他包里面测试时也加了main函数) -->
<mainClass>${start-class}</mainClass>
<skip>true</skip><!-- 跳过测试 -->
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties配置src/main/resources下面
# 监听端口
server.port=8081
#
#http://blog.csdn.net/xiaoyu411502/article/details/47865561
########################################################
### tomcat 配置
########################################################
# tomcat最大线程数,默认为200
#server.tomcat.max-threads=800
# tomcat的URI编码
#server.tomcat.uri-encoding=UTF-8
# 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹(如:C:\Users\Shanhy\AppData\Local\Temp)
#server.tomcat.basedir=D:/springboot-log
# 打开Tomcat的Access日志,并可以设置日志格式的方法:
#server.tomcat.access-log-enabled=true
#server.tomcat.access-log-pattern=
# accesslog目录,默认在basedir/logs
#server.tomcat.accesslog.directory=
# 日志文件目录
logging.path=D:/springboot-log
# 日志文件名称,默认为spring.log
logging.file=springboot.log
########################################################
###datasource 配置MySQL数据源;
########################################################
spring.datasource.url=jdbc:mysql://192.168.10.10:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
## Mybatis 配置 #由于使用的是jpa所以这里就注释掉mybatis
#mybatis.typeAliasesPackage=org.spring.springboot.domain
#mybatis.mapperLocations=classpath:mapper/*.xml
########################################################
### Java Persistence Api 自动进行建表
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sqlquery
spring.jpa.show-sql = true
# hibernate ddl auto (create,create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy =org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them tothe entity manager)
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
########################################################
###REDIS (RedisProperties) redis基本配置;
########################################################
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=0
Application 不能放在java.main的根目录,必须建一个目录存放不然保错
它会自动扫描它所在目录下的子目录的相关注解
package com;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.common.filter.AuthFilter;
/*******
* Springboot 默认不需要继承SpringBootServletInitializer
* <p>如果部署到外部的TOMCAT则需要继承SpringBootServletInitializer,并且重写configure
* pom中添加<packaging>war</packaging>和spring-boot-starter-tomcat.jar
* </p>
* @author bamboo
*
*/
@RestController
@SpringBootApplication
public class Application {
@Autowired
private RedisTemplate redisTemplate;
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
@RequestMapping("/")
//@ResponseBody
String home() {
return "sms server";
}
@RequestMapping("/city")
//@ResponseBody
String city(String id) {
// 从缓存中获取城市信息
String key = "city_" + id;
ValueOperations<String, String> operations = redisTemplate.opsForValue();
// 缓存存在
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey) {
String city = operations.get(key);
System.out.println("CityServiceImpl.findCityById() : 从缓存中获取了城市 >> " + city.toString());
return city;
}
// 从 DB 中获取城市信息
String city = "空城";
// 插入缓存
operations.set(key, city, 120, TimeUnit.SECONDS);
System.out.println("CityServiceImpl.findCityById() : 城市插入缓存 >> " + city.toString());
return city;
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
运行Application main方法
刷新两次浏览器,可以看到控制台打印如下
http://localhost:8081/city?id=1
CityServiceImpl.findCityById() : 城市插入缓存 >> 空城
CityServiceImpl.findCityById() : 从缓存中获取了城市 >> 空城
可以在redis desktop manager 刷新看到数据已经插入进来了