第三篇: 服务消费者(Feign)

本文根据https://blog.csdn.net/forezp/article/details/81040965写出,修正了部分瑕疵,在此对那位博主表示感谢。

上一篇文章讲述通过RestTemplate+Ribbon消费服务。这篇文章主要讲述如何通过Feign去消费服务。

一、Feign简介

Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性。

Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。

简而言之:

  • Feign 采用的是基于接口的注解
  • Feign 整合了ribbon,具有负载均衡的能力
  • 整合了Hystrix,具有熔断的能力

二、准备工作

继续用上一节的工程, 启动eureka-server,端口为8761; 启动eureka-client 两次,端口分别为8762 、8773.

三、创建一个feign的服务

新建一个spring-boot工程,取名为eureka-serice-feign,在它的pom文件引入Feign的起步依赖spring-cloud-starter-feign、Eureka的起步依赖spring-cloud-starter-netflix-eureka-client、Web的起步依赖spring-boot-starter-web,代码如下:

View Code

在工程的配置文件application.yml文件,指定程序名为service-feign,端口号为8765,服务注册地址为http://localhost:8761/eureka/ ,代码如下:

View Code

在程序的启动类ServiceFeignApplication,加上@EnableFeignClients注解开启Feign的功能:

View Code

定义一个feign接口,通过@FeignClient(“服务名”),来指定调用哪个服务。比如在代码中调用了service-hi服务的“/hi”接口,代码如下:

View Code

在Web层的controller层,对外暴露一个”/hi”的API接口,通过上面定义的Feign客户端SchedualServiceHi 来消费服务。代码如下:

View Code

启动程序,多次访问http://localhost:8765/hi?name=sun,浏览器交替显示:

hi sun ,i am from port:8762

hi sun ,i am from port:8763

聪明如你,一定可以想到传对象的问题,下面来看看feign如何传对象给服务生产者:

1、eureka-client工程新建一个实体类User:

package com.sun;

public class User {
    private Long id;
    private String username;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    
    
}
View Code

2、新建controller:UserController代码如下:

package com.sun;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping(value="users")
public class UserController {
    
    @RequestMapping(value="/list",method={RequestMethod.GET,RequestMethod.POST,RequestMethod.PUT})
    @ResponseBody
    public User list(@RequestBody User user) throws InterruptedException{
        user.setUsername(user.getUsername().toUpperCase());
        user.setId(user.getId()+100l);
        return user;
    }
}
View Code

注意到接收参数是由@RequestBody User user接收。

3、eureka-serice-feign工程新建实体类User,代码和eureka-client一样。

4、新建RemoteService,作为消费的service:

package com.sun;

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

import feign.Headers;
import feign.Param;

@FeignClient(value = "service-hi")
public interface RemoteService {
    @Headers({"Content-Type: application/json","Accept: application/json"})
    @RequestMapping(value = "/users/list",method = RequestMethod.POST)
    String getOwner(@Param(value = "user") User user);
}
最关键的一句是@Headers({"Content-Type: application/json","Accept: application/json"}),提交的是json格式的数据。
接口传了一个User对象。
5、新建UserController,作为controller映射地址:
package com.sun;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
     //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
    @Autowired
    RemoteService remoteService;

    @RequestMapping(value = "/getOwner", method = RequestMethod.GET)
    public String sayHi(@RequestParam String name) {
        User user = new User();
        user.setUsername(name);
        user.setId(100L);
        String result = remoteService.getOwner(user);
        return result;
    }
}

访问http://localhost:8765/getOwner?name=sun,就可以看到返回的json串:

{"id":200,"username":"SUN"}

 
原文地址:https://www.cnblogs.com/PPBoy/p/9376814.html