SpringCloud教程 - feign的使用实践:如何优雅的发送请求

682人浏览 / 0人评论 / 添加收藏

总结:

Feign是一个声明性web服务客户端。它使编写web服务客户机更容易。使用Feign创建一个接口并注释它。它具有可插入的注释支持,包括Feign注释和JAX-RS注释。Feign也支持可插拔编码器和解码器。Spring Cloud增加了对Spring MVC注释的支持,并支持使用Spring Web中默认使用的HttpMessageConverters。Spring Cloud集成了Ribbon和Eureka,以及Spring Cloud LoadBalancer,在使用Feign时提供一个负载均衡的http客户端。
 

前文中我们创建了一个普通的消费者端:j-cloud-consumer

在这个工程中,我们使用RestTemplate来发送请求,代码看起来较为凌乱。

接下来进入feign实践:

1、基于上一章节,在j-cloud下新建一个moudle,使用feign的消费者工程:j-cloud-consumer-feign

创建过程参考上一章节。

2、pom.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>cn.jorian.framework</groupId>
   <artifactId>j-cloud-consumer-feign</artifactId>
   <version>1.0.0</version>
   <name>j-cloud-consumer-feign</name>
   <description>Demo project for Spring Boot</description>

   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.5.4.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>
       <cloud.version>Dalston.SR1</cloud.version>
   </properties>
   <dependencies>
       <!-- hystrix断路器 -->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-hystrix</artifactId>
       </dependency>
       <!--LOMBOK-->
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <version>1.16.20</version>
           <scope>provided</scope>
       </dependency>
       <!-- eureka 配置中心 -->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-eureka</artifactId>
       </dependency>
       <!-- fegin -->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-feign</artifactId>
       </dependency>
       <!-- boot-web -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>


   </dependencies>

   <!-- dependency management -->
   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>${cloud.version}</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>

</project>

3、控制器HelloController,代码如下:

注意,此调用服务的方式已经改为使用feign,而非RestTemplate

package cn.jorian.framework.controller;

import cn.jorian.framework.service.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Hellocontroller {
@Autowired
private UserClient feignClient;
/**
 * 此处的mapping是一级controller,调用方法里边绑定了二级的conroller,相当于用http完成一次转发
 * @return
 */
@GetMapping("/hello")
public String hello(){
 return feignClient.sayHello();
}

@GetMapping("/hi")
public String hi(){
 return feignClient.sayHi();
}

@GetMapping("/haha")
public String haha(){
 return feignClient.sayHaha();
}
}

4、启动器JCloudConsumerFeignApplication,代码如下:

此处注意,增加了feign客户端的配置,需要配置feign的代码基础包路径。

package cn.jorian.framework;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

/**
* @author jorian
*/
@SpringCloudApplication
@EnableEurekaClient //表明这是一个eureka客户端
@EnableFeignClients(basePackages = "cn.jorian.*") //开启feign
public class JCloudConsumerFeignApplication {

public static void main(String[] args) {
 // TODO Auto-generated method stub
 SpringApplication.run(JCloudConsumerFeignApplication.class, args);
}
}

5、Feign客户端UserClient,代码如下:

仔细观察代码,使用了@FeignClient来表明是哪个应用

package cn.jorian.framework.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* 这个接口相当于把原来的服务提供者项目当成一个Service类
* @author jorian
*
*/
@FeignClient("PROVIDER-USER")
public interface UserClient {
 /**
  * Feign中没有原生的@GetMapping/@PostMapping/@DeleteMapping/@PutMapping,要指定需要用method进行
  *
  *
  * 接口上方用requestmapping指定是服务提供者的哪个controller提供服务
  */
 @RequestMapping(value="/user/sayHello",method=RequestMethod.GET)
 public String sayHello();

 @RequestMapping(value="/user/sayHi",method=RequestMethod.GET)
 public String sayHi();

 @RequestMapping(value="/user/sayHaha",method=RequestMethod.GET)
 public String sayHaha();
}

6、application.yml文件配置如下:

server:
 port: 8002
spring:
 application:
   name: j-cloud-consumer-feign

# eureka 配置
eureka:
 client:
   serviceUrl:
     defaultZone: http://jorian:123456@localhost:8761/eureka
logging:
 level:
   root: INFO

7、依次启动j-cloud-server,j-cloud-provider1,j-cloud-provider2,j-cloud-consumer-feign

8、在注册中心查看各个服务是否上线

9、访问使用了feign的消费者服务接口:

多次访问http://localhost:8002/hello,可以看到返回结果已经实现了负载均衡。依次返回:

I`m provider 1 ,Hello consumer!

I`m provider 2 ,Hello consumer!

至此,feign的学习已经完成。

 

全部评论