Spring cloud ribbon 使用eureka注册中心 客户端负载均衡(源码下载)
1、传统的服务器端负载均衡
服务器端负载平衡涉及单体应用程序,其中我们在负载负载平衡器后面的应用程序实例数量有限。 我们将我们的 war/ear 文件部署到多个服务器实例中,这些实例基本上是一个部署了相同应用程序的服务器池,我们在它前面放置了一个负载均衡器。负载均衡器具有公共 IP 和 DNS。 客户端使用该公共 IP/DNS 发出请求。 负载均衡器决定将请求转发到哪个内部应用程序服务器。 它主要使用循环或粘性会话算法。 我们称之为服务器端负载平衡。
1.1. 微服务架构中的问题
大多数服务器端负载平衡是手动工作,我们需要手动向负载平衡器添加/删除实例才能工作。 因此,理想情况下,我们正在失去当今的按需可扩展性,无法在任何新实例被剥离时自动发现和配置。另一个问题是要有一个故障转移策略来为客户提供无缝体验。 最后,我们需要一个单独的服务器来托管负载均衡器实例,这会影响成本和维护。
2. 客户端负载均衡
为了克服传统负载均衡的问题,客户端负载均衡应运而生。 它们作为内置组件驻留在应用程序中并与应用程序捆绑在一起,因此我们不必将它们部署在单独的服务器中。现在让我们想象一下大图。 在微服务架构中,我们将不得不开发许多微服务,每个微服务在生态系统中可能有多个实例。 为了克服这种复杂性,我们已经有了一个使用服务发现模式的流行解决方案。 在 Spring Boot 应用程序中,我们在服务发现领域有几个选项,例如 eureka、consoul、zookeeper 等。
现在,如果一个微服务想要与另一个微服务通信,它通常会使用发现客户端查找服务注册中心,并且 Eureka 服务器将该目标微服务的所有实例返回给调用者服务。 然后由调用方服务负责选择发送请求的实例。
在这里,客户端负载平衡出现并自动处理围绕这种情况的复杂性,并以负载平衡的方式委托给适当的实例。 请注意,我们可以指定要使用的负载平衡算法。
3. Netflix ribbon – 客户端负载均衡器
Spring Cloud 系列的 Netflix ribbon提供了这样的工具来设置客户端负载平衡以及服务注册表组件。 Spring Boot 具有非常好的配置ribbon客户端负载平衡器的方法,并且只需最少的努力。 它提供以下功能- 负载均衡
- 容错
- 异步和反应模型中的多协议(HTTP、TCP、UDP)支持
- 缓存和批处理
<dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon</artifactId>
<version>2.2.2</version>
</dependency>
4. Netflix 功能演示
4.1. 技术栈
- Java、Eclipse、Maven 作为开发环境
- Spring-boot 和 Cloud 作为应用程序框架
- Eureka 作为服务注册服务器
- ribbon作为客户端负载均衡器
- 两个使用 Spring Boot 的微服务。
- 一个需要根据业务需求调用另一个 Eureka 服务注册服务器
- 调用微服务中的功能区以使用服务发现以负载均衡的方式调用其他服务
- 在没有服务发现的情况下以负载平衡的方式调用服务
4.2. 创建后端微服务
我们将使用 Spring boot 创建一个简单的微服务,并将公开一个简单的 REST 端点。 创建一个名为ribbon-server 的简单spring boot 项目,带有spring-boot-web 和服务发现客户端依赖项,用于在Web 服务器中托管它,并公开一个Rest Controller 以进行测试。为此,我们需要转到 https://start.spring.io/ 并提供 maven 坐标并选择依赖项。 下载包含骨架项目的 zip 文件。 然后我们需要在 eclipse 中将其解压缩到合适的文件夹中后导入。

4.2.1. 创建休息端点
编写一个 Rest Controller 并公开一个 Rest Endpoint,如下所示。import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyRestController {
@Autowired
Environment environment;
@GetMapping("/")
public String health() {
return "I am Ok";
}
@GetMapping("/backend")
public String backend() {
System.out.println("Inside MyRestController::backend...");
String serverPort = environment.getProperty("local.server.port");
System.out.println("Port : " + serverPort);
return "Hello form Backend!!! " + " Host : localhost " + " :: Port : " + serverPort;
}
}
4.2.2 启用发现客户端
将此服务注册到 eureka 要做到这一点,我们需要在应用程序类中添加 @EnableDiscoveryClient。 我们还需要在应用程序属性文件中添加以下条目。import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonServerApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonServerApplication.class, args);
}
}
application.properties
spring.application.name=server
server.port = 9090
eureka.client.serviceUrl.defaultZone= http://${registry.host:localhost}:${registry.port:8761}/eureka/
eureka.client.healthcheck.enabled= true
eureka.instance.leaseRenewalIntervalInSeconds= 1
eureka.instance.leaseExpirationDurationInSeconds= 2
4.3. Eureka 服务注册服务器
创建服务发现服务器。 这也很容易。 只需要像上面一样创建一个以 Eureka Server 为依赖项的 spring boot 项目,并进行以下配置。4.3.1. 尤里卡服务器配置
一旦 spring boot 服务准备好并在 eclipse 中导入,在 spring boot 应用程序类中添加 @EnableEurekaServer 注释,并在应用程序属性文件中添加以下配置。import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class RibbonEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonEurekaServerApplication.class, args);
}
}
$title(application.properties)
spring.application.name= ${springboot.app.name:eureka-serviceregistry}
server.port = ${server-port:8761}
eureka.instance.hostname= ${springboot.app.name:eureka-serviceregistry}
eureka.client.registerWithEureka= false
eureka.client.fetchRegistry= false
eureka.client.serviceUrl.defaultZone: http://${registry.host:localhost}:${server.port}/eureka/
4.4. 创建另一个微服务
按照上一节创建另一个名为添加依赖项的服务。 下载后,在eclipse中导入项目并做如下配置ribbon-clientspring-cloud-starter4.4.1. 色带配置
在应用类中,添加两个注解@RibbonClient 和@EnableDiscoveryClient 为服务注册启用ribbon和Eureka客户端。import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
@EnableDiscoveryClient
@SpringBootApplication
@RibbonClient(name = "server", configuration = RibbonConfiguration.class)
public class RibbonClientApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonClientApplication.class, args);
}
}
在 中,我们需要进行以下配置。 这里 server.ribbon.listOfServers 被禁用,我们可以启用它来手动将服务器添加到此负载均衡器。 我们将在测试部分检查这一点。 其他属性是不言自明的.application.properties
spring.application.name=client
server.port=8888
eureka.client.serviceUrl.defaultZone= http://${registry.host:localhost}:${registry.port:8761}/eureka/
eureka.client.healthcheck.enabled= true
eureka.instance.leaseRenewalIntervalInSeconds= 1
eureka.instance.leaseExpirationDurationInSeconds= 2
server.ribbon.eureka.enabled=true
#server.ribbon.listOfServers=localhost:9090,localhost:9091,localhost:9092
server.ribbon.ServerListRefreshInterval=1000
#logging.level.root=TRACE
现在我们需要为ribbon再创建一个配置类来提及负载均衡算法和健康检查。 我们现在将使用 Ribbon 提供的默认值,但在这个类中,我们可以很好地覆盖它们并添加我们的自定义逻辑。import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AvailabilityFilteringRule;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
public class RibbonConfiguration {
@Autowired
IClientConfig config;
@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
}
@Bean
public IRule ribbonRule(IClientConfig config) {
return new AvailabilityFilteringRule();
}
}
5. 测试应用
5.1. 启动组件
执行最终构建使用命令并检查构建是否成功。如果有任何错误,您需要修复它们才能继续。一旦我们成功构建了所有 maven 项目,我们将一一启动服务.mvn clean install先是 Eureka,然后是后端微服务,最后是前端微服务。
要启动每个微服务,我们将使用 command.'java -jar -Dserver.port=XXXX target/YYYYY.jar'
5.3.验证尤里卡服务器
现在转到浏览器并检查 eureka 服务器是否正在运行,所有微服务都注册了所需数量的实例。 http://localhost:8761/
5.4.检查客户端负载平衡是否正常工作
在前端微服务中,我们使用 RestTemplate 调用后端微服务。使用 @LoadBalanced 注释启用 Rest tempate 作为客户端负载均衡器。
现在转到浏览器并打开客户端微服务休息端点,并看到响应来自后端实例中的任何一个。 http://localhost:8888/client/frontend
为了理解这个后端服务器正在返回它正在运行的端口,我们也在客户端微服务响应中显示它。尝试刷新此 url 几次并注意后端服务器的端口不断变化,这意味着客户端负载平衡正在工作。现在尝试添加更多后端服务器实例,并检查它是否也在 eureka 服务器中注册并最终在ribbon 中考虑,因为一旦它将在 eureka 中注册,ribbon 也会自动向新实例发送请求。
5.5. 在没有服务发现的情况下使用硬代码后端进行测试
转到前端微服务文件并启用 this.application.propertiesserver.ribbon.listOfServers=localhost:9090,localhost:9091,localhost:9092
server.ribbon.eureka.enabled=false
现在测试客户端 url。 您只会收到已注册实例的响应。 现在,如果您在不同端口启动后端微服务的新实例,Ribbon 将不会向新实例发送请求,直到我们在 Ribbon 中手动注册该实例。如果您在测试时遇到困难,我建议您也从所有应用程序中删除所有与 eureka 相关的配置,并停止 eureka 服务器。 希望您在测试时也不会遇到任何困难。
6. 总结
所以我们已经看到在 Spring Boot 微服务开发中我们可以轻松地将 Ribbon 和 Eureka 一起使用。 所以下次如果你有这种需求,可以用这种方式。示例源码下载:(访问密码:9987)
spring-cloud-ribbon.zip
https://blog.xqlee.com/article/854.html
评论