第四章 spring cloud feign(ribbon+hystrix)

1.ribbon

客戶端負載均衡,從服務中心獲取服務列表,按照負載均衡策略進行負載均衡

2.hystrix

熔斷機制,如超時、信號量佔滿、線程池佔滿、網絡異常等,服務降級,無需等待響應,直接返回結果,避免故障蔓延

3.feign=ribbon+hystrix

由於每個客戶端都需要包含ribbon+hystrix,故處理了封裝框架feign

3.1 A服務依賴B服務,A需要熔斷機制

3.2 熔斷機制必須面向接口編程,如B服務有一個helloService接口,實現了三個不同的方法

3.3

啓動類增加@EnableFeignClients

A服務實現B接口進行熔斷,A服務實現類HelloServiceFallBack implements HelloService,方法return爲降級結果,如return “error”

A服務增加註解,A服務使用feign框架@FeignClient(name=“B服務名稱”,fallback=HelloServiceFallBack.class)

A服務調用類RequestMapping注入A服務相應接口,調用B服務

5.feign hystrix搭建完整過程

5.1 pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>misFeignTest</groupId>
  <artifactId>misFeignTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>misFeignTest Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/>
  </parent>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <springCloud.version>Greenwich.RELEASE</springCloud.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      <version>2.0.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-openfeign</artifactId>
      <version>2.0.1.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
  </dependencies>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${springCloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <build>
    <finalName>misFeignTest</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

5.2 application.properties

spring.application.name=feign-consumer
server.port=9001

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka

mis-api-service.ribbon.ConnectTimeout=20000
mis-api-service.ribbon.ReadTimeout=10000
feign.hystrix.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=4000

5.3 A調用B,接口編程

package rest.test;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
//mis-api-service——》B服務,@RequestMapping("/hello")——》B服務api
@FeignClient(value = "mis-api-service",fallback = HystrixHelloService.class)
public interface HelloService {
    @RequestMapping("/hello")
    String hello();
}

5.4 hystrix熔斷編程

package rest.test;

import org.springframework.stereotype.Component;

@Component
public class HystrixHelloService implements HelloService {
    @Override
    public String hello() {
        return "error";
    }
}

5.5 B服務sleep一段時間,對應application.properties裏的

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=4000

B服務api:

@RequestMapping(value="/hello")
	public String testHello(){
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return "hello world";
	}

 5.6 啓動類,啓動測試

package rest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

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

根據sleep毫秒數,返回“hello world”或者“error”

 

 

 

 

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