• 创建服务消费者(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();
      }
    }
  • 相关阅读:
    JLOI2014 聪明的燕姿【搜索-细节】
    JOI2014 バス通学【思维】
    【线段树】USACO 08FEB Hotel G
    USACO20FEB Equilateral Triangles P【思维-曼哈顿距离-切比雪夫距离】
    USACO 2019DEC Milk Visits
    【规律】ABC179 E Sequence Sum
    【前缀和优化dp】ABC179 D Leaping Tak
    SPOJ2916 GSS5-Can you answer these queries V【线段树】
    SPOJ1557 GSS2-Can you answer these queries II【线段树】
    推荐系统之余弦相似度的Spark实现
  • 原文地址:https://www.cnblogs.com/liuenyuan1996/p/10288722.html
Copyright © 2020-2023  润新知