• 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是否注册成功。

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

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


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

    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,显示如下:


    说明增加的Hystrix已经生效。

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

    总结

    本文主要简单实现了feign作为服务消费的简单应用和Hystrix的集成。

    代码地址

    gitHub地址


    《Srping Cloud 2.X小白教程》目录

    转载请注明出处,

    • 联系方式:4272231@163.com
  • 相关阅读:
    Java 单链表的倒置
    Android查询:模拟键盘鼠标事件(adb shell 实现)
    安卓 发送短信两种方式
    java tcp socket实例
    Java中读取某个目录下的所有文件和文件夹
    Android剖析和运行机制
    linux下搭建android NDK开发环境
    把log存起来
    判断Android系统net和wap接入点的开发实例
    android 4.0.4系统下实现apk的静默安装和启动
  • 原文地址:https://www.cnblogs.com/fengfujie/p/11798197.html
Copyright © 2020-2023  润新知