zoukankan      html  css  js  c++  java
  • 创建服务消费者(Feign)

    概述

    Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单。使用 Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS 注解。Feign 支持可插拔的编码器和解码器。Feign 默认集成了 Ribbon,并和 Eureka 结合,默认实现了负载均衡的效果

    • Feign 采用的是基于接口的注解

    • Feign 整合了 ribbon和hystrix

    创建服务消费者

            <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-starter-eureka</artifactId>
           </dependency>

           <!--spring cloud starter feign-->
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-starter-feign</artifactId>
           </dependency>

           <dependency>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-web</artifactId>
           </dependency>

    主要增加对Feign的依赖

    Application

    通过@EnableFeignClients注解开启Feign功能


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

    @EnableFeignClients
    @EnableDiscoveryClient
    @SpringBootApplication
    public class FeignApplication {

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

    application.yml

    server:
    port: 9002

    eureka:
    client:
      serviceUrl:
        defaultZone: http://localhost:1111/eureka/
    instance:
      lease-renewal-interval-in-seconds: 10 #服务续约
      lease-expiration-duration-in-seconds: 15 #服务剔除>服务续约时间
      hostname: localhost
      instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
    spring:
    application:
      name: eureka-client-feign
    feign:
    client:
      config:
        eureka-provider:
        #配置feign日志级别
          logger-level: full
     #开启feign对hystrix的支持,默认为false
    hystrix:
      enabled: true
    logging:
    level:
    #必须设置feign的服务包日志级别为debug
      com.ley.springcloud.feign.service: debug

    创建Feign接口

    通过@FeignClient("服务名")注解来指定调用哪个服务

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

    @FeignClient(name = "eureka-provider")
    public interface HelloService {

       @GetMapping("/hello")
       String hello();
    }

    创建测试用的Controller

    import com.ley.springcloud.feign.service.HelloService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    @RequestMapping("/api")
    public class FeignController {

       @Autowired
       private HelloService helloService;

       @GetMapping("/feign/hello")
       public String hello() {
           return helloService.hello();
      }
    }
  • 相关阅读:
    hihocoder [Offer收割]编程练习赛14 投掷硬币
    hihocoder [Offer收割]编程练习赛14 小Hi和小Ho的礼物
    CodeForces
    [HNOI2004] 打砖块
    CodeForces
    hdu4028 The time of a day[map优化dp]
    hdu5009 Paint Pearls[指针优化dp]
    hdu4719 Oh My Holy FFF[线段树优化dp]
    hdu1024 Max Sum Plus Plus[降维优化好题(貌似以后可以不用单调队列了)]
    hdu3709 Balanced Number[数位dp]
  • 原文地址:https://www.cnblogs.com/liuenyuan1996/p/10288722.html
Copyright © 2011-2022 走看看