1、Eureka集成

  • eureka服务端

    • pom依赖

      1
      2
      3
      4
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>
    • yml配置

      1
      2
      3
      4
      5
      6
      7
      8
      9
      eureka: 
      client:
      #是否将自己注册到Eureka Server,默认为true,由于当前就是server,故而设置成false,表明该服务不会向eureka注册自己的信息
      register-with-eureka: false
      #是否从eureka server获取注册信息,由于单节点,不需要同步其他节点数据,用false
      fetch-registry: false
      #设置服务注册中心的URL,用于client和server端交流
      service-url:
      defaultZone: http://localhost:1001/eureka/
    • 代码

      1
      2
      启动类上添加此注解标识该服务为配置中心
      @EnableEurekaServer
  • eureka客户端

    • pom依赖

      1
      2
      3
      4
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
    • yml配置

      1
      2
      3
      4
      5
      6
      #注册中心
      eureka:
      client:
      #设置服务注册中心的URL
      service-url:
      defaultZone: http://root:root@localhost:7900/eureka/

2、Ribbon集成

  • 轮询请求

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static String send(LoadBalancerClient loadBalancerClient, String serviceId, String path){
    ServiceInstance serviceInstance = loadBalancerClient.choose(serviceId);
    String url = Utils.getUrl(serviceInstance.getHost(),String.valueOf(serviceInstance.getPort()), path);
    System.out.println(url);

    RestTemplate restTemplate = new RestTemplate();
    String ret = restTemplate.getForObject(url, String.class);

    return ret;
    }
  • 随机请求

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public static String sendByService(ServiceInstance serviceInstance, String path){
    String url = Utils.getUrl(serviceInstance.getHost(), String.valueOf(serviceInstance.getPort()), path);
    System.out.println(url);

    RestTemplate restTemplate = new RestTemplate();

    return restTemplate.getForObject(url, String.class);
    }

    public static ServiceInstance balance(String serviceId, DiscoveryClient discoveryClient){
    List<ServiceInstance> serviceList = discoveryClient.getInstances(serviceId);
    ServiceInstance serviceInstance = serviceList.get(new Random().nextInt(serviceList.size()));

    return serviceInstance;
    }

3、Feign集成

  • pom依赖

    1
    2
    3
    4
    5
    <!-- 引入feign依赖 ,用来实现接口伪装 -->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    启动类增加:
    //@EnableFeignClients就像是一个开关,只有使用了该注解,OpenFeign相关的组件和配置机制才会生效。
    //@EnableFeignClients还可以对OpenFeign相关组件进行自定义配置
    @EnableFeignClients

    接口定义:
    //一般一个服务提供者,写一个interface
    //此处由于结合了eureka,所以name是 虚拟主机名,默认服务名,请求时 会将它解析成注册表中的服务。
    //不结合eureka,就是自定义一个client名字。就用url属性指定 服务器列表。url=“http://ip:port/”
    //此时的name作用就是创建负载均衡器。
    //也可以添加@RequestMapping
    @FeignClient(name = "ANTI-ADDICTION")
    public interface ClientAntiAddictionFeign {

    @RequestMapping("/anti/realName/")
    public String check();
    }

4、hystrix集成

  • 和restTemplate结合

    • pom依赖

      1
      2
      3
      4
      5
      <!-- 引入hystrix依赖 -->
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
      </dependency>
    • 代码

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
         启动类增加:
      @EnableCircuitBreaker

      调用的方法上,通过使用@HystrixCommand,将方法纳入到hystrix监控中。
      @HystrixCommand(fallbackMethod = "hystrix")
      public String hystrixByRibbon() {
      ServiceInstance serviceInstance = loadBalancerClient.choose(serviceId);
      String url = utils.getUrl(serviceInstance.getHost(),String.valueOf(serviceInstance.getPort()), path);
      System.out.println(url);

      RestTemplate restTemplate = new RestTemplate();
      String ret = restTemplate.getForObject(url, String.class);

      return ret;
      }

      private String hystrix(){
      System.out.println("熔断触发");
      return "熔断触发";
      }
  • 和feign结合

    • pom依赖

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      <!-- 引入feign依赖 -->
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
      </dependency>
      <!-- 引入hystrix依赖 -->
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
      </dependency>
    • yml配置

      1
      2
      3
      4
      //feign自带Hystrix,但是默认没有打开,首先打开Hystrix。(从Spring Cloud Dalston开始,feign的Hystrix 默认关闭,如果要用feign,必须开启
      feign:
      hystrix:
      enabled: true
    • 代码

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      启动类增加:
      @EnableFeignClients
      @EnableCircuitBreaker

      接口定义:
      @FeignClient(name = "REDEEM",fallback = HystrixRedeemFeignImpl.class)
      public interface HystrixRedeemFeign {

      @RequestMapping("/redeem/checkRedeem")
      String hystrixRedeemByFeign();
      }

      接口实现类:
      @Component
      public class HystrixRedeemFeignImpl implements HystrixRedeemFeign {
      @Override
      public String hystrixRedeemByFeign(){
      System.out.println("feign hystrix 触发");
      return "feign hystrix 触发";
      }
      }

5、zuul网关集成

  • pom依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <!--zuul -->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
  • yml配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    server:
    port: 1301

    spring:
    profiles: zuul
    application: zuul
    cloud:
    loadbalancer:
    retry:
    max-retries-on-next-service-instance: 2
    max-retries-on-same-service-instance: 2

    eureka:
    client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
    defaultZone: http://localhost:1105/eureka/

    zuul:
    routes:
    genpe:
    path: /**
    retryable: true
  • 代码

    1
    2
    @EnableZuulProxy
    该注解声明这是一个zuul代理,该代理使用Ribbon来定位注册到eureka server上的微服务,同时,整合了hystrix,实现了容错。

6、Config配置中心集成

  • config服务端

    • git配置仓库

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      获取配置规则:根据前缀匹配
      /{name}-{profiles}.properties
      /{name}-{profiles}.yml
      /{name}-{profiles}.json
      /{label}/{name}-{profiles}.yml

      name 服务名称
      profile 环境名称,开发、测试、生产:dev qa prd
      lable 仓库分支、默认master分支

      匹配原则:从前缀开始。
    • pom依赖

      1
      2
      3
      4
      5
      6
      7
      8
      9
      <!-- 配置中心服务端:config-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>
    • yml配置

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      spring:
      application:
      name: config-server
      cloud:
      config:
      server:
      git:
      uri: https://gitee.com/genpe-git/springcloud-config.git
      username:
      password:
      timeout: 15
      search-paths: config

      server:
      port: 1202

      eureka:
      client:
      register-with-eureka: true
      service-url:
      defaultZone: http://localhost:1105/eureka/
    • 代码

      1
      2
      3
      启动类增加:
      @EnableConfigServer
      @EnableEurekaClient
  • config客户端

    • pom依赖

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      <!-- 配置中心客户端:config-client -->
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-client</artifactId>
      </dependency>

      <!-- web -->
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <!-- eureka客户端 -->
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      </dependency>
    • yml配置

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      server:
      port: 1201

      spring:
      application:
      name: config-client
      profiles: config-client
      cloud:
      config:
      discovery:
      service-id: config-server
      enabled: true
      profile: dev
      label: master
      # uri: http://localhost:1202

      eureka:
      client:
      service-url:
      defaultZone: http://localhost:1105/eureka/
      register-with-eureka: true
      fetch-registry: true

      env: dev
      env1: dev1
    • 代码

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      @RestController
      @RefreshScope
      @RequestMapping("/config")
      public class ConfigClientController {

      @Value("${env}")
      private String env;

      @Value("${env1}")
      private String env1;

      @RequestMapping("/env")
      public String getEnv(){
      return env;
      }

      @RequestMapping("/env1")
      public String getEnv1(){
      return env1;
      }
      }

7、Sleuth链路追踪

  • pom依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <!-- 引入sleuth依赖 监控 -->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>
    <!-- zipkin -->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
    <version>2.2.7.RELEASE</version>
    </dependency>
  • yml配置

    1
    2
    3
    4
    5
    6
    7
    8
    spring:
    zipkin:
    base-url: http://localhost:9411/
    sender:
    type: web
    sleuth:
    sampler:
    rate: 1

8、Admin健康检查

  • admin服务端

    • pom依赖

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      <version>3.0.1</version>
      </dependency>
      <!-- 邮件通知-->
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-mail</artifactId>
      </dependency>
      <dependency>
      <groupId>de.codecentric</groupId>
      <artifactId>spring-boot-admin-starter-server</artifactId>
      </dependency>
      <!-- Admin 界面 -->
      <dependency>
      <groupId>de.codecentric</groupId>
      <artifactId>spring-boot-admin-server-ui</artifactId>
      </dependency>
    • yml配置

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      server:
      port: 1401

      spring:
      profiles: admin
      application:
      name: admin

      eureka:
      client:
      service-url:
      defaultZone: http://localhost:1105/eureka/
      #,http://localhost:1107/eureka/,http://localhost:1106/eureka/
      fetch-registry: true
      register-with-eureka: false
      security:
      user:
      name: root
      password: root
      # 邮件设置
      mail:
      host: smtp.qq.com
      username: 单纯QQ号
      password: xxxxxxx授权码
      properties:
      mail:
      smpt:
      auth: true
      starttls:
      enable: true
      required: true
      boot:
      admin:
      notify:
      mail:
      to: genpe@qq.com
      from: genpe@qq.com
    • 代码

      1
      2
      启动类增加:
      @EnableAdminServer
  • admin客户端

    • pom依赖

      1
      2
      3
      4
      <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
    • yml配置

      1
      2
      3
      4
      5
      6
      7
      8
      9
      management:
      endpoints:
      web:
      exposure:
      include: "*"
      endpoint:
      health:
      show-details: always
      enabled: true