feign服務調用,hystrix熔斷器,actuator應用監控

一:feign是springCloud跨服務調用的組件,feign底層也是一個基於http的封裝

我現在創建兩個服務,一個消費者,一個生產者:

pom:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

創建一個生產者:

server:
  port: 8020
spring:
  application:
    name: producer

eureka:
  client:
    healthcheck:
      enabled: true #健康檢查
    serviceUrl.defaultZone: http://localhost:8010/eureka/
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
    lease-expiration-duration-in-seconds: 10 # 發呆時間,即服務續約到期時間(缺省爲90s)
    lease-renewal-interval-in-seconds: 5 # 心跳時間,即服務續約間隔時間(缺省爲30s)
    metadata-map:
      cluster: service

feign:
  hystrix:
    enabled: true

創建一個http接口:

package com.example.producer.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: gongyiyang
 * @Date: 2018/10/22 12:47
 * @Description:
 */
@RestController
@RequestMapping("/producer")
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "你好,這是producer";
    }
}

創建一個消費者:
 

server:
  port: 8021
spring:
  application:
    name: consumer
  

eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl.defaultZone: http://localhost:8010/eureka/
  instance:
    #實例顯示格式,默認按照ip,名稱,端口
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
    # 發呆時間,即服務續約到期時間(缺省爲90s)
    lease-expiration-duration-in-seconds: 10
    # 心跳時間,即服務續約間隔時間(缺省爲30s)
    lease-renewal-interval-in-seconds: 5
    metadata-map:
      cluster: service

feign:
  hystrix:
    enabled: true

接口開發:

package com.example.consumer.controller;

import com.example.consumer.servise.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author PC_gongyiyang
 * @Auther: gongyiyang
 * @Date: 2018/10/22 15:47
 * @Description:
 */
@RestController
@RequestMapping("/consumer")
public class FeignHelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String hello() {
        return helloService.hello();
    }
}

調用生產者接口:

package com.example.consumer.servise;

import com.example.consumer.servise.fallback.HelloServiceBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author PC_gongyiyang
 * @Auther: gongyiyang
 * @Date: 2018/10/22 15:44
 * @Description:
 */
@FeignClient(name = "producer",fallback = HelloServiceBack.class)
public interface HelloService {

    @GetMapping("/producer/hello")
    String hello();
}

設置一個異常回調:

package com.example.consumer.servise.fallback;

import com.example.consumer.servise.HelloService;
import org.springframework.stereotype.Component;

/**
 * @author PC_gongyiyang
 * @Auther: gongyiyang
 * @Date: 2018/10/22 16:00
 * @Description:
 */
@Component
public class HelloServiceBack implements HelloService {
    @Override
    public String hello() {
        return "這是consumer的fallback";
    }
}

調用結果:

簡單的配置服務間的調用就完成了!

二:hystrix熔斷器:

在應用啓動時配置熔斷器地址:

package com.example.consumer;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

/**
 * @author PC_gongyiyang
 * @Auther: gongyiyang
 * @Date: 2018/10/22 14:02
 * @Description:
 */
@EnableHystrix
@EnableHystrixDashboard
@EnableFeignClients
@SpringCloudApplication
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

    @SuppressWarnings("Duplicates")
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

訪問熔斷器地址;

可以看到熔斷器正在工作:

三:actuator應用監控:

爲actuator創建一個應用:

pom:

      <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

配置信息:

server:
  port: 8030
spring:
  application:
    name: turbine

eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl.defaultZone: http://localhost:8010/eureka/
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true
    lease-expiration-duration-in-seconds: 10 # 發呆時間,即服務續約到期時間(缺省爲90s)
    lease-renewal-interval-in-seconds: 5 # 心跳時間,即服務續約間隔時間(缺省爲30s)
turbine:
  app-config: consumer,producer,zuul #需要監控的服務名
  aggregator:
    cluster-config: service,zuul #需要監控的服務集羣名,default
  cluster-name-expression: metadata['cluster'] #new String("default")
  combine-host-port: true
  instanceUrlSuffix:
    service: hystrix.stream    ##key爲clusterConfig的集羣名字,默認爲default
    zuul: hystrix.stream    ##value爲集羣的hystrix監控url後綴,springboot2.0默認爲actuator/hystrix.stream


啓動日誌可以看到有哪些熔斷地址:

訪問以下監控頁面:

看一下應用監控詳情:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章