前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >服务 Docker 化

服务 Docker 化

原创
作者头像
程序员果果
修改2019-05-28 18:00:16
4680
修改2019-05-28 18:00:16
举报
文章被收录于专栏:程序员果果程序员果果

文章首发于公众号《程序员果果》

地址:https://mp.weixin.qq.com/s/E_gJFbRaWOE-mnVR1lsYfQ

一、简介

Docker的出现让容器化技术得以普及,更快的部署和维护与Spring Cloud的结合,能让我们不再像以前一样为了某一个模块的增加而服务器上大动干戈,还需要考虑环境的问题。在这一篇中会讲到 SpringCloud 项目 Docker 化 。

二、创建一个 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
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();
	}
}

三、构建镜像

1. Dockerfile

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

eureka-server 项目的 Dockerfile

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

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

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 命令来构建镜像:

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 服务正常。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、简介
  • 二、创建一个 SpringCloud 项目
    • 1. eureka-server 项目
      • pom.xml
      • application.yml
      • EurekaServerApplication
    • 2. service-hi 项目
      • pom.xml
      • application.yml
      • ServiceHiApplication
    • 3. service-ribbon 项目
      • pom.xml
      • application.yml
      • HelloService
      • HelloControler
      • ServiceRibbonApplication
  • 三、构建镜像
    • 1. Dockerfile
    相关产品与服务
    容器镜像服务
    容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档