Spring Cloud 进阶--Rest微服务加入Feign负载均衡客户端组件(通过接口方式调用Rest服务)

版权声明:本文为博主原创文章,如果觉得写的不错需要转载,在转载时请注明博文出处! https://blog.csdn.net/Hello_World_QWP/article/details/87918412

                            《 Rest微服务加入Feign负载均衡客户端组件 》

前言

在上一篇文章中,主要完成了 《  Rest微服务加入Ribbon负载均衡客户端组件实现负载均衡 》,并且完成了 《 Feign 基本理论概述 》的详细简介,本篇博客将带领读者完整在 Rest 微服务中集成 Feign 负载均衡客户端组件,并通接口嗲用的方式实现服务的调用,本篇博客主要主要涉及模块,包括:

  • 修改整体微服务项目的公共子模块,供其它子模块引用,达到通用的目的,项目名为 “ microservice-api ”;
  • 新增支持 Feign 的服务消费者模块,服务名称为 “ microservice-feign-80 ”;

Rest微服务加入Feign负载均衡客户端组件(通过接口方式调用Rest服务)

1、修改整体微服务项目的公共子模块 “ microservice-api ”,

POM 修改,新增对 Feign 的支持,完整内容如下:

<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.huazai.springcloud</groupId>
		<artifactId>microservice</artifactId>
		<version>${project.version}</version>
	</parent>

	<!-- 当前Module的名字 -->
	<artifactId>microservice-api</artifactId>

	<dependencies>
		<!-- 当前Module需要用到的jar包,按自己需求添加,如果父类已经包含了,可以不用写版本号 -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
		<!-- 实现对 Feign 的支持 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
	</dependencies>



</project>

新增接口 “ DepartmentClientService.java ” ,并条件注解 “ @FeignClient ” 标识为 Feign 客户端,内容如下:

package com.huazai.springcloud.service;

import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.huazai.springcloud.entity.Department;
import com.huazai.springcloud.factory.DepartmentClientServiceFallbackFactory;

/**
 * 
 * @author HuaZai
 * @contact [email protected]
 *          <ul>
 * @description TODO
 *              </ul>
 * @className DepartmentClientService
 * @package com.huazai.springcloud.service
 * @createdTime 2018年05月13日 下午8:21:01
 *
 * @version V1.0.0
 */
@FeignClient(value = "MICROSERVICE-PROVIDER/department")
public interface DepartmentClientService
{

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public boolean add(@RequestBody Department department);

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	public void delete(@PathVariable(value = "id") Long id);

	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	public void update(@RequestBody Department department);

	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public Department get(@PathVariable(value = "id") Long id);

	@RequestMapping(value = "/list")
	public List<Department> list();
}

重新安装公共模块 “ microservice-api ” ,以供其它子模块引用,达到通用的目的,点击项目右键 -> Run As -> Maven clean/Maven install。

 

2、集成 Feign 负载均衡客户端组件

参考服务消费者 “ microservice-consumer-80 ” 模块,新建 “ microservice-feign-80 ” 服务,新建完成后,如下图:

POM 内容修改,新增对 Feign 的支持,完整内容如下:

<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.huazai.springcloud</groupId>
		<artifactId>microservice</artifactId>
		<version>${project.version}</version>
	</parent>

	<artifactId>microservice-feign-80</artifactId>
	<description>服务消费者模块-feign-80</description>

	<!-- 项目依赖包 -->
	<dependencies>
		<!-- 引入自己定义的 api -->
		<dependency>
			<groupId>com.huazai.springcloud</groupId>
			<artifactId>microservice-api</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 添加对 feign 的支持 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<!-- 对 Eureka 的支持 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<!-- hystrix 相关 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>
		<!-- 修改后立即生效,热部署 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
	</dependencies>


</project>

修改 “ DepartmentController.java ” 类,调用上面新建的 “ DepartmentClientService.java ” 接口,完整内容如下:

package com.huazai.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.huazai.springcloud.entity.Department;
import com.huazai.springcloud.service.DepartmentClientService;

/**
 * 
 * <p>
 * 
 * @ClassName : DepartmentController
 *            </p>
 *            <p>
 * @Description : TODO
 *              </p>
 * 
 * @Author : HuaZai
 * @ContactInformation : [email protected]
 * @Date : 2018年05月23日 下午9:01:38
 * @Version : V1.0.0
 * 
 * @param
 */
@RestController
@RequestMapping("/department")
public class DepartmentController
{

	@Autowired
	private DepartmentClientService deptClientService;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public boolean add(@RequestBody Department department)
	{
		return this.deptClientService.add(department);
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
	public void delete(@PathVariable(value = "id") Long id)
	{
		this.deptClientService.delete(id);
	}

	@RequestMapping(value = "/update", method = RequestMethod.PUT)
	public void update(@RequestBody Department department)
	{
		this.deptClientService.update(department);
	}

	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public Department get(@PathVariable(value = "id") Long id)
	{
		return this.deptClientService.get(id);
	}

	@RequestMapping(value = "/list")
	public List<Department> list()
	{
		return this.deptClientService.list();
	}
}

修改主启动类 “ MicroServiceFeignApp.java ” ,并新增注解 “ @EnableFeignClients ” ,开启对 Feign 客户端的支持,完整内容如下:

package com.huazai.springcloud;

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;
import org.springframework.context.annotation.ComponentScan;

/**
 * 
 * @author HuaZai
 * @contact [email protected]
 *          <ul>
 * @description
 *              <li>Feign 负载均衡器
 *              </ul>
 * @className MicroServiceFeignApp
 * @package com.huazai.springcloud
 * @createdTime 2018年05月26日 下午4:44:03
 *
 * @version V1.0.0
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages =
{ "com.huazai.springcloud" })
@ComponentScan("com.huazai.springcloud")
public class MicroServiceFeignApp
{

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

*注:该 “ @EnableFeignClients ” 注解与接口中的 “ @FeignClient ” 是对应使用的(必须的!!!)

3、测试,先启动 Eureka 集群,再启动 三台服务提供者模块,最后启动集成了 Feign 的服务消费在模块,启动成功后,注意观察服务注册列表,如下图:

访问集成了 Feign 的服务器消费者地址,并反复刷新,注意观察数据的变化,如下图:

关于 Feign :

上面完成了 Rest 微服务对 Feign 的整合,可以清晰的看到 Feign 主要通过接口的方式来调用 Rest 服务(之前是 Ribbon + Spring 的 RestTemplate 实现的),将该请求发送到 Eureka 服务器(http://MICROSERVICE-PROVIDER/department/list),通过 Feign 直接找到服务需要调用的那个接口,由于在进行服务调用的时候结合了 Ribbon 技术,所以也支持服务的负载均衡,而且和 Ribbon 类似是默认的采用了轮询的负载均衡机制。

GitLab 源码地址:

项目源码地址(zip格式的工程包):


好了,关于 Rest微服务加入Feign负载均衡客户端组件(通过接口方式调用Rest服务) 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。 
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。


作       者: 华    仔
联系作者: [email protected]
来        源: CSDN (Chinese Software Developer Network)
原        文: https://blog.csdn.net/Hello_World_QWP/article/details/87918412
版权声明: 本文为博主原创文章,请在转载时务必注明博文出处!

猜你喜欢

转载自blog.csdn.net/Hello_World_QWP/article/details/87918412