EE颠覆者第十章 部署变成服务 dockerfile

开发时候默认是开启模板引擎的缓存,开发时候我们要关闭

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 文件来编译自己的镜像

  1. from 指明当前镜像的基镜像。会自动下载

    FROM ubuntu

  2. maint ainer 指明当前镜像的作者

    MAINTAINER zhangsan

  3. run 在当前镜像上执行Linux命令 并形成一个新的层。 编译时build的动作

    RUN /bin/bash -c "echo helloworld"
    RUN ["/bin/bash","-c","echo hello"]
    
  4. cmd指令 启动镜像容器时的默认行为

    一个Dockerfile只能有一个。设定命令可以在运行镜像时使用参数覆盖。

    CMD echo “this is a test"

    可被: docker run -d image_name echo “this is not a test”

  5. expose 指明了镜像运行时的容器必须监听指定的端口

EXPOSE 8080

  1. ENV 用来设置环境变量

    ENV myName=zhangsan

    或 ENV myName zhangsan

  2. ADD 从当前工作目录复制文件到镜像目录去

    ADD test.txt /mydir/

  3. 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
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值