zoukankan      html  css  js  c++  java
  • springcloud第七步:fegin客户端调用工具

    什么是Feign

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

    简而言之:

    • Feign 采用的是基于接口的注解
    • Feign 整合了ribbon

    创建service-order-feign工程

    Maven依赖

    <parent>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-starter-parent</artifactId>

                <version>1.5.2.RELEASE</version>

                <relativePath /> <!-- lookup parent from repository -->

          </parent>

     

          <properties>

                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

                <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

                <java.version>1.8</java.version>

          </properties>

     

          <dependencies>

                <dependency>

                      <groupId>org.springframework.cloud</groupId>

                      <artifactId>spring-cloud-starter-eureka</artifactId>

                </dependency>

                <dependency>

                      <groupId>org.springframework.cloud</groupId>

                      <artifactId>spring-cloud-starter-ribbon</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>

                </dependency>

                <dependency>

                      <groupId>org.springframework.boot</groupId>

                      <artifactId>spring-boot-starter-test</artifactId>

                      <scope>test</scope>

                </dependency>

          </dependencies>

     

          <dependencyManagement>

                <dependencies>

                      <dependency>

                           <groupId>org.springframework.cloud</groupId>

                           <artifactId>spring-cloud-dependencies</artifactId>

                           <version>Dalston.RC1</version>

                           <type>pom</type>

                           <scope>import</scope>

                      </dependency>

                </dependencies>

          </dependencyManagement>

     

          <build>

                <plugins>

                      <plugin>

                           <groupId>org.springframework.boot</groupId>

                           <artifactId>spring-boot-maven-plugin</artifactId>

                      </plugin>

                </plugins>

          </build>

     

          <repositories>

                <repository>

                      <id>spring-milestones</id>

                      <name>Spring Milestones</name>

                      <url>https://repo.spring.io/milestone</url>

                      <snapshots>

                           <enabled>false</enabled>

                      </snapshots>

                </repository>

          </repositories>

    application.yml配置

    eureka:

      client:

        serviceUrl:

          defaultZone: http://localhost:8888/eureka/

    server:

      port: 8765

    spring:

      application:

        name: service-order-feign

    编写service,调用service-member

    @FeignClient("service-member")

    public interface MemberFeign {

          @RequestMapping("/getUserList")

          public List<String> getOrderByUserList();

    }

    @FeignClient 需要调用服务名称,@RequestMapping服务请求名称

    演示效果

    @SpringBootApplication

    @EnableEurekaClient

    @EnableFeignClients

    public class OrderFeignApp {

     

          public static void main(String[] args) {

                SpringApplication.run(OrderFeignApp.class, args);

          }

     

    }

  • 相关阅读:
    【Unity】第10章 Mecanim动画系统
    【Unity】9.3 粒子系统生成器详解
    只用一招,让你Maven依赖下载速度快如闪电
    原来实现项目多环境打包部署是如此的简单
    手写Maven的archetype项目脚手架
    通俗易懂详解Java代理及代码实战
    谈谈分布式版本管理工具Git
    拥抱了IDEA却发现再也回不去Eclipse...
    解决Redisson出现Failed to instantiate [org.redisson.api.RedissonClient]: Factory method 'create' threw exception; nested exception is java.lang.ArrayIndexOutOfBoundsException: 0的问题
    解决org.apache.shiro.session.UnknownSessionException: There is no session with id的问题
  • 原文地址:https://www.cnblogs.com/XJJD/p/10452531.html
Copyright © 2011-2022 走看看