服務 Docker 化

文章首發於公衆號《程序員果果》php

地址:mp.weixin.qq.com/s/E_gJFbRaW…java

1、簡介

​Docker的出現讓容器化技術得以普及,更快的部署和維護與Spring Cloud的結合,能讓咱們再也不像之前同樣爲了某一個模塊的增長而服務器上大動干戈,還須要考慮環境的問題。在這一篇中會講到 SpringCloud 項目 Docker 化 。程序員

2、建立一個 SpringCloud 項目

建立一個springcloud項目 ,包含eureka-server、service-hi、service-ribbon。web

1. eureka-server 項目

pom.xml

<?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>

	<groupId>com.gf</groupId>
	<artifactId>eureka-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eureka-server</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.gf</groupId>
		<artifactId>chapter02</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>


</project>
複製代碼

application.yml

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://eureka-server:8761/eureka/
spring:
  application:
    name: eureka-server
複製代碼

EurekaServerApplication

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}
}
複製代碼

2. service-hi 項目

pom.xml

<?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>

	<groupId>com.gf</groupId>
	<artifactId>service-hi</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>service-hi</name>
	<description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.gf</groupId>
        <artifactId>chapter02</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>


</project>
複製代碼

application.yml

server:
  port: 8763
spring:
  application:
    name: service-hi
eureka:
  client:
    service-url:
      defaultZone: http://eureka-server:8761/eureka/
複製代碼

ServiceHiApplication

@EnableEurekaClient
@SpringBootApplication
@RestController
public class ServiceHiApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceHiApplication.class, args);
	}

	@Value( "${server.port}" )
	private String port;

	@GetMapping("/hi")
	public String hi() {
		return "hello , port is " + port;
	}

}
複製代碼

3. service-ribbon 項目

pom.xml

<?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>

	<groupId>com.gf</groupId>
	<artifactId>service-ribbon</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>service-ribbon</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.gf</groupId>
		<artifactId>chapter02</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>


</project>
複製代碼

application.yml

server:
  port: 8764
spring:
  application:
    name: service-ribbon
eureka:
  client:
    service-url:
      defaultZone: http://eureka-server:8761/eureka/
複製代碼

HelloService

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    public String hiService() {
        return restTemplate.getForObject( "http://SERVICE-HI/hi" , String.class );
    }

}
複製代碼

HelloControler

@RestController
public class HelloControler {

    @Autowired
    private HelloService helloService;

    @GetMapping(value = "/hi")
    public String hi() {
        return helloService.hiService();
    }

}
複製代碼

ServiceRibbonApplication

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceRibbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceRibbonApplication.class, args);
	}

	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}
}
複製代碼

3、構建鏡像

1. Dockerfile

編寫Dockerfile ,把項目構建成鏡像,須要把 項目jar包 複製到 鏡像中,並且鏡像中要有java的運行環境,因此如今給每一個項目都建立一個Dockerfile,內容以下:spring

eureka-server 項目的 Dockerfiledocker

FROM 192.168.31.143:9090/jdk/openjdk:8-jre

MAINTAINER gf gf@163.com

COPY target/eureka-server-0.0.1-SNAPSHOT.jar /eureka-server-0.0.1-SNAPSHOT.jar

ENTRYPOINT ["java" , "-jar" , "/eureka-server-0.0.1-SNAPSHOT.jar"]
複製代碼

service-hi 項目的 Dockerfileshell

FROM openjdk:8-jre

MAINTAINER gf 782969359@qq.com

COPY target/service-hi-0.0.1-SNAPSHOT.jar /service-hi-0.0.1-SNAPSHOT.jar

ENTRYPOINT ["java" , "-jar" , "/service-hi-0.0.1-SNAPSHOT.jar"]
複製代碼

service-ribbon 項目的 Dockerfileapache

FROM openjdk:8-jre

MAINTAINER gf gf@163.com

COPY target/service-ribbon-0.0.1-SNAPSHOT.jar /service-ribbon-0.0.1-SNAPSHOT.jar

ENTRYPOINT ["java" , "-jar" , "/service-ribbon-0.0.1-SNAPSHOT.jar"]
複製代碼

分別在各個項目根目錄下,經過 mvn packge 命令來,把項目打成jar包 ,並經過 docker build 命令來構建鏡像:bash

mvn package -Dmaven.test.skip=true
複製代碼
docker build -t 項目名:版本號
例如:
docker build -t eureka-server:latest
複製代碼

咱們經過 docker image 命令,查看咱們本地的鏡像,發現鏡像已經構建到本地了:服務器

$ docker images
REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
service-ribbon                                         latest              81b61a359e23        39 hours ago        487MB
service-hi                                             latest              f893c76be99d        39 hours ago        487MB
eureka-server                                          latest              41997de8006f        40 hours ago        487MB
openjdk                                                8-jre               e01608fba686        12 days ago         442MB
複製代碼

根據這三個鏡像運行容器 ,來檢查服務容器化後 ,是否訪問正常,值得注意的是 註冊中心的地址爲 http://eureka-server:8761/eureka/ ,咱們使用容器名做爲地址 ,這就須要,咱們在啓動eureka-server 的容器時,指定指定一個名稱eureka-server ,不然會找不到註冊中心。

docker run -it -d -p 8761:8761 --name eureka-server 41997de8006f
複製代碼
docker run -it -d --name service-hi f893c76be99d
複製代碼
docker run -it -d -p 8764:8764 --name service-ribbon 81b61a359e23
複製代碼

訪問 127.0.0.1:8761 註冊中心正常。

訪問 127.0.0.1:8764/hi 服務正常。

歡迎掃碼或微信搜索公衆號《程序員果果》關注我,關注有驚喜~

相關文章
相關標籤/搜索