Docker(二)集成部署gitlab+springboot+config统一配置中心
1.在gitlab上建立config项目
建立好后使用git clone 拉取到本地
2. IDEA建立spring config工程
1.选择添加相关的依赖如下
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.在入口类添加@EnableConfigServer注解
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
3 配置application.yml文件
spring:
application:
name: wx-config //工程名
cloud:
config:
label: master //在gitlab上的分支
server:
git:
username: gitlab账号
uri: https://gitlab.com/wx2019/config.git //gitlab上拉取的https地址
search-paths: resp //新建放入配置文件的文件夹
password: gitlab的密码
-
因为config模块是独立放到远程服务器上的,所以配置eureka注册中心如下。由于config服务注册到注册中心使用的是远程服务器的私有地址,而不是公有地址,可以通过配置
ip-address: xxx.xxx.xxx.xxx
hostname: ${eureka.instance.ip-address}
来使之注册到注册中心的地址使用公网地址(自己指定的IP地址)eureka: client: register-with-eureka: true #注册到注册中心 service-url: defaultZone: http://xxxxxxxx:xxxx/eureka/ //注册中心的地址 instance: prefer-ip-address: true #访问路径可以显示Ip地址 lease-expiration-duration-in-seconds: 90 #告诉服务端,如果我90s之内没有给你发心跳,就代表我“死”了,将我踢出掉 lease-renewal-interval-in-seconds: 30 #每间隔30s,向服务端发送一次心跳,证明自己依然存活 ip-address: xxx.xxx.xxx.xxx hostname: ${eureka.instance.ip-address}
5建立resp文件夹,将各个分布式模块的yaml配置文件放入其中,并使用Git推送到远程Gitlab的resp文件夹下
gitlab上对应的resp文件夹:
6打包工程,部署到远程服务器上,完成启动。
需要使用配置中心的配置文件的模块
例如我文件模块服务需要使用config配置中心的统一配置文件,工程引入config依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2接着配置优先级最高的bootstrap.yml文件,选择使用对应的配置文件
spring:
cloud:
config:
uri: http://xxx.xx.89.101:8502 //部署config服务模块的远程服务器的IP地址
name: wx2019-file-service //要使用gitlab上对应的配置文件名
label: master //config在gitlab哪个分支上
profile: dev //使用dev后缀版本的配置文件
注意wx2019-file-service 要对应使用gitlab上的配置文件名,如下
打包部署config客户端的服务文件模块
1.使用IDEA的maven工具生成可执行的jar文件
2.配置Dockefile文件,构建文件模块镜像,如下
FROM java:8
MAINTAINER 60kg xxxxx@qq.com
VOLUME /tmp
ADD file-service-0.0.1-SNAPSHOT-exec.jar file-service.jar
RUN bash -c 'touch /file-service.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/file-service.jar"]
3.将该dockerfile文件和jar包部署到远程服务器同一个文件夹目录下,使用如下命令,构建镜像
docker build -t wx-file-service .
4配置docker-compose的启动配置类 docker-compose-service.yml如下
version: "3.1"
services:
spring_file_service:
image: wx-file-service //生成的镜像名
ports:
- "10003:10003" //使用服务器上的端口和对外对应的端口
environment:
- "spring.profiles.active=dev" //配置jar包使用的配置文件为dev版本
- EUREKA_INSTANCE_IP-ADDRESS=47.102.97.30 //将该服务指定对应的IP地址注册到注册中心
- SERVER_PORT=10000
volumes:
- ./wx-file/static:/static //配置数据卷
需要注意的是,如果jar包工程配置文件有test,prod,dev不同的配置文件,需要配置 environment:选项,在启动是时候选择对应的yaml文件启动
最后使用docker-compose的命令启动工程,完事!
docker-compose -f docker-compose-service.yml up -d
最后使用 docker ps命令查看启动成功,运行如下