openfeign默认集成了ribbon,做下简单配置即可实现负载均衡(springboot和springcloud的强大的便利性!!!)
如果没有私服无法下载feign-service-api-test-0.0.1-SNAPSHOT.jar 点我下载
ribbon配置
我们需要三个实例,两个spring-cloud-provider(服务提供者)实例和一个spring-cloud-consumer(服务消费者)实例
服务providerDemo1实例目录如下图
TestImpl.java,提供服务的类
import com.demo.api.test.TestApi;
import com.demo.model.UserTest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class TestImpl implements TestApi {
@Override
public List<UserTest> getBigCat(@RequestBody UserTest userTest) {
System.out.println("provider1被调用");
System.out.println(userTest.getUserName());
userTest.setUserName("111111");
List<UserTest> userTests = new ArrayList<>();
userTests.add(userTest);
return userTests;
}
}
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.demo</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consumer</name>
<description>服务消费者</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath />
</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>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- springCloud注测中心服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--w微服务jar包-->
<dependency>
<groupId>com.demo</groupId>
<artifactId>feign-service-api-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
</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>
properties文件
spring.application.name=spring-cloud-consumer
server.port=9003
eureka.client.serviceUrl.defaultZone=http://192.168.1.192:1111/eureka/
logging.config=classpath:logback-spring.xml
# 配置请求GZIP压缩
feign.compression.request.enabled=true
# 配置响应GZIP压缩
feign.compression.response.enabled=true
# 配置压缩支持的MIME TYPE
feign.compression.request.mime-types=text/xml,application/xml,application/json
# 配置压缩数据大小的下限
feign.compression.request.min-request-size=2048
spring-cloud-provider.ribbon.NFLoadBalancerRuleClassName = com.netflix.loadbalancer.RandomRule
启动类
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
}
}
直接启动就好,我们把providerDemo1复制一份,命名为providerDemo2:
只需要改一下端口号,再把TestImpl.java改为如下:
package com.demo.feignImpl;
import com.demo.api.test.TestApi;
import com.demo.model.UserTest;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
public class TestImpl implements TestApi {
@Override
public List<UserTest> getBigCat(@RequestBody UserTest userTest) {
System.out.println(userTest.getUserName());
userTest.setUserName("2222222");
List<UserTest> userTests = new ArrayList<>();
userTests.add(userTest);
return userTests;
}
}
此处的改动只是为了我们方便查看负载均衡的效果,(调用了provide1,返回“111111”,调用provider2返回“222222”)生产上和集群服务中同一个实例一般都是同样的代码。
至此我们模拟了一个有两个实例的服务提供者,接下来我们搞一个服务消费者,工程名为consumerDemo
项目目录如下
controller方便我们进行接口调用的测试,TestService是我们使用feign调用的远程接口
TestService.java,继承了自定义jar包中的TestApi接口,通过如下代码我们可以看到调用的是spring-cloud-provider的服务,我们通过上面的步骤已经启动了两个服务名为spring-cloud-provider的服务实例,注意!!!!!@FeignClien中要使用name属性,value属性适用于服务只有一个实例的情况下。
package com.demo.eurekaFeignService;
import com.demo.api.test.TestApi;
import org.springframework.cloud.openfeign.FeignClient;
@FeignClient(name = "spring-cloud-provider",path = "/")
public interface TestService extends TestApi {
}
通过这步简单的操作已经可以进行远程调用了,并且服务消费者可以像使用本地服务一样注入使用即可,
TestController.java如下所示
为了便于查看负载均衡的效果,我们在控制台打印一下提供者返回的数据。
package com.demo.controller;
import com.demo.eurekaFeignService.TestService;
import com.demo.model.UserTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value = "api/test",method = RequestMethod.GET)
public List<UserTest> test(){
UserTest userTest = new UserTest();
userTest.setUserName("111111");
List<UserTest> bigCat = testService.getBigCat(userTest);
System.out.println(bigCat.get(0).getUserName());
return bigCat;
}
}
启动类
添加了@EnableFeignClients,表示开启feign调用远程接口。
package com.demo;
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
@EnableDiscoveryClient
@EnableFeignClients
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
properties文件
开启了压缩功能,占用一定的cpu资源,降低网络传输的压力,提高接口性能。
spring.application.name=spring-cloud-consumer
server.port=9003
eureka.client.serviceUrl.defaultZone=http://192.168.1.192:1111/eureka/
logging.config=classpath:logback-spring.xml
# 配置请求GZIP压缩
feign.compression.request.enabled=true
# 配置响应GZIP压缩
feign.compression.response.enabled=true
# 配置压缩支持的MIME TYPE
feign.compression.request.mime-types=text/xml,application/xml,application/json
# 配置压缩数据大小的下限
feign.compression.request.min-request-size=2048
启动服务消费者工程,在浏览器输入“http://localhost:9003/api/test”
多刷新几次,消费者控制台打印如下:
可以看到交替调用了两个实例,这是ribbon默认的负载均衡策略----轮询 (RoundRobin),比较常用的负载均衡策略有:
随机 (Random)
轮询 (RoundRobin)
一致性哈希 (ConsistentHash)
哈希 (Hash)
加权(Weighted)
我们可以在配置文件中配置某个服务的负载均衡策略,例如:
spring-cloud-provider.ribbon.NFLoadBalancerRuleClassName = com.netflix.loadbalancer.RandomRule
我们在properties文件中添加这段代码后就给“spring-cloud-provider”服务实现实现了随机的负载均衡策略。
重启consumer,多次访问“http://localhost:9003/api/test”后,控制台可以看到,两个服务实例都是随机调用的,所以我们已经实现了负载均衡的随机策略。
本篇文章的工程已上传,点我下载 除了ribbon内置的负载均衡策略,我们也可以自定义自己的策略,留作下篇博客和大家分享。