Docker(二)集成部署gitlab+springCloud+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的密码
  1. 因为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命令查看启动成功,运行如下
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_给条出路吧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值