zoukankan      html  css  js  c++  java
  • springcloud-Feign基础使用

    声明式REST客户端:Feign

    Feign是一个声明式的Web服务客户端。它使得Web服务客户端的写入更加方便。具有可插拔注解支持,包括Feign注解和JAX-RS注解。

    Spring Cloud增加了对Spring MVC注释的支持,并且使用了在Spring Web中默认使用的相同的HttpMessageConverter。Spring Cloud集成

    了Ribbon和Eureka,在使用Feign时提供了负载均衡的http客户端。

    服务提供方就是个简单的EurekaClient+web应用(spring.application.name : service-provider)。控制层如下:

    @RestController
    public class IndexController {
    
        @Autowired
        private IUserService userService;
    
        @GetMapping("/find/{id}")
        public User findById(@PathVariable Long id) {
            return userService.findById(id);
        }
    
        @PostMapping("/findUser")
        public User findOne(@RequestBody User user) {
            return user;
        }
    
    }

    服务调用方项目

    pom依赖:

          <dependency>
                <!-- eureka客户端 -->
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <!-- feign -->
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
            </dependency>        

    这里只依赖了Feign,没有依赖Hystrix和Ribbon

    application.yml文件配置:

    server:
      port: 18082
        
    spring:
      application:
        name: service-consumer #应用程序名称
          
    eureka:
      client:
        serviceUrl:
          defaultZone: http://admin:admin@localhost:8761/eureka
      instance:
        prefer-ip-address: true #当猜测主机名时,服务器的IP地址应该在操作系统报告的主机名中使用
        instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}} #更改Eureka实例ID

    入口启动程序,加上@EnableFeignClients注解开启Feign:

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients // 开启feign
    public class ConsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
        }
    
    }

    核心客户端代码:

    /**
     * 使用feign
     */
    @FeignClient(name = "service-provider", fallback = UserFallback.class)
    public interface UserFeignClient {
    
        /**
         * get请求
         */
        @GetMapping("/find/{id}")
        UserEntity findById(@PathVariable("id") Long id); // PathVariable注解必须得设置value
    
        /**
         * post请求
         */
        @PostMapping("/findUser")
        UserEntity findUser(@RequestBody UserEntity user);
    
    }

    @FeignClient注解定义了该接口的一个Feign客户端,name指定了注册到Eureka的服务名,fallback是服务降级后的接口实现类(可参考springcloud-feign的hystrix支持)

    @GetMapping指定了请求的相对url和http请求方式,与服务端一一对应。请求参数和返回类型也得和服务端对应

    遇到的坑:   1、使用@PathVariable时,一定得设置value(这点和spring MVC不一样,算是springcloud的一个坑)

            2、有些SpringCloud版本,这里只支持@RequestMapping,不支持GetMapping和PostMapping

    控制层代码:

    @RestController
    public class FeignController {
    
        @Autowired
        private UserFeignClient feignClient;
    
        @GetMapping("/find/{id}")
        public UserEntity getOne(@PathVariable Long id) {
            UserEntity user = feignClient.findById(id);
            return user;
        }
        
        @GetMapping("/getUser")
        public UserEntity findUser() {
            UserEntity user = new UserEntity();
            user.setId("100");
            user.setAge(10);
            user.setLastName("xuwenjin");
            return feignClient.findUser(user);
        }
    
    }
  • 相关阅读:
    1414 冰雕
    1475 建设国家(优先队列)
    Digit Division
    Sleep Buddies
    Reverse and Compare(DP)
    1536 不一样的猜数游戏
    Genealogical tree
    网站日志实时分析工具GoAccess使用
    CentOS下一键安装Openstack
    权重轮询调度算法(WeightedRound-RobinScheduling)-Java实现2
  • 原文地址:https://www.cnblogs.com/xuwenjin/p/9353314.html
Copyright © 2011-2022 走看看