DockerSwarm 微服务部署

栏目: 后端 · 发布时间: 5年前

内容简介:之前《服务Docker化》中,使用 docker-compose.yml 来一次配置启动多个容器,在 Swarm 集群中也可以使用 compose 文件 (docker-compose.yml) 来配置、启动多个服务。在《DockerSwarm集群环境搭建》中,我们使用docker service create 来部署服务时,一次只能部署一个服务,这一节就讲解 DockerSwarm 集群环境中, 使用 docker-compose.yml 一次启动多个关联的服务。创建一个springcloud项目 ,包含

之前《服务 Docker 化》中,使用 docker-compose.yml 来一次配置启动多个容器,在 Swarm 集群中也可以使用 compose 文件 (docker-compose.yml) 来配置、启动多个服务。

在《DockerSwarm集群环境搭建》中,我们使用docker service create 来部署服务时,一次只能部署一个服务,这一节就讲解 DockerSwarm 集群环境中, 使用 docker-compose.yml 一次启动多个关联的服务。

二、创建 SpringCloud 项目

创建一个springcloud项目 ,包含eureka-server、service-hi、service-ribbon。

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
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
复制代码

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:
    serviceUrl:
      defaultZone: http://eureka-server:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: service-hi:8763
复制代码

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:
    serviceUrl:
      defaultZone: http://eureka-server:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: eureka-server:8764
复制代码

HelloService

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    public String hiService() {
        return restTemplate.getForObject( "http://service-hi:8763/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();
	}
}
复制代码

三、构建镜像

1. Dockerfile

编写Dockerfile ,把项目构建成镜像,需要把 项目jar包 复制到 镜像中,而且镜像中要有 java 的运行环境,所以现在给每个项目都创建一个Dockerfile,内容如下:

eureka-server 项目的 Dockerfile

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 项目的 Dockerfile

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 项目的 Dockerfile

#!/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
复制代码

2. 创建 build.sh

为了方便,三个项目根目录下创建 build.sh 脚本,来一键执行项目的打jar包、构建镜像、推送到私有仓库。

eureka-server 项目的 build.sh

#!/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.sh

#!/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 脚本,这样私有仓库就有三个项目的镜像了,如图:

DockerSwarm 微服务部署

三、部署服务

1. 启动集群环境

启动之前搭建好的 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 就是管理节点,不需要切换节点了。

2. 编写 services.yml

之后用 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
复制代码

文件详细说明,这里就不说了,可以网上查一下。

3. 启动服务

通过 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 ,都可以正常访问,说明已经部署成功了。

欢迎扫码或微信搜索公众号《程序员果果》关注我,关注有惊喜~

DockerSwarm 微服务部署

以上所述就是小编给大家介绍的《DockerSwarm 微服务部署》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java解惑

Java解惑

(美)布洛克·加夫特 / 陈昊鹏 / 人民邮电出版社 / 2010-11-22 / 49.00元

你认为自己了解Java多少?你是个爱琢磨的代码侦探吗?你是否曾经花费数天时间去追踪一个由Java或其类库的陷阱和缺陷而导致的bug?你喜欢智力测验吗?本书正好适合你! Bloch和Gafter继承了Effective Java一书的传统,深入研究了Java编程语言及其核心类库的细微之处。本书特写了95个噩梦般的谜题,中间穿插着许多有趣的视觉幻象,寓教于乐。任何具备Java知识的人都可以理解这......一起来看看 《Java解惑》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码