7.SpringCloud-EurekaClient-Docker

EurekaClient可以在EurekaServer注册服务,每个模块都是一个EurekaClient,工程中所有模块都注册到服务中心,模块间通过注册的服务相互访问。
1 工程搭建
1.1 创建工程,加入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</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>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
    <version>1.3.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.43</version>
</dependency>
<dependency>
    <groupId>p6spy</groupId>
    <artifactId>p6spy</artifactId>
    <version>3.6.0</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

1.2 修改启动application加注解

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
@RestController
@EnableAsync
@RefreshScope

1.3 配置文件

eurekaService:
  service1: eurekaService1
  service2: eurekaService2
  service3: eurekaService3
spring:
  cloud:
    config:
      name: config-client # git仓库中 config/目录下的配置文件名称
      label: master # 分支名
      profile: development # 配置文件后缀
      discovery:
        enabled: true
        serviceId: configserverdocker
  rabbitmq:
    host: rabbit1
    port: 5672
    username: myuser
    password: mypass
  zipkin:
    baseUrl: http://localhost:9411
  sleuth:
    sampler:
      percentage: 1.0
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:mysql://192.168.1.6:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456
    # 下面为连接池的补充设置,应用到上面所有数据源中
    # 初始化大小,最小,最  大
    initialSize: 5
    minIdle: 5
    maxActive: 20
    # 配置监控统计拦截的filters
    filters: stat
    # 配置获取连接等待超时的时间
    maxWait: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
    # 验证语句
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    # 从连接池获取连接时,是否检测连接可用性
    testOnBorrow: false
    # 释放连接到连接池时,是否检测连接可用性
    testOnReturn: false
    # 打开PSCache,并且指定每个连接上PSCache的大小
    poolPreparedStatements: true
    # psCache缓存 100
    maxOpenPreparedStatements: 100
    maxPoolPreparedStatementPerConnectionSize: 50
    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    # 对于建立连接过长的连接强制关闭
    removeAbandoned: true
    # 如果连接建立时间超过了30分钟,则强制将其关闭
    removeAbandonedTimeout: 1800
    # 将当前关闭动作记录到日志
    logAbandoned: true
mybatis:
  configLocations: classpath:config/mybatis-config.xml
  mapperLocations: classpath:mapper/*.xml
  typeAliasesPackage:
mapper:
  mappers: com.gao.eurekaclientdockera.base.MyMapper
  notEmpty: false
  identity: MYSQL
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql
eureka:
  instance:
    preferIpAddress: true
  client:
    serviceUrl:
      defaultZone: http://${eurekaService.service1}:8888/eureka/,http://${eurekaService.service2}:8887/eureka/,http://${eurekaService.service3}:8886/eureka/
feign:
  hystrix:
    enabled: true

2.启动
2.1 配置compose.xml

version: "3"
services:
  modulea:
    image: docker.registry.server:5000/eurekaclientdockera
    ports:
      - "8001:8001"
    networks:
      default:
        ipv4_address: 182.18.0.120
    deploy:
      replicas: 2
      resources:
        limits:
          cpus: "2"
          memory: 500M
      restart_policy:
        condition: on-failure
    logging:
      options:
        max-size: "10m"
        max-file: "10"
networks:
  default:
    external:
      name: eureka-net

2.2 区别于基础服务命名为ma

docker stack deploy -c docker-compose.yml ma

注意:
1.调用主机不需要使用固定ip使用host:[service 名]
2.eurekaserver注册显示localhost 需要设置

eureka:
  instance:
    preferIpAddress: true
http://www.itmuch.com/spring-cloud-code-read/spring-cloud-code-read-eureka-registry-ip/

常用命令
查看服务日志

docker service logs -f 651c45c56bc9

查看容器日志
1.docker attach 651c45c56bc9
使用“docker attach”命令进入container(容器)有一个缺点,那就是每次从container中退出到前台时,container也跟着退出了。
2.进入container 使用docker container ls查看container ID

docker exec -it 651c45c56bc9 /bin/bash

检查网络

docker inspect 651c45c56bc9

使用Links
可以通过-link name:alias命令连接指定容器,Docker 在两个互联的容器之间创建了一个安全隧道,而且不用映射它们的端口到宿主主机上,从而避免了暴露数据库端口到外部网络上。

推荐阅读更多精彩内容

  • 一、Docker 简介 Docker 两个主要部件:Docker: 开源的容器虚拟化平台Docker Hub: 用...
    R_X阅读 3,825评论 0 27
  • 如果一件事,当你不做便如身未定、便觉心难安,愧疚与难过像无边的水汽一点点漫过自己,纵然深夜将至,一日告罄,仍仿佛被...
    周简生阅读 212评论 0 0