您好,登錄后才能下訂單哦!
原創(chuàng)文章,歡迎轉(zhuǎn)載。轉(zhuǎn)載請(qǐng)注明:轉(zhuǎn)載自IT人故事會(huì),謝謝!
原文鏈接地址:『高級(jí)篇』docker之微服務(wù)服務(wù)docker化(18)這次進(jìn)入微服務(wù)的部署,代碼也基本都通過了。如果比做一首歌曲的話,前奏已經(jīng)結(jié)束,現(xiàn)在開始我們的高潮部分,如果吧我們的服務(wù)使用docker,使用服務(wù)編排工具,把項(xiàng)目給部署運(yùn)行起來。源碼:https://github.com/limingios/msA-docker
因docker話都是在linux環(huán)境,為了方便編寫dockerfile文件,我切換到mac本上進(jìn)行演示,目的只有一個(gè)方便開發(fā)sh。方便使用。CICD學(xué)習(xí)實(shí)踐首選電腦還是mac。
服務(wù)有個(gè)適合的環(huán)境,服務(wù)可以運(yùn)行起來,給他準(zhǔn)備一個(gè)環(huán)境,比如服務(wù)是個(gè)種子,我們需要準(zhǔn)備一片土地,服務(wù)是一條魚,就需要準(zhǔn)備一片大海。源碼中的服務(wù)有一個(gè)是python寫的,有一個(gè)是java寫的,也就是需要2個(gè)運(yùn)行環(huán)境,一個(gè)基于java,一個(gè)基于python。
去hub.docker.com 搜索java,找到tag
先配置加速,https://www.daocloud.io/mirror
docker pull java:openjdk-8
docker images|grep jdk
docker run -it --entrypoint bash java:openjdk-8
java -version
熟悉docker的老鐵應(yīng)該都知道,如果要編譯docker的話需要docker化,首選需要開發(fā)dockerfile文件。
準(zhǔn)備工作
文件中不能有寫死的情況,如果寫死了,每次服務(wù)的變更都需要變更鏡像。為了減少構(gòu)建鏡像的過程,盡量吧數(shù)據(jù)庫的訪問地址,經(jīng)常會(huì)發(fā)生變化的東西,需要踢出去,不要在配置文件中配置死,針對(duì)數(shù)據(jù)庫的訪問,不能地址直接寫死,mysql的地址,當(dāng)服務(wù)運(yùn)行在docker之后,他的ip是實(shí)時(shí)都在變化的,不能寫死在鏡像里,直接就找不到了就報(bào)錯(cuò)了。還有個(gè)問題,我們的服務(wù)以什么樣的形式放在我們docker里面,springboot之所以說適用于微服務(wù),他有個(gè)很大的好處,它可以將咱們的服務(wù)構(gòu)建成一個(gè)fat jar,只有一個(gè)jar包,然后通過java的一個(gè)命令:java -jar 文件.jar 運(yùn)行起來,這種方式對(duì)于微服務(wù)來說也是很友好的,也非常的簡單,就使用這種方式來做。變量的方式就可以通過springboot --mysql.address 就可以傳遞進(jìn)來了。
修改配置文件 和 pom.xml文件
application.properties
service.name=user-thrift-service
service.port=7911
#數(shù)據(jù)源的配置
spring.datasource.url=jdbc:mysql://{mysql.address}:3306/db_user
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.idig8</groupId>
<artifactId>user-thrift-service</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>
<dependency>
<groupId>com.idig8</groupId>
<artifactId>user-thrift-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<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>
Dockfile編寫
FROM java:openjdk-8
MAINTAINER liming www.idig8.com
COPY target/user-thrift-service-1.0-SNAPSHOT.jar /user-thrift-service.jar
ENTRYPOINT ["java","-jar","/user-thrift-service.jar"]
執(zhí)行build生成鏡像
docker build -t user-thrift-service:latest .
查看ip地址
ifconfig
生成容器
docker run -it user-thrift-service:latest --mysql.address=192.168.1.140
新建立build.sh
#!/usr/bin/env bash
mvn package
docker build -t user-thrift-service:latest .
![](https://upload-images.jianshu.io/upload_images/11223715-966e41457da6513c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 構(gòu)建message-thrift-python-service
>后面很多的服務(wù),都依賴它,必須把它做好。它是一個(gè)python的服務(wù),我們需要找一個(gè)python的鏡像。去官方找吧
``` bash
docker pull python:3.6
docker images|grep python
編寫Dockerfile
FROM python:3.6
MAINTAINER liming www.idig8.com
RUN pip install thrift
ENV PYTHONPATH /
COPY message /message
ENTRYPOINT ["python","/message/message_service.py"]
docker build -t message-thrift-python-service:latest .
build開發(fā)
#!/usr/bin/env bash
docker build -t message-thrift-python-service:latest .
鏡像生成容器
docker run -it message-thrift-python-service:latest
微服務(wù)的依賴,了解docker的老鐵應(yīng)該知道可以通過link的方式,去根據(jù)名字搜索到這個(gè)服務(wù),只要不在開發(fā)范圍內(nèi)的,認(rèn)為是通過另外的介入方式ip,域名的方式。通過docker分為2種情況,微服務(wù)和微服務(wù)之間的通信,微服務(wù)和外圍系統(tǒng)的通信。
修改配置文件 和 pom.xml文件
application.properties
server.name=user-edge-service
server.port=8082
thrift.user.ip=user-thrift-service
thrift.user.port=7911
thrift.message.ip=message-thrift-python-service
thrift.message.port=9090
#redis config
spring.redis.host=${redis.address}
spring.redis.port=6379
spring.redis.password=liming
spring.redis.timeout=30000
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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.idig8</groupId>
<artifactId>user-edge-service</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>
<dependency>
<groupId>com.idig8</groupId>
<artifactId>user-thrift-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.idig8</groupId>
<artifactId>message-thrift-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<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>
Dockerfile編寫
FROM java:openjdk-8
MAINTAINER liming www.idig8.com
COPY target/user-edge-service-1.0-SNAPSHOT.jar /user-edge-service.jar
ENTRYPOINT ["java","-jar","/user-edge-service.jar"]
build.sh 編寫
#!/usr/bin/env bash
mvn package
docker build -t user-edge-service:latest .
sh build.sh
創(chuàng)建容器
docker run -it user-edge-service:latest --redis.address=192.168.1.140
微服務(wù)的依賴,了解docker的老鐵應(yīng)該知道可以通過link的方式,去根據(jù)名字搜索到這個(gè)服務(wù),只要不在開發(fā)范圍內(nèi)的,認(rèn)為是通過另外的介入方式ip,域名的方式。通過docker分為2種情況,微服務(wù)和微服務(wù)之間的通信,微服務(wù)和外圍系統(tǒng)的通信。
修改配置文件 和 pom.xml文件
application.properties
#dubbo 配置
spring.dubbo.application.name=course-dubbo-service
spring.dubbo.registry.address=zookeeper://${zookeeper.address}:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
#spring.dubbo.protocol.host=127.0.0.1
spring.dubbo.scan=com.idig8.course
#數(shù)據(jù)源的配置
spring.datasource.url=jdbc:mysql://${mysql.address}:3306/db_course
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
thrift.user.ip=user-thrift-service
thrift.user.port=7911
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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.idig8</groupId>
<artifactId>course-dubbo-service</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>com.idig8</groupId>
<artifactId>course-dubbo-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.idig8</groupId>
<artifactId>user-thrift-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.idig8</groupId>
<artifactId>user-thrift-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<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>
Dockerfile編寫
FROM java:openjdk-8
MAINTAINER liming www.idig8.com
COPY target/course-dubbo-service-1.0-SNAPSHOT.jar /course-dubbo-service.jar
ENTRYPOINT ["java","-jar","/course-dubbo-service.jar"]
build.sh 編寫
#!/usr/bin/env bash
mvn package
docker build -t course-dubbo-service:latest .
sh build.sh
創(chuàng)建容器
docker run -it course-dubbo-service:latest --redis.address=192.168.1.140 --zookeeper.address=192.168.1.140
跟之間的基本是一樣的,都是重復(fù)的工作。直接看源碼吧
- 修改pom文件增加maven構(gòu)建
- 增加dockerfile文件
- 增加build.sh文件
- 資源文件的修改
docker run -it course-edge-service:latest --zookeeper.address=192.168.1.140
docker run -it gataway-zuul:latest
PS:把所有單獨(dú)的服務(wù),做成了鏡像,下次想個(gè)辦法服務(wù)和鏡像統(tǒng)一的運(yùn)行起來。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。