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

说到spring-cloud 服务调用不的不提dubbo. spring-cloud的服务调用是基于http调用,dubbo是基于RPC调用。使用feign就能实现微服务间的方法调用。其实本质上相当于前端调用http接口,只不过这里用java代码实现,但也因此, 使用feign就可以与其他语言进行http通信了, 就是需要多一步写接口的流程。

spring-cloud的服务调用一般有两种,第一种是ribbon+restTemplate,第二种是feign
由于feign默认集成了ribbon,这里集中介绍feign
基于上一篇文章的例子eureka-client改造
首先新建一个模块feign-client,在pom里加入如下依赖:

pom.xml:


<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.0modelVersion>
    <artifactId>eureka-feignartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>jarpackaging>
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <java.version>1.8java.version>
        <file.encoding>UTF8file.encoding>
        <spring-cloud.version>Finchley.SR2spring-cloud.version>
    properties>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.2.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            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

你可能感兴趣的:(Java,eureka,springcloud,feigin,spring-cloud入门)