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
	}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章