6.SpringCloud-ConfigServer-Docker

在RabbitMQ搭建好的基础上,部署ConfigServer,ConfigServer部署相对简单,要启动多个实例即可。
1.创建ConfigServer集群
1.1 创建工程,并引入eureka-server

        <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>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

1.2修改启动application加注解

@EnableConfigServer

1.3配置文件
application-config1.yml

eurekaService:
  service1: eurekaService1
  service2: eurekaService2
  service3: eurekaService3
spring:
  application:
    name: configserverdocker
  cloud:
    config:
      server:
        git:
          uri: https://github.com/gaojingyuan/springcloud
          searchPaths: config
          username:
          password:
      label: master
  rabbitmq:
    host: rabbit1
    port: 5672
    username: myuser
    password: mypass
server:
  port: 8866
eureka:
  instance:
# 通过docker stack deploy -c docker-compose.yml base启动时 eurekaserver上注册的是localhost configclient通过localhost访问不到configserver
# 注:使用docker-compose up时没有上述问题 localhost位置是container的ID
# 加入这个配置 虽然eurekaserver仍然显示localhost 但是configclient访问时能访问到当前config主机的ip
    preferIpAddress: true
  client:
    serviceUrl:
      defaultZone: http://${eurekaService.service1}:8888/eureka/,http://${eurekaService.service2}:8887/eureka/,http://${eurekaService.service3}:8886/eureka/
# https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Security-2.0
management:
  endpoints:
    web:
      exposure:
        include: '*'

application-config2.yml,application-config1.yml同上,把端口分别设置为8865和8864
2 启动
2.1 配置compose.xml
由于使用了RabbitMQ,在ConfigServer配置文件中需要配置IP地址,所以在compose文件的network中设置了网段,并且给rabbigmq指定了IP。

version: "3"
services:
  rabbit1:
    image: bijukunjummen/rabbitmq-server:3.7.0
    networks:
      eureka-net:
        ipv4_address: 182.18.0.10
    ports:
      - "5672:5672"
      - "15672:15672"
    environment:
      - RABBITMQ_DEFAULT_USER=myuser
      - RABBITMQ_DEFAULT_PASS=mypass
  eurekaService1:      # eureka server
    image: docker.registry.server:5000/eurekaserverdocker
    networks:
      - eureka-net
    ports:
      - "8888:8888"
    environment:
      - spring.profiles.active=eurekaService1
  config1:      # config server
    image: docker.registry.server:5000/configserverdocker
    networks:
      - eureka-net
    ports:
      - "8866:8866"
    environment:
      - spring.profiles.active=config1
# 使用已有的network 提前用命令创建 docker network create -d overlay --subnet 182.18.0.0/24 --gateway=182.18.0.1 eureka-net
networks:
  default:
    external:
      name: eureka-net
# 新建network
# networks:
#   eureka-net:
#     driver: overlay
#     ipam:
#       config:
#         - subnet: 182.18.0.0/24

2.2 本地启动
本地启动直接使用命令,名为base提供基础服务

docker stack deploy -c docker-compose.yml base

也可使用以下命令启动以便观察控制台

docker-compose up

2.3 线上服务器启动
由于compose文件包含了帐号、密码,子网IP等信息,出于安全考虑compose文件不必拷贝到服务器,
可以在本地使用CloudAgent进行线上部署
3 关于compose文件中各个模块启动顺序的问题,
虽然提供了links以及depends_on等关键字来表示各个模块的启动关系

links:
- rabbitmq
depends_on:
- rabbitmq 

但是这种设置并不能保证rabbitmq启动完成,只能保证工程的启动顺序,并不能保证依赖的工程已经启动完成。
这种设计的目的是让开发者保证程序的健壮性,开发者保证有容错机制,当目标服务不可用时进行重连。
如果一定要先启动一些服务,再启动另外一些服务,主要有2种方式:
1.定义多个compose文件,使用同一个network,第一个compose启动完成后,再用下一个compose启动。
2.定义脚本,验证服务是否启动。
参考
https://www.jianshu.com/p/9446f210e327
https://github.com/vishnubob/wait-for-it

参考:
多个compose文件使用同一network
https://blog.csdn.net/gezhonglei2007/article/details/51627969
手动创建network
docker network create -d overlay –subnet 182.18.0.0/24 eureka-net
https://blog.csdn.net/gezhonglei2007/article/details/51627821

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值