Docker 部署 Spring Cloud并让docker容器使用宿主机ip

Docker 部署 Spring Cloud并让docker容器使用宿主机ip

点击链接加入群聊【java菜鸟学习】:https://jq.qq.com/?_wv=1027&k=5afU7nS
群号:124569404

使用系统、软件版本:

linux 版本:

[root@VM_0_17_centos docker]# uname -a
Linux VM_0_17_centos 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@VM_0_17_centos docker]# cat /proc/cpuinfo
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 79
model name	: Intel(R) Xeon(R) CPU E5-26xx v4
stepping	: 1
microcode	: 0x1
cpu MHz		: 2394.446
cache size	: 4096 KB
physical id	: 0
siblings	: 1
core id		: 0
cpu cores	: 1
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 13
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch bmi1 avx2 bmi2 rdseed adx xsaveopt
bogomips	: 4788.89
clflush size	: 64
cache_alignment	: 64
address sizes	: 40 bits physical, 48 bits virtual
power management:

docker版本:


[root@VM_0_17_centos docker]# docker --version
Docker version 1.13.1, build 8633870/1.13.1

安装 Docker 环境

  1. yum安装
yum install docker
  1. 安装完成后,使用下面的命令来启动 docker 服务,并将其设置为开机启动:
ervice docker start
chkconfig docker on

#LCTT 译注:此处采用了旧式的 sysv 语法,如采用CentOS 7中支持的新式 systemd 语法,如下:
systemctl  start docker.service
systemctl  enable docker.service
  1. 使用Docker 中国加速器
vi  /etc/docker/daemon.json

#添加后:
{
    "registry-mirrors": ["https://registry.docker-cn.com"],
    "live-restore": true
}
  1. 重新启动docker
systemctl restart docker

maven版本:

[root@VM_0_17_centos docker]# mvn -version
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /usr/local/maven3
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-514.26.2.el7.x86_64", arch: "amd64", family: "unix"

安装MAVEN

  1. 下载:http://mirrors.shu.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
## 解压
tar vxf apache-maven-3.5.2-bin.tar.gz
## 移动
mv apache-maven-3.5.2 /usr/local/maven3
  1. 修改环境变量, 在/etc/profile中添加以下几行
MAVEN_HOME=/usr/local/maven3
export MAVEN_HOME
export PATH=${PATH}:${MAVEN_HOME}/bin
  1. 记得执行source /etc/profile使环境变量生效。

  2. 输入mvn -version 返回版本信息则安装正常。

jdk版本:

[root@VM_0_17_centos docker]# java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

  1. 安装JDK
yum -y install java-1.8.0-openjdk*

  1. 配置环境变量
    打开 vim /etc/profile
    添加一下内容
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.el7_4.x86_64 
export PATH=$PATH:$JAVA_HOME/bin 
  1. 修改完成之后,使其生效
source /etc/profile
  1. 输入java -version 返回版本信息则安装正常。

1,创建Eureka服务

目录结构如下:

│  eureka-service.iml
│  pom.xml
│  README.md
│
└─src
    └─main
        ├─java
        │  ├─com
        │  │  └─udo
        │  │      └─eureka
        │  │              EurekaApplication.java
        │  │
        │  └─docker
        │          Dockerfile
        │
        └─resources
                application.properties

application.properties内容如下:

server.port=8761
spring.application.name=udo-eureka
#eureka.instance.hostname=localhost

#通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://129.204.42.203:${server.port}/eureka/
eureka.instance.prefer-ip-address:true
eureka.instance.ip-address: 129.204.42.203
logging.level.root=WARN
logging.level.org.springframework=WARN
logging.level.com.cool=INFO

pom.xml 文件加入docker build

  <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.8java.version>
        <docker.image.prefix>springbootdocker.image.prefix>

    properties>    
<build>
        <finalName>eureka-servicefinalName>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <configuration>
                    <source>${java.version}source>
                    <target>${java.version}target>
                    <encoding>${project.build.sourceEncoding}encoding>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-resources-pluginartifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}encoding>
                configuration>
            plugin>
            <plugin>
                <groupId>com.spotifygroupId>
                <artifactId>docker-maven-pluginartifactId>
                <version>0.4.13version>
                <configuration>
                    <imageName>${docker.image.prefix}/${project.artifactId}imageName>
                    <dockerDirectory>${project.basedir}/src/main/dockerdockerDirectory>
                    <resources>
                        <resource>
                            <targetPath>/targetPath>
                            <directory>${project.build.directory}directory>
                            <include>${project.build.finalName}.jarinclude>
                        resource>
                    resources>
                configuration>
            plugin>
        plugins>
    build>

Dockerfile文件内容如下:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD eureka-service.jar eureka-service.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/eureka-service.jar"]
EXPOSE 8761
  • FROM ,表示使用 Jdk8 环境 为基础镜像,如果镜像不是本地的会从 DockerHub 进行下载
  • VOLUME ,VOLUME 指向了一个/tmp的目录,由于 Spring Boot 使用内置的Tomcat容器,Tomcat 默认使用/tmp作为工作目录。这个命令的效果是:在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
  • ADD ,拷贝文件并且重命名
  • ENTRYPOINT ,为了缩短 Tomcat 的启动时间,添加java.security.egd的系统属性指向/dev/urandom作为 ENTRYPOINT

同样Client 也是相同的配置pom.xmlDockerfile (注意修改Dockerfile 名字和EXPOSE端口)

Client application.properties配置如下:

spring.application.name=udo-product
eureka.instance.prefer-ip-address=true
server.port=8084
spring.mvc.favicon.enabled = false
eureka.client.serviceUrl.defaultZone=http://129.204.42.203:8761/eureka/
spring.cloud.inetutils.ignoredInterfaces[0]=lo
spring.cloud.inetutils.ignoredInterfaces[1]=eth0
spring.cloud.inetutils.ignoredInterfaces[2]=eth1
spring.instance.instance-id: ${spring.cloud.client.ip-address}:${server.port}
spring.instance.prefer-ip-address: true

然后使用xFTP将两个文件夹上传到linux服务器

我上传的目录是:

[root@VM_0_17_centos ftp_down]# pwd
/usr/local/ftp_down
[root@VM_0_17_centos ftp_down]# ls
apache-maven-3.6.0-bin.tar.gz  eureka-service  eureka-service-0.0.1-SNAPSHOT.jar  udo-product
[root@VM_0_17_centos ftp_down]# 

可以看到有eureka-service文件夹和udo-product文件夹

先打开eureka-service ,使用mvn打包命令测试是否能够正常打包,第一次可能打包时间比较长

mvn clean package -Dmaven.skip.test=true
#-Dmaven.skip.test=true 跳过测试并且不构建test的包

打包结果如下:

[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.udo:eureka-service >-----------------------
[INFO] Building eureka-server 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ eureka-service ---
[INFO] Deleting /usr/local/ftp_down/eureka-service/target
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ eureka-service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ eureka-service ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /usr/local/ftp_down/eureka-service/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ eureka-service ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /usr/local/ftp_down/eureka-service/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ eureka-service ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ eureka-service ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ eureka-service ---
[INFO] Building jar: /usr/local/ftp_down/eureka-service/target/eureka-service.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.0.4.RELEASE:repackage (default) @ eureka-service ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  25.078 s
[INFO] Finished at: 2018-12-27T11:25:56+08:00
[INFO] ------------------------------------------------------------------------

打包正常,然后使用docker构建,第一次可能打包时间比较长

mvn clean package docker:build

构建结果如下:

[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.udo:eureka-service >-----------------------
[INFO] Building eureka-server 1.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- docker-maven-plugin:0.4.13:build (default-cli) @ eureka-service ---
[INFO] Copying /usr/local/ftp_down/eureka-service/target/eureka-service.jar -> /usr/local/ftp_down/eureka-service/target/docker/eureka-service.jar
[INFO] Copying /usr/local/ftp_down/eureka-service/src/main/docker/Dockerfile -> /usr/local/ftp_down/eureka-service/target/docker/Dockerfile
[INFO] Building image springboot/eureka-service
Step 1/5 : FROM openjdk:8-jdk-alpine
 ---> 97bc1352afde
Step 2/5 : VOLUME /tmp
 ---> Using cache
 ---> b2072d304eeb
Step 3/5 : ADD eureka-service.jar eureka-service.jar
 ---> 867dd6b7e3a1
Removing intermediate container 83785bff791a
Step 4/5 : ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -jar /eureka-service.jar
 ---> Running in 34986325eecd
 ---> 07d1cf9034dd
Removing intermediate container 34986325eecd
Step 5/5 : EXPOSE 8761
 ---> Running in e122c6148e70
 ---> 91d01dd28847
Removing intermediate container e122c6148e70
Successfully built 91d01dd28847
[INFO] Built springboot/eureka-service
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  18.564 s
[INFO] Finished at: 2018-12-27T11:27:34+08:00
[INFO] ------------------------------------------------------------------------

可以看到 依次执行了5个步骤,且构建了docker image_id为91d01dd28847

重复相同的操作构建 udo-product

使用docker images查看镜像

REPOSITORY                                      TAG                 IMAGE ID            CREATED              SIZE
springboot/eureka-service                       latest              91d01dd28847        About a minute ago   149 MB
springboot/udo-product-core                     latest              ce13df6d307c        About an hour ago    204 MB
springboot/product-api                          latest              833022b7a675        About an hour ago    103 MB
                                                        e413e1ef2c9d        12 hours ago         204 MB
                                                        59f24d920431        12 hours ago         204 MB
                                                        f93d372a1017        17 hours ago         149 MB
target_docker                                   latest              0c7b92f346a0        18 hours ago         149 MB
                                                        aac971ad6391        25 hours ago         147 MB
                                                        d89a2d10f5d8        26 hours ago         103 MB
docker.io/rabbitmq                              3-management        d69a5113ceae        8 weeks ago          149 MB
docker.io/openjdk                               8-jdk-alpine        97bc1352afde        2 months ago         103 MB
docker.elastic.co/elasticsearch/elasticsearch   6.1.1               06f0d8328d66        12 months ago        539 MB
docker.io/gliderlabs/registrator                latest              3b59190c6c80        2 years ago          23.8 MB

springboot/eureka-servicespringboot/udo-product-core 都在内

删除镜像

[root@VM_0_17_centos eureka-service]# docker rmi $(docker images -f "dangling=true" -q)

使用命令启动springboot/eureka-service

docker run -di -p 8761:8761 --name=eureka springboot/eureka-service
#-di 后台启动
#-p 8761:8761 将宿主机的8761端口映射到docker容器的8761端口
#--name=eureka  命名
#springboot/eureka-service 对应 images REPOSITORY

访问http://129.204.42.203:8761/

Docker 部署 Spring Cloud并让docker容器使用宿主机ip_第1张图片

dock命令启动udo-product(此时给udo-product分配静态ip,否则注册到eureka上面是内网ip)

docker run -d --name product_core --expose=8084 -p 8084:8084 -e "EUREKA_INSTANCE_IP-ADDRESS=129.204.42.203" -e "SERVER_PORT=8084" springboot/udo-product-core

再次访问http://129.204.42.203:8761/,鼠标悬停在08e62989da4f:udo-product:8084,可看到左下角提示跳转地址为:http://129.204.42.203:8084/actuator/info

Docker 部署 Spring Cloud并让docker容器使用宿主机ip_第2张图片

同理,在本地再开一个服务也可以注册到到eureka,且本地的服务可以调到docker内的udo-product服务

你可能感兴趣的:(docker)