PcLGXNjMTdiFVKTrElCl__DSC2245

Dalaoyang

Welcome to Us !

声明式调用---Feign


Feign:Feign是一种声明式、模板化的HTTP客户端。

用我的理解来说,Feign的功能类似dubbo暴露服务,但是与dubbo稍有不同的是Feign是HTTP REST接口的形式暴露的。

这一篇还是要利用到上一篇中的service(8762),service(8763)两个服务,先启动这两个服务。

新建项目,service-feign(8765),pom加入feign依赖,完整pom代码如下:

<?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>

    <groupId>com.dalaoyang</groupId>
    <artifactId>springcloud_feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springcloud_feign</name>
    <description>springcloud_feign</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR1</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</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>

在启动类加入@EnableFeignClients注解,如果没有加入basePackages制定扫描包得话,默认会去找项目会扫描到的所有@FeignClient。启动类代码如下:

package com.dalaoyang;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


@SpringBootApplication
@EnableEurekaClient
//@EnableFeignClients(basePackages = "com.dalaoyang.interfaces")
@EnableFeignClients
public class SpringcloudFeignApplication {

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

创建Feign暴露接口,接口上加入@FeignClient(value=“service”,fallback = FeignFallbackService.class)
注解。
service为要远程调用服务的名字,即你要调用服务的spring.application.name
fallback为远程调用失败后回调的方法。代码如下:

package com.dalaoyang.interfaces;

import com.dalaoyang.back.FeignFallbackService;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author dalaoyang
 * @Description
 * @project springcloud_learn
 * @package com.dalaoyang.interfaces
 * @email yangyang@dalaoyang.cn
 * @date 2018/4/20
 */
@FeignClient(value="service",fallback = FeignFallbackService.class)
//这里是要远程调用的服务的名称,即你要调用服务的spring.application.name
//fallback是远程调用失败回调的方法
public interface FeignInterface {
    @GetMapping("/")
    String IndexInfo();
}

远程调用失败回调类FeignFallbackService,要实现对应的FeignClient实现对应的方法:

package com.dalaoyang.back;

import com.dalaoyang.interfaces.FeignInterface;
import org.springframework.stereotype.Service;

/**
 * @author dalaoyang
 * @Description
 * @project springcloud_learn
 * @package com.dalaoyang.back
 * @email yangyang@dalaoyang.cn
 * @date 2018/4/20
 */
@Service
public class FeignFallbackService implements FeignInterface {
    @Override
    public String IndexInfo(){
        return "远程调用失败!";
    };
}

最后看一下配置文件,需要注意的是feign.hystrix.enabled,如果配置远程调用回调的话需要声明一下Feign的 hystrix支持,不然页面还是会显示错误。

##端口号
server.port=8765

##服务名称
spring.application.name=service_feign

##注册中心地址
eureka.client.service-url.defaultZone=http://eureka.dalaoyang.cn/eureka/


##声明Feign的 hystrix支持
feign.hystrix.enabled=true

启动service-feign(8765),先去看一眼http://eureka.dalaoyang.cn

image

可以看到服务注册成功,然后访问http://localhost:8765/,可以看到页面和使用ribbon的时候一样交替显示。

image

image

控制台

image

然后我们关闭service(8762),service(8763)两个服务,再次访问,可以看到

image

源码下载 :大老杨码云

个人网站:https://dalaoyang.cn





标签: SPRINGCLOUD