美文网首页
7.SpringCloud-EurekaClient-Docke

7.SpringCloud-EurekaClient-Docke

作者: 元代码 | 来源:发表于2018-07-30 16:41 被阅读0次

    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 在两个互联的容器之间创建了一个安全隧道,而且不用映射它们的端口到宿主主机上,从而避免了暴露数据库端口到外部网络上。

    相关文章

      网友评论

          本文标题:7.SpringCloud-EurekaClient-Docke

          本文链接:https://www.haomeiwen.com/subject/klifvftx.html