开发时候默认是开启模板引擎的缓存,开发时候我们要关闭
spring.thymeleaf.cache=false
spring.freemarker.cache=false
注册为Linux服务
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
mvn package
rpm -ivh jdk-8u51-linux-x64.rpm
centos 6.6基于Linux的init.d部署
sudo ln -s /root/桌面/ch10-0.0.1-SNAPSHOT.jar /etc/init.d/ch10
service ch10 start
service ch10 stop
service ch10 status
chkconfig ch10 on
基于Linux 的 Systemd部署(可用)
在 /etc/systemd/system/ch10.service
[Unit]
Description=ch10
After=syslog.target
[Service]
ExecStart= /usr/local/jdk1.8.0_11/bin/java -jar /root/桌面/ch10-0.0.1-SNAPSHOT.jar
[Install]
WantedBy=muti-user.target
systemctl start ch10/ch10.service
systemctl stop ch10/ch10.service
systemctl status ch10/ch10.service
systemctl enable ch10/ch10.service 开机启动
journalctl -u ch10/ch10.service 项目日志
jar包修改成war包
<packaging>war</packaging>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
//super.configure(builder);
return builder.sources(Ch10Application.class);
}
}
docker 云部署
Docker使用 Dockerfile 文件来编译自己的镜像
-
from 指明当前镜像的基镜像。会自动下载
FROM ubuntu
-
maint ainer 指明当前镜像的作者
MAINTAINER zhangsan
-
run 在当前镜像上执行Linux命令 并形成一个新的层。 编译时build的动作
RUN /bin/bash -c "echo helloworld" RUN ["/bin/bash","-c","echo hello"]
-
cmd指令 启动镜像容器时的默认行为
一个Dockerfile只能有一个。设定命令可以在运行镜像时使用参数覆盖。
CMD echo “this is a test"
可被: docker run -d image_name echo “this is not a test”
-
expose 指明了镜像运行时的容器必须监听指定的端口
EXPOSE 8080
-
ENV 用来设置环境变量
ENV myName=zhangsan
或 ENV myName zhangsan
-
ADD 从当前工作目录复制文件到镜像目录去
ADD test.txt /mydir/
-
entrypoint 让容器像一个可执行程序一样运行。 是运行时 run的动作
ENTRYPOINT ["/bin/echo"]
docker run -d image_name “this is not a test” //向镜像传递参数
安装docker
yum install docker
systemctl start docker
systemctl enable docker
一个简单的项目
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<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>
@SpringBootApplication
public class Ch10Application{
public static void main(String[] args) {
SpringApplication.run(Ch10Application.class, args);
}
}
@Controller
public class DemoController {
@RequestMapping("/")
public String index(){
return "index";
}
}
spring.thymeleaf.cache=false
src/main/resources/templates/index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>test</title>
</head>
<body >
<div>
首页,哈哈哈哈
</div>
</body>
</html>
DockerFile
同目录下新建 DockerFile
FROM java:8
MAINTAINER zhangsan
ADD ch10-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"] //启动时 运行 java -jar app.jar
运行成docker image
docker build -t wisely/ch10docker .
公司名/项目名,命名规范
. 表示当前路径下
运行镜像
docker run -d --name ch10 -p 38080:8080 wisely/ch10docker
spring Boot 测试
实体类
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
}
数据访问 和 控制器
public interface PersonRepository extends JpaRepository<Person, Long> {
}
@RestController
@RequestMapping("/person")
public class PersonController {
@Autowired
PersonRepository personRepository;
@RequestMapping(method = RequestMethod.GET,produces = {MediaType.APPLICATION_JSON_VALUE} )
public List<Person> findAll(){
return personRepository.findAll();
}
}
测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Ch104Application.class) //1 替代 ContextConfiguration
@WebAppConfiguration
@Transactional //2 确保每次测试后数据都会被回滚
public class Ch104ApplicationTests {
@Autowired
PersonRepository personRepository;
MockMvc mvc;
@Autowired
WebApplicationContext webApplicationContext;
String expectedJson;
@Before //3 测试之前的初始化
public void setUp() throws JsonProcessingException{
Person p1 = new Person("wyf");
Person p2 = new Person("wisely");
personRepository.save(p1);
personRepository.save(p2);
expectedJson =Obj2Json(personRepository.findAll()); //4 期望返回json
mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
protected String Obj2Json(Object obj) throws JsonProcessingException{//5将对象转换为json
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(obj);
}
@Test
public void testPersonController() throws Exception {
String uri="/person";
MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON))
.andReturn(); //6 获取一个request的执行结果
int status = result.getResponse().getStatus(); //7 状态
String content = result.getResponse().getContentAsString(); //8 内容
Assert.assertEquals("错误,正确的返回值为200",200, status); //9 预期结果,比较
Assert.assertEquals("错误,返回值和预期返回值不一致", expectedJson,content); //10
}
}