zoukankan      html  css  js  c++  java
  • springcloud--Feign(WebService客户端)

    Feign是一个声明式的Web服务客户端,使用Feign可使得Web服务客户端的写入更加方便。
    它具有可插拔注释支持,包括Feign注解和JAX-RS注解、Feign还支持可插拔编码器和解码器、Spring Cloud增加了对Spring MVC注释的支持,并HttpMessageConverters在Spring Web中使用了默认使用的相同方式。Spring Cloud集成了Ribbon和Eureka,在使用Feign时提供负载平衡的http客户端。

    Feign利用RestTemplate对http请求进行封装。

    实际操作(基于springcloud入门案例):

    1:修改pom文件(添加依赖)

    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
    <version>1.4.2.RELEASE</version>
    </dependency>
    </dependencies>
    2:配置文件(application.yml):

    server:
    port: 6002
    eureka:
    client:
    service-url:
    defaultZone: http://localhost:9001/eureka
    instance:
    instance-id: consumer6001
    prefer-ip-address: true
    spring:
    application:
    name: consumer2

    3:启动类(App):
     
    4:编写接口(HelloService):
    public Map<String,Object> h();
    在接口上面添加注解@FeignClient(value = "XXX")
      当前service和方法绑定。就是提供者配置文件中spring.application.name: 的值
    在方法上面添加注解@GetMapping("XXX")
      XXX是提供者的请求地址
    5:编写控制器(HelloController):
    注入service,调用方法。

    @Autowired
    private HelloService service;

    @RequestMapping("jj")
    public Map<String,Object> hello(){
    return service.h();
    }

     
  • 相关阅读:
    [原创]利用Browser协议探测内网主机操作系统版本(无需端口无视防火墙)
    [EXP]Microsoft Windows 10 (Build 17134)
    [EXP]Microsoft Windows
    [EXP]Apache Spark
    [EXP]Adobe ColdFusion 2018
    [EXP]ThinkPHP 5.0.23/5.1.31
    [EXP]Cisco RV110W
    [EXP]Huawei Router HG532e
    [EXP]Microsoft Windows CONTACT
    [EXP]Microsoft Windows 10
  • 原文地址:https://www.cnblogs.com/niexinlei/p/9716226.html
Copyright © 2011-2022 走看看