spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)

本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3

本文基于前两篇文章eureka-server和eureka-client的实现。 参考

创建Feign工程

1.1 创建sping boot工程:eureka-feign

1.2 添加pom.xml相关依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
复制代码

1.3 application添加配置信息

spring:
  application:
    name: eureka-feign

server:
  port: 8601

eureka:
  instance:
    hostname: localhost
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
复制代码

1.4 启动类EurekaFeignApplication增加注解

package spring.cloud.demo.eurekafeign;

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

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class EurekaFeignApplication {

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

}
复制代码

@EnableDiscoveryClient:这里使用EnableDiscoveryClient注解,在eureka-client已说明,这边就不在阐述

@EnableFeignClients:启用Feign客户端

1.5 创建服务接口EurekaFeignService

package spring.cloud.demo.eurekafeign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @auther: maomao
 * @DateT: 2019-09-17
 */
@FeignClient(value = "eureka-client")
public interface EurekaFeignService {

    @RequestMapping(value = "/info")
    String syaHello();
}
复制代码

@FeignClient:定义Feign客户端,调用远程client。eureka-client代表client的spring.application.name,调用远程地址示例: http://eureka-client/info

1.6 创建EurekaFeignController控制类

package spring.cloud.demo.eurekafeign.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.cloud.demo.eurekafeign.service.EurekaFeignService;

import javax.annotation.Resource;

/**
 * @auther: maomao
 * @DateT: 2019-09-17
 */
@RestController
@RequestMapping("/feign")
public class EurekaFeignController {

    @Resource
    private EurekaFeignService eurekaFeignService;

    @RequestMapping("/sayHello")
    public String sayHello() {
        return "feign result: " + eurekaFeignService.syaHello();
    }

}
复制代码

1.7 启动eureka-feign服务

可以在eureka-server服务注册中心看到eureka-feign是否注册成功。

![image-20190919175930555](/Users/fengfujie/Library/Application Support/typora-user-images/image-20190919175930555.png)

红框中内容代表eureka-feign已经正常启动并成功注册到eureka-server服务注册中心。

访问http://localhost:8601/feign/sayHello,显示如下:

![image-20190919180340340](/Users/fengfujie/Library/Application Support/typora-user-images/image-20190919180340340.png)

多刷新几次可以在浏览器中看到不通的结果,端口是变化的。

Feign默认的负载均衡策略是轮询方式。如果想修改Feign的负载均衡策略请参考eureka-ribbon中的配置

至此,一个简单的单机Feign服务消费者工程就搭建完成了。

彩蛋

Feign集成Hystrix熔断机制

pom.xml增加依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
复制代码

增加application.yml配置

feign:
  hystrix:
    enabled: true
复制代码

修改EurekaFeignService

在@FeignClient注解中增加fallback

package spring.cloud.demo.eurekafeign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @auther: maomao
 * @DateT: 2019-09-17
 */
@FeignClient(value = "eureka-client", fallback = EurekaFeignServiceFailure.class)
public interface EurekaFeignService {

    @RequestMapping(value = "/info")
    String syaHello();
}
复制代码

增加EurekaFeignServiceFailure类:

package spring.cloud.demo.eurekafeign.service;

import org.springframework.stereotype.Service;

/**
 * @auther: maomao
 * @DateT: 2019-09-17
 */
@Service
public class EurekaFeignServiceFailure implements EurekaFeignService {
    @Override
    public String syaHello() {
        return "网络繁忙,请稍后在试";
    }
}
复制代码

启动eureka-feign服务

首先在eureka-client全不启动的情况,访问http://localhost:8601/feign/sayHello看是否全不正常,然后停掉其中一个eureka-client,在多次刷新http://localhost:8601/feign/sayHello,显示如下:

![image-20190919181813532](/Users/fengfujie/Library/Application Support/typora-user-images/image-20190919181813532.png)

说明增加的Hystrix已经生效。

至此,Feign集成Hystrix熔断机制全部完成。

我来评几句
登录后评论

已发表评论数()

相关站点

+订阅
热门文章