1. 程式人生 > >springboot+feign+ribbon+hystrix構建高可用的客戶端api訪問

springboot+feign+ribbon+hystrix構建高可用的客戶端api訪問

spring-boot-feign-ribbon-hystrix

在springboot中使用feign、ribbon、hystrix組合功能,構建高可用的外部api訪問

新增feign、ribbon、hystrix對應的依賴

springboot1.5.15+Dalston.SR4版本依賴

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

springboot2.1.0+Greenwich.M1版本依賴

<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-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.22</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

編寫使用者介面用來測試

  1. 使用者訪問介面
@RestController
@Slf4j
public class UserController {

    @RequestMapping(value = "/getUser", method = RequestMethod.POST)
    public Map<String, Object> getUser(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        log.info("來自ip:{}, 請求引數:{}"
, request.getRemoteAddr(), JSON.toJSONString(params)); Integer id = (Integer) params.getOrDefault("id", 1); return ImmutableMap.of("id", id, "name", "xiaoming", "age", 19); } }
  1. feign測試介面
/**
 * <p>fegin test</p>
 * Created by @author [email protected] on 2018/11/2.
 */
@RestController
@Slf4j
public class FeignController {

    @Autowired
    private UserClient userClient;

    @RequestMapping(value = "/getUser/{id}", method = RequestMethod.GET)
    public Map<String, Object> getUser(@PathVariable Integer id){
        log.info("收到客戶端請求getUser id:{}",id);
        return userClient.getUser(ImmutableMap.of("id", id));
    }

}
  1. 編寫feign訪問使用者api介面
/**
 * <p>使用者介面</p>
 * Created by @author [email protected] on 2018/11/2.
 */
@FeignClient(name = "user", fallback = UserClient.UserFallback.class)
@RibbonClient(name = "user")
public interface UserClient {

    /**
     * 測試
     * @param data 測試資料
     * @return 測試資料
     */
    @RequestMapping(method = RequestMethod.POST,
            value = "/getUser",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    Map<String, Object> getUser(Map<String, Object> data);

    @Slf4j
    @Component
    class UserFallback implements UserClient {
        @Override
        public Map<String, Object> getUser(Map<String, Object> data) {
            log.error("fallback:" + JSON.toJSONString(data));
            return ImmutableMap.of("code", -1, "msg","獲取使用者資訊失敗");
        }
    }

}
  1. 配置feign啟用

在類上新增EnableFeignClients註解

/**
 * 在springboot裡使用feign ribbon hystrix組合
 */
@SpringBootApplication
@EnableFeignClients
public class SpringBootFeignRibbonHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootFeignRibbonHystrixApplication.class, args);
    }
}
  1. 配置ribbon負載均衡訪問

在application.properties裡面配置對應@RibbonClient(name = “user”)、@FeignClient(name = “user”)的user配置,並開啟熔斷機制

feign.hystrix.enabled=true
user.ribbon.listOfServers=http://192.168.97.120:8080,http://127.0.0.1:8080

訪問請求

請求地址: http://127.0.0.1:8080/getUser/1

返回:

{
    "id": 1,
    "name": "xiaoming",
    "age": 19
}

可以發現控制檯列印如下,已經達到ribbon負載均衡了,並且配置ribbon地址錯誤之後,會觸發熔斷機制。

控制檯

原始碼

原始碼下載

參考