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();
    }

     
  • 相关阅读:
    支付宝及时到帐接口使用详解
    简便无刷新文件上传系统
    EyesBaby功能实现之窗口拖拽与缩放功能
    Jquery各行换色 click变色
    纯CSS圆角框3-圆角化图片
    WINFORM自定义皮肤制作(上)
    EyesBaby1.0使用帮助文档
    C#实现小写金额转大写金额
    在winform中运用FusionCharts图表(一)
    第一章、基本的圆角框
  • 原文地址:https://www.cnblogs.com/niexinlei/p/9716226.html
Copyright © 2011-2022 走看看