上一章节,讲解了SpringCloud怎样经由过程RestTemplate+Ribbon去负载平衡消耗效劳,本章重要报告怎样经由过程Feign去消耗效劳。

一、Feign 简介:

Feign是一个方便的rest框架,在Ribbon的基础上举行了一次革新,接纳接口的体式格局,将须要挪用的其他效劳的要领界说成笼统要领,不须要自身构建http要求,简化了挪用。然则末了的道理照样经由过程ribbon在注册效劳器中找到效劳实例,然后对要求举行分派。

在工作中,我们基本上都是运用Feign来举行效劳挪用,由于Feign运用起来就像是挪用自身当地的要领一样,而觉得不到是挪用长途要领,相称惬意,它重要有3个长处。

  • 1. feign自身内里就包罗有了ribbon,具有负载平衡的才能
  • 2. fegin是一个接纳基于接口的注解的编程体式格局,越发轻便
  • 3. fegin整合了Hystrix,具有熔断的才能

二、 准备工作:

1. 启动eureka-server 工程,eureka注册中间就启动了。

2. 启动springcloud-eureka-client工程,springcloud-eureka-client工程的端口为9300。

3. 将springcloud-eureka-client工程的application.properties文件中端口改成9400,然后再启动springcloud-eureka-client。

经由过程上面步调,我们就启动了端口9300,9400两个一样的springcloud-eureka-client效劳模块(为了测试feign的负载平衡才能)。

三、新建一个feign效劳:

1. 新建一个spring-boot工程,取名为springcloud-feign-client,修正pox文件以下:

<parent>标签就是引入我们第一章节新建的父工程的pom.xml文件,详细可参考:一起来学Spring Cloud | 第一章 :怎样搭建一个多模块的springcloud项目

<?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.haly</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.haly</groupId> <artifactId>springcloud-feign-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springcloud-ribbon-client</name> <description>新建一个springcloud项目</description> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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. 修正application.properties文件以下

server.port=9600
spring.application.name=springcloud-feign-client
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

3. 模块启动类须要增添注解@EnableFeignClients,透露表现开启Feign的功用

package com.haly;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class SpringcloudFeignClientApplication {

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

}

4. 界说一个feign接口,经由过程@FeignClient(“效劳名”),来指定挪用哪一个效劳。本章案例中挪用了springcloud-eureka-client效劳的“/hello”接口

springcloud-eureka-client模块中的hello接口,详细完成能够参考:一起来学Spring Cloud | 第二章:效劳注册和发明组件 (Eureka)

ps:笼统要领的注解、要领名、参数要和效劳提供方保持一致(这里是与springcloud-eureka-client模块中的 /hello要领保持一致)

package com.haly.romote;

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;

@FeignClient(value = "springcloud-eureka-client")
public interface FeignRemoteService {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(@RequestParam(value = "name") String name);
    
}

5. controller层,对外袒露一个"/getHello"的API接口,给页面测试,经由过程上面界说的Feign客户端FeignRemoteService来消耗效劳。代码以下:

package com.haly.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.haly.romote.FeignRemoteService;


@RestController
public class FeignController {
	
    @Autowired
    FeignRemoteService feignRemoteService;

    @GetMapping(value = "/getHello")
    public String getHello(@RequestParam String name) {
        return feignRemoteService.hello(name);
    }

}

 6. 启动各个效劳模块,效劳注册效果以下

 接见地点 http://localhost:9600/getHello?name=young码农  , 屡次轮番接见页面,涌现9300,9400效劳接口返回效果,证实feign是整合了负载平衡功用

四、总结:

加上本章节代码后,代码目次构造:

Last modification:March 25, 2020
如果觉得我的文章对你有用,请随意赞赏