一、docker安裝
這里先采用在線安裝,利用docker hup下載基礎鏡像
1.環境版本要求
內核版本3.10及其以上
操作系統位數為64位
CPU架構為x86_64或amd64(目前也有別的支持)
內核開啟并支持cgroup和命名空間
2.命令檢查內核版本,本地環境為centos7
uname -r
1
3.更新yum
sudo yum update
1
4.添加Docker的yum源
sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
?
5.安裝Docker軟件包
sudo yum install docker-engine
1
6.設置Docker服務開機自啟
sudo systemctl enable docker.service
1
7.啟動Docker服務
sudo systemctl start docker
1
8.驗證Docker是否安裝成功
docker -v
1
9.查看鏡像
[root@localhost ~]# docker images
REPOSITORY ? ? ? ? ?TAG ? ? ? ? ? ? ? ? IMAGE ID ? ? ? ? ? ?CREATED ??
1
2
10.刪除docker
?sudo yum remove docker \
? ? ? ? ? ? ? ? ? docker-client \
? ? ? ? ? ? ? ? ? docker-client-latest \
? ? ? ? ? ? ? ? ? docker-common \
? ? ? ? ? ? ? ? ? docker-latest \
? ? ? ? ? ? ? ? ? docker-latest-logrotate \
? ? ? ? ? ? ? ? ? docker-logrotate \
? ? ? ? ? ? ? ? ? docker-selinux \
? ? ? ? ? ? ? ? ? docker-engine-selinux \
? ? ? ? ? ? ? ? ? docker-engine
?
二、項目配置
springcloud版本:
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
spring-boot-starter-parent:2.0.3
?
1.eureka
1)關鍵pom
?? ??? ?<dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
? ? ? ? </dependency>
?
2)啟動類
@SpringBootApplication
@EnableEurekaServer
public class CommonserviceEurekaApplication extends SpringBootServletInitializer {
? ? public static void main(String[] args) {
? ? ? ? SpringApplication.run(CommonserviceEurekaApplication.class, args);
? ? }
? ? @Override
? ? protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
? ? ? ? return builder.sources(CommonserviceEurekaApplication.class);
? ? }
}
?
3)配置文件關鍵參數
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
1
2.config
1)關鍵pom
?? ? <!--注冊發現-->
?? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
? ? ? ? </dependency>
? ? ? ? ? <!--配置中心-->
? ? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.springframework.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-config-server</artifactId>
? ? ? ? </dependency>
?
2)啟動類
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
@EnableEurekaClient
public class ConfigServerApplication {
?? ?public static void main(String[] args) {
?? ??? ?SpringApplication.run(ConfigServerApplication.class, args);
?? ?}
}
?
3)配置文件關鍵參數
#配置注冊服務中心
eureka.client.serviceUrl.defaultZone= http://${eureka-container-name}:${config-service-port}/eureka/
#設置為本地啟動的方式,而不是通過git
spring.profiles.active=native
#配置本地配置路徑
spring.cloud.config.server.native.search-locations=${local-config-path}
注意:上面的${eureka-container-name}為eureka容器的別名或者id
三、生成鏡像
1.項目eureka\config分別打包上傳服務器
2.Dockerfile
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD ROOT.jar app.jar
#RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
EXPOSE ${PORT}
注意上面的${PORT} 應替換成eureka\config設置的不同容器聲明端口
3.build 生成鏡像
docker build -t orginaztion/service-image-name:tag
1
其中-t 標識鏡像tag ,格式為:所屬庫/服務鏡像名:版本
四、運行鏡像
其中localhost???port替換為宿主機開放端口,{localhost-*-port}替換為宿主機開放端口,localhost???port替換為宿主機開放端口,{container-*-port}替換為容器服務端口
1.啟動eureka
docker run --name eureka-service -d -it -p ${localhost-eureka-port}:${container-eureka-port} orginaztion/service-image-name:tag
1
注意:
并且name對應config中的注冊機參數的${eureka-container-name}
2.啟動config
docker run --name config-server ?--link eureka-server:eureka-server-name-alias -d -it -p ${localhost-config-port}:${container-config-port} -v ${local-config-path}:${contain-config-path} orginaztion/service-image-name:tag
1
注意:config依賴eureka,用–link鏈接 ,-v指定本地配置路徑鏈接容器相應路徑
3.訪問config配置信息
http://${localhost}:${localhost-config-port}/${prefix-config-name}/test|dev|prod
?
?