负载均衡之Feign----服务消费者

本文介绍了如何在Spring Boot应用中利用Feign作为服务消费者实现负载均衡。首先确保已有服务注册中心和两个调用方,然后创建server-feign工程,配置相关属性和服务名,添加Feign依赖并重新加载。通过启用@EnableFeignClients和@EnableDiscoveryClient注解开启Feign功能,并编写测试类。当项目启动并访问测试接口时,浏览器中显示调用方client-01和client-02交替响应,表明Feign负载均衡配置成功。
摘要由CSDN通过智能技术生成

 

 

1.Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。

  使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。

  Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果

2.简单理解:

  ·Feign 采用的是基于接口的注解

  ·Feign 整合了ribbon

在学习前请确认已创建好一个服务注册中心和两个调用方:

服务注册中心创建:https://blog.csdn.net/quguoxun/article/details/111030810

调用方创建:https://blog.csdn.net/quguoxun/article/details/111034298

复制创建好的调用方client-01创建client-02并修改application.properties中端口为9092并启动

spring.application.name不修改,仍为client-01: 

由于服务之间是根据此名称进行相互调用,所以此时表9091,9092对外提供一个服务,

服务名为client-01相当于一个小的集群

创建server-feign工程

在application.properties中添加相关配置,端口号为9094,服务名为server-feign

#Eureka服务注册中心的访问地址
eureka.client.serviceUrl.defaultZone=http://localhost:9090/eureka/
#client服务的端口
server.port=9094
#clinet服务的服务名称
spring.application.name=server-feign

在pom.xml文件中添加feign依赖,这里最好添加两个依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.joyin</groupId>
	<artifactId>server-feign</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>server-feign</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>2020.0.0-M6</spring-cloud.version>
	</properties>

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

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

		<!--高版本feign依赖============================-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</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>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
	</repositories>

</project>

手动添加时需要进行加载,否则会报错,在pom.xml文件中,右击鼠标-->Maven-->Reimport

启动类中加入@EnableFeignClients注解开启Feign的功能(@EnableDiscoveryClient和@EnableFeignClients)

package com.joyin.serverfeign;

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

@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class ServerFeignApplication {

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

}

编写测试类service、controller和serverimp,feign基于接口——创建FeignTest

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignController {

    @Autowired
    private FeignService feignservice;
    @RequestMapping(value = "FeignTest")
    public String FeignTest(@RequestParam String message){
        
        return feignservice.FeignTest(message);
    }
}
package com.joyin.serverfeign.Service;


import com.joyin.serverfeign.ServiceImp.FeignServiceImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Service
@FeignClient(value = "client-01", fallback = FeignServiceImpl.class)
public interface FeignService {
    @RequestMapping("/ClientTest")  //此处名称为client-01服务中对应的方法名
    String FeignTest(@RequestParam(value = "message") String message);
}
package com.joyin.serverfeign.ServiceImp;

import com.joyin.serverfeign.Service.FeignService;


public class FeignServiceImpl implements FeignService{
    @Override
    public String FeignTest(String message) {
        return "Feign 负载均衡测试"+message;
    }
}

配置完成后启动项目,保证服务和端口都启动成功:

在浏览器中刷新地址:http://localhost:9094/FeignTest?message="Feign”,浏览器交替显示:“"Feign"端口号:9092”,“"Feign"端口号:9091”;

 

出现上图效果说明Feign负载均衡配置完成,需要的小伙伴不妨动动小手进行自主操作!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值