SpringCloud 服務消費者(Feign)

Feign簡介

Feign 是一個聲明web服務客戶端,這便得編寫web服務客戶端更容易,使用Feign 建立一個接口並對它進行註解,它具備可插拔的註解支持包括Feign註解與JAX-RS註解,Feign還支持可插拔的編碼器與解碼器,Spring Cloud 增長了對 Spring MVC的註解。在Spring Cloud中使用Feign, 咱們能夠作到使用HTTP請求遠程服務時能與調用本地方法同樣的編碼體驗。Feign 採用的是基於接口的註解,Feign 整合了ribbon,具備負載均衡的能力。java

準備工做

啓動註冊中心eureka-server,服務提供者say-hello。對這兩個項目各自啓動兩個實例。web

建立Feign客戶端

1.新建一個springboot工程,取名爲service-feignspring

<?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.修改配置文件apache

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(「服務名」),來指定調用哪一個服務。springboot

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

5.建立controller,對外提供接口app

@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中添加兩個方法maven

/**
     *  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接口spring-boot

/**
     * 多參數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接口
圖片描述
訪問 /feignObjParam接口
圖片描述

相關文章
相關標籤/搜索