基于spring-cloud-feign的服务间调用,类比

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Little_Matches/article/details/84497211

说到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

猜你喜欢

转载自blog.csdn.net/Little_Matches/article/details/84497211