1. 程式人生 > >基於spring-cloud-feign的服務間呼叫,類比

基於spring-cloud-feign的服務間呼叫,類比

說到spring-cloud 服務呼叫不的不提dubbo. spring-cloud的服務呼叫是基於http呼叫,dubbo是基於RPC。使用feign就能實現微服務間的方法呼叫。

spring-cloud的服務呼叫一般有兩種,第一種是ribbon+restTemplate,第二種是feign
由於feign預設集成了ribbon,這裡集中介紹feign
基於上一篇文章的例子eureka-client改造
首先新建一個模組feign-client,在pom里加入如下依賴:

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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-feign</artifactId
>
<version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <file.encoding>UTF8</file.encoding
>
<spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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> </project>

增加配置如下
application.properties

server.port=8884
spring.application.name=feignService

eureka.client.service-url.defaultZone=http://localhost:8881/eureka/

Application.java配置如下:

package top.littlematch.feign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
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;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@RestController
public class FeignApplication {
    public static void main(String[] args){
        SpringApplication.run(FeignApplication.class, args);
    }
    //呼叫的服務名,以介面的方式定義
    @FeignClient("baseService")
    public interface BaseService {
        //呼叫的方法所暴露的url,其實就相當於頁面呼叫方法一樣
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String hello(@RequestParam("name") String name);
    }

    /**
     * 注入該方法,idea這裡可能會報紅
     *  這裡就如dubbo的方法呼叫,只是這裡需重新定義個介面接收方法
     *  基於http會多這一步,但也因為基於http其擴充套件性很強,能輕易實現與其他語言基於http服務呼叫
     */

    @Autowired
    private BaseService baseService;


    @RequestMapping("/hello")
    public String hello(@RequestParam("name")String name){
        //呼叫方法
        return baseService.hello(name);
    }
}

依次啟動eureka-server,eureka-client,feign-client,再呼叫 http://localhost:8884/hello?name=match

瀏覽器返回:

hello! match

gitee程式碼地址:https://gitee.com/leftbehindmatches/spring-cloud-examples/tree/master/eureka-feign