docker swarm部署spring cloud服务

一、准备docker swarm的集群环境

ip 是否主节点  
192.168.91.13  
192.168.91.43  

二、准备微服务

①eureka服务 application.yml配置

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    hostname: localhost

java代码略...

②microserviceone服务 application.yml配置

spring:
  application:
    name: serviceone
  resources:
    static-locations: file:/home/front
  profiles:
    active: pro

server:
  port: 8080

ribbon:
  eureka:
    enabled: true

---
spring:
  profiles: dev
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
    fetch-registry: true
  instance:
    prefer-ip-address: true


---
# docker 环境下使用此配置
spring: profiles: pro cloud: inetutils: preferred-networks: 10.0.0 #正则匹配10.0.0开头的网卡ip eureka: client: service-url: defaultZone: http://discovery:8761/eureka fetch-registry: true instance: prefer-ip-address: true

java代码略...

③microservicetwo服务 application.yml配置

server:
  port: 8081

spring:
  application:
    name: servicetwo
  profiles:
    active: pro

ribbon:
  eureka:
    enabled: true


---
spring:
  profiles: dev
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
    fetch-registry: true
  instance:
    prefer-ip-address: true


---
# docker 环境下使用此配置
spring:
  profiles: pro
  cloud:
    inetutils:
      preferred-networks: 10.0.0 #正则匹配10.0.0开头的网卡ip
eureka:
  client:
    service-url:
      defaultZone: http://discovery:8761/eureka
    fetch-registry: true
  instance:
    prefer-ip-address: true

java代码略...

重点配置:

  preferred-networks: 指定向eureka注册时使用的网卡地址(需要能够被其他微服务访问的ip,微服务间不能访问很可能是注册的网卡ip不对)

  defaultZone: 使用服务的方式指定eureka地址,这里的discovery服务是在deploy.yml配置文件中指定的服务名

  prefer-ip-address: eureka上使用ip地址注册

应用间关系:

  microserviceone、microservicetwo微服务向discovery服务进行注册

  microserviceone通过feign调用microservicetwo的接口

// Feign接口
@FeignClient("servicetwo")
public interface DemoFeignService {

    @RequestMapping(method= RequestMethod.GET,value = "/userlist")
    @ResponseBody List getUserList();
}

---------------------------------------分割线----------------------------------------

//microserviceone中的controller方法
@Autowired
    private DemoFeignService demoFeignService;

    @RequestMapping(method = RequestMethod.GET,value = "invocation")
    public String testInvoke(){
        List list = demoFeignService.getUserList();
        String result = JSONObject.toJSONString(list);
        return "SUCCESS: " + result;
    }

 三、编写docker-compose配置文件

deploy.yml

version: "3"
services:
  discovery:      # 其他微服务可以通过服务名访问此服务,如服务注册http://discovery:8761/eureka
    image: registry.cn-hangzhou.aliyuncs.com/study1990/discovery:1.0
    deploy:
      replicas: 1   #定义 replicated 模式的服务的复本数量
      update_config:
        parallelism: 1    #每次更新复本数量
        delay: 2s       #每次更新间隔
      restart_policy:
        condition: on-failure     #定义服务的重启条件
    networks:
      - eureka-net
    ports:
      - "8761:8761"
    # environment:
      # - spring.profiles.active=peer1
  serviceone: # 微服务1
    image: registry.cn-hangzhou.aliyuncs.com/study1990/simple-microserviceone:1.0
    deploy:
      replicas: 1   #定义 replicated 模式的服务的复本数量
      update_config:
        parallelism: 1    #每次更新复本数量
        delay: 2s       #每次更新间隔
      restart_policy:
        condition: on-failure     #定义服务的重启条件
    networks:
      - eureka-net
    ports:
      - "8080:8080"
    environment:
      - spring.profiles.active=pro
    depends_on:
      - discovery #依赖服务
  servicetwo:
    image: registry.cn-hangzhou.aliyuncs.com/study1990/simple-microservicetwo:1.0
    deploy:
      replicas: 1   #定义 replicated 模式的服务的复本数量
      update_config:
        parallelism: 1    #每次更新复本数量
        delay: 2s       #每次更新间隔
      restart_policy:
        condition: on-failure     #定义服务的重启条件
    networks:
      - eureka-net
    ports:
      - "8081:8081"
    environment:
      - spring.profiles.active=pro
    depends_on:
      - discovery
networks:
  eureka-net:            # 创建网络
    driver: overlay

 

四、部署

  1、在docker swarm主节点上执行 docker stack -c deploy.yml microservice 创建名为microservice的stack服务

  2、docker stack ps microservice查看docker服务情况

  docker swarm部署spring cloud服务

  3、其他相关命令

      docker stack rm microservice  删除名为microservice的stack服务

      docker service ls 查看服务列表

 

上一篇:docker swarm的常用操作


下一篇:Docker swarm集群增加节点和删除节点