1. 首页
  2. IT教程

SpringCloud 服务消费者(Feign)

Feign简介

Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创建一个接口并对它进行注解,它具有可插拔的注解支持包括Feign注解与JAX-RS注解,Feign还支持可插拔的编码器与解码器,Spring Cloud 增加了对 Spring MVC的注解。在Spring Cloud中使用Feign, 我们可以做到使用HTTP请求远程服务时能与调用本地方法一样的编码体验。Feign 采用的是基于接口的注解,Feign 整合了ribbon,具有负载均衡的能力。

准备工作

启动注册中心eureka-server,服务提供者say-hello。对这两个项目各自启动两个实例。

创建Feign客户端

1.新建一个springboot工程,取名为service-feign

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.definesys</groupId>
        <artifactId>my_cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.definesys</groupId>
    <artifactId>service-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>service-feign</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.修改配置文件

server.port=4444

eureka.client.service-url.defaultZone=http://server2:11112/eureka/,http://server1:11111/eureka/

spring.application.name=service-feign

3.修改启动类

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServiceFeignApplication.class, args);
    }

}

4.定义Feign接口
通过@ FeignClient(“服务名”),来指定调用哪个服务。

@FeignClient(value = "say-hello")
public interface SayHelloFeignSer {
    @RequestMapping(value = "/sayHello",method = RequestMethod.GET)
    String feignSayHelloSer(@RequestParam(value = "helloName") String helloName);
}

5.创建controller,对外提供接口

@RestController
public class FeignServiceController {

    //编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
    @Autowired
    private SayHelloFeignSer sayHelloFeignSer;

    @GetMapping("/feignSayHello")
    public String feignSayHelloCtr(@RequestParam("helloName")String helloName){
        return sayHelloFeignSer.feignSayHelloSer(helloName);

    }
}

6.启动service-feign
访问http://localhost:4444/feignSayHello?helloName=adaad,发现浏览器交替显示端口,说明feign已经集成ribbon。

Feign多参数请求

1.修改say-hello项目,在SayHelloController中添加两个方法

/**
     *  get请求多请求参数
     * @param userName
     * @param userPassword
     * @return
     */
    @RequestMapping(value = "/manyParams",method = RequestMethod.GET)
    public String manyParamsCtr(@RequestParam("userName")String userName,@RequestParam("userPassword")String userPassword){
        return "用户名:"+userName+",用户密码"+userPassword;
    }

    /**
     * post 对象参数
     * @param user
     * @return
     */
    @RequestMapping(value = "/objParams",method = RequestMethod.POST)
    public User objParamsCtr(@RequestBody User user){
        System.out.println(JSON.toJSON(user));
        return user;
    }
public class User {
    private String userName;

    private String userPassword;

    private String userSex;
    
    ...get(),set()
}

2.修改service-feign项目Feign接口

/**
     * 多参数get请求
     * @param userName
     * @param userPassword
     * @return
     */
    @RequestMapping(value = "/manyParams",method = RequestMethod.GET)
    String manyParamsSer(@RequestParam("userName")String userName,@RequestParam("userPassword")String userPassword);

    /**
     * 对象参数 post请求
     * @param user
     * @return
     */
    @RequestMapping(value = "/objParams",method = RequestMethod.POST)
    Object objParamsCtr(@RequestBody Object user);

3.修改service-feign项目FeignServiceController

    @GetMapping("/feignManyParams")
    public String feignManyParamsCtr(@RequestParam("userName")String userName,@RequestParam("userPassword")String userPassword){
        return sayHelloFeignSer.manyParamsSer(userName,userPassword);
    }

    @PostMapping("/feignObjParam")
    public Object feignObjParamCtr(@RequestBody Map<String,Object> map){
        return sayHelloFeignSer.objParamsCtr(map);
    }

注:为了不重复创建User实体类,这里用map去接收参数。
4.重新启动say-hello,service-feign两个项目
通过postman访问 /feignManyParams接口

SpringCloud 服务消费者(Feign)

访问 /feignObjParam接口

SpringCloud 服务消费者(Feign)

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/101170.html原文链接:https://javaforall.cn

原创文章,作者:夜风博客,如若转载,请注明出处:https://www.homedt.net/155934.html