Netflix Feign

学习Netflix Feign

Imagem de capa

理解

疑问

怎么结合Hystrix(熔断)的?

怎么结合Ribbon(负载均衡)?

如果在启动时遇到如下错误? placeholder

ribbon:
  eureka:
    enabled: true
  ReadTimeout: 120000
  ConnectTimeout: 120000
  MaxAutoRetries: 0
  MaxAutoRetriesNextServer: 1
  OkToRetryOnAllOperations: false


hystrix:
  threadpool:
    default:
      coreSize: 1000 ##并发执行的最大线程数,默认10
      maxQueueSize: 1000 ##BlockingQueue的最大队列数
      queueSizeRejectionThreshold: 500 ##即使maxQueueSize没有达到,达到queueSizeRejectionThreshold该值后,请求也会被拒绝
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 50000
          strategy: SEMAPHORE

placeholder

架构图

placeholder

创建项目

前提环境 IDEA,JDK1.8

server:
  port: 8082

spring:
  application:
    name: user

eureka:
    instance:
        statusPageUrlPath: /info
        healthCheckUrlPath: /health
        prefer-ip-address: true
        ip-address: 127.0.0.1
        <module>user</module>
<?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>com.springcloud</groupId>
    <artifactId>user</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>user</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.springcloud</groupId>
        <artifactId>cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

server:
  port: 8081

spring:
  application:
    name: manage

eureka:
    instance:
        statusPageUrlPath: /info
        healthCheckUrlPath: /health
        prefer-ip-address: true
        ip-address: 127.0.0.1
package com.springcloud.user.rest;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 测试访问 下面@FeignClient("manage")中的manage是需要访问的微服务名称
 * 定义一个getuserinfo接口,"method"代表请求方式,"value"代表请求路径
 * @Author yudong
 * @Date 2018年05月29日 上午12:51
 */
@FeignClient("manage")
public interface FeignTest {

    @RequestMapping(method = RequestMethod.GET, value = "/getuserinfo")
    public String getuserinfo();
}
package com.springcloud.user.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试controller
 * @Author yudong
 * @Date 2018年05月29日 上午12:51
 */
@RestController
public class TestController {

    //注入之前定义的接口
    @Autowired
    FeignTest feignTest;

    @RequestMapping(method = RequestMethod.GET, value = "/getuser")
    public String getmanage(){
       return feignTest.getuserinfo();
    }
}
package com.springcloud.manage.rest;

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

/**
 * 测试controller
 * @Author yudong
 * @Date 2018年05月29日 上午12:48
 */
@RestController
public class TestController {

    @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)
    public String getuserinfo() {
        return "我是manage";
    }
}