文章首發於公衆號《程序員果果》 地址:mp.weixin.qq.com/s/nWpbqAheu…php
以前《服務Docker化》中,使用 docker-compose.yml 來一次配置啓動多個容器,在 Swarm 集羣中也可使用 compose 文件 (docker-compose.yml) 來配置、啓動多個服務。
在《DockerSwarm集羣環境搭建》中,咱們使用docker service create 來部署服務時,一次只能部署一個服務,這一節就講解 DockerSwarm 集羣環境中, 使用 docker-compose.yml 一次啓動多個關聯的服務。java
建立一個springcloud項目 ,包含eureka-server、service-hi、service-ribbon。node
<?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>
複製代碼
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka-server:8761/eureka/
instance:
prefer-ip-address: true
instance-id: eureka-server:8761
複製代碼
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
複製代碼
<?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>
複製代碼
server:
port: 8763
spring:
application:
name: service-hi
eureka:
client:
serviceUrl:
defaultZone: http://eureka-server:8761/eureka/
instance:
prefer-ip-address: true
instance-id: service-hi:8763
複製代碼
@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;
}
}
複製代碼
<?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>
複製代碼
server:
port: 8764
spring:
application:
name: service-ribbon
eureka:
client:
serviceUrl:
defaultZone: http://eureka-server:8761/eureka/
instance:
prefer-ip-address: true
instance-id: eureka-server:8764
複製代碼
@Service
public class HelloService {
@Autowired
private RestTemplate restTemplate;
public String hiService() {
return restTemplate.getForObject( "http://service-hi:8763/hi" , String.class );
}
}
複製代碼
@RestController
public class HelloControler {
@Autowired
private HelloService helloService;
@GetMapping(value = "/hi")
public String hi() {
return helloService.hiService();
}
}
複製代碼
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
複製代碼
編寫Dockerfile ,把項目構建成鏡像,須要把 項目jar包 複製到 鏡像中,並且鏡像中要有java的運行環境,因此如今給每一個項目都建立一個Dockerfile,內容以下:程序員
eureka-server 項目的 Dockerfileweb
FROM hub.gf.com: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 項目的 Dockerfilespring
FROM hub.gf.com:9090/jdk/openjdk:8-jre
MAINTAINER gf gf@163.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 項目的 Dockerfiledocker
#!/usr/bin/env bash
mvn package -Dmaven.test.skip=true
docker build -t hub.gf.com:9090/springcloud-ribbon/service-ribbon:latest .
docker login -u admin -p Harbor12345 hub.gf.com:9090
docker push hub.gf.com:9090/springcloud-ribbon/service-ribbon:latest
複製代碼
爲了方便,三個項目根目錄下建立 build.sh 腳本,來一鍵執行項目的打jar包、構建鏡像、推送到私有倉庫。shell
eureka-server 項目的 build.shapache
#!/usr/bin/env bash
mvn package -Dmaven.test.skip=true
docker build -t hub.gf.com:9090/springboot-ribbon/eureka-server:latest .
docker login -u admin -p Harbor12345 hub.gf.com:9090
docker push hub.gf.com:9090/springboot-ribbon/eureka-server:latest
複製代碼
service-hi 項目的 build.shspringboot
#!/usr/bin/env bash
mvn package -Dmaven.test.skip=true
docker build -t hub.gf.com:9090/springboot-ribbon/service-hi:latest .
docker login -u admin -p Harbor12345 hub.gf.com:9090
docker push hub.gf.com:9090/springboot-ribbon/service-hi:latest
複製代碼
service-ribbon 項目的 build.sh
#!/usr/bin/env bash
mvn package -Dmaven.test.skip=true
docker build -t hub.gf.com:9090/springboot-ribbon/service-ribbon:latest .
docker login -u admin -p Harbor12345 hub.gf.com:9090
docker push hub.gf.com:9090/springboot-ribbon/service-ribbon:latest
複製代碼
分別執行三個 build.sh 腳本,這樣私有倉庫就有三個項目的鏡像了,如圖:
啓動以前搭建好的 docker swarm 集羣環境:
docker-machine start myvm-1 myvm-2 myvm-3
複製代碼
要在管理節點下部署服務,因此須要知道哪臺是管理節點,隨便鏈接一臺機器,經過 docker node 命令查看節點信息:
docker-machine ssh myvm-1
複製代碼
docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ib1498ex2q18i7gznb2zgicqq * myvm-1 Ready Active Leader 18.09.1-beta2
vels0fe3eh5s5cxj1s573v9wx myvm-2 Ready Active Reachable 18.09.1-beta2
obxnnqelh4p16wajrwvyn6j8v myvm-3 Ready Active Reachable 18.09.1-beta2
複製代碼
myvm-1 就是管理節點,不須要切換節點了。
以後用 docker stack 部署服務,因此須要編寫服務編排文件,內容以下:
version: "3.4"
services:
eureka-server:
image: hub.gf.com:9090/springcloud-ribbon/eureka-server:latest
deploy:
endpoint_mode: vip
resources:
limits:
cpus: "0.5"
memory: "1024M"
ports:
- "8761:8761"
service-hi:
image: hub.gf.com:9090/springcloud-ribbon/service-hi:latest
deploy:
endpoint_mode: vip
resources:
limits:
cpus: "0.5"
memory: "1024M"
ports:
- "8763:8763"
depends_on:
- eureka-server
service-ribbon:
image: hub.gf.com:9090/springcloud-ribbon/service-ribbon:latest
deploy:
endpoint_mode: vip
resources:
limits:
cpus: "0.5"
memory: "1024M"
ports:
- "8764:8764"
depends_on:
- eureka-server
- service-hi
networks:
default:
external:
name: my-overlay
複製代碼
文件詳細說明,這裏就不說了,能夠網上查一下。
經過 docker stack deploy 命令 啓動服務:
docker stack deploy -c services.yml ms
複製代碼
經過 docker service ls 查看服務啓動狀態:
docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
q99gd5rquv3f ms_eureka-server replicated 1/1 hub.gf.com:9090/springcloud-ribbon/eureka-server:latest *:8761->8761/tcp
wjsv5s6fce6k ms_service-hi replicated 1/1 hub.gf.com:9090/springcloud-ribbon/service-hi:latest *:8763->8763/tcp
zjwe7cnpn42y ms_service-ribbon replicated 1/1 hub.gf.com:9090/springcloud-ribbon/service-ribbon:latest *:8764->8764/tcp
複製代碼
服務啓動後 ,訪問 192.168.99.100:8761 , 192.168.99.100:8763/hi , 192.168.99.100:8764/hi ,均可以正常訪問,說明已經部署成功了。
歡迎掃碼或微信搜索公衆號《程序員果果》關注我,關注有驚喜~