内容简介:客户端负载均衡和服务端负载均衡最大的不同点在于上面所提到服务清单所存储的位置。在客户端负载均衡中,所有客户端节点都维护着自己要访问的服务端清单,而这些服务端端清单来自于服务注册中心通过Spring Cloud Ribbon的封装,我们在微服务架构中使用客户端负载均衡调用非常简单,只需要如下两步:这样,我们就可以将服务提供者的高可用以及服务消费者的负载均衡调用一起实现了。
- Ribbon是Netflix开源的一个实现了客户端负载均衡的组件;
- Ribbon客户端组件提供了一系列的完善的配置项,例如:连接超时、重试、各种负载均衡策略,并且支持自定义负载均衡算法;
- 它不像其他微服务组件如配置中心、注册中心、API网关一样需要独立部署,但是它几乎在每一个微服务当中都会用到;
二、负载均衡图例
客户端负载均衡和服务端负载均衡最大的不同点在于上面所提到服务清单所存储的位置。在客户端负载均衡中,所有客户端节点都维护着自己要访问的服务端清单,而这些服务端端清单来自于服务注册中心
服务端负载均衡
客户端负载均衡
Eureka集成Ribbon的架构图
通过Spring Cloud Ribbon的封装,我们在微服务架构中使用客户端负载均衡调用非常简单,只需要如下两步:
- ️服务提供者只需要启动多个服务实例并注册到一个注册中心或是多个相关联的服务注册中。
- 服务消费者直接通过调用被@LoadBalanced注解修饰过的RestTemplate来实现面向服务的接口调用。
这样,我们就可以将服务提供者的高可用以及服务消费者的负载均衡调用一起实现了。
三、下面我们来看代码吧~
首先拿到上两张我们的代码:
- juejin.im/post/5ceb93… Eureka服务注册和发现
- juejin.im/post/5ceb9b… Eureka高可用配置
在cloud-demo-parent上创建maven模块 cloud-demo-ribbon,同样项目结构修改成我们习惯的模样~~~
项目结构是这样的
pom文件时这样的,这里之所以不引入ribbon的包是因为spring-cloud-starter-netflix-eureka-client包含了ribbon
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.hewl</groupId> <artifactId>cloud-demo-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>cloud-demo-ribbon</artifactId> <name>cloud-demo-ribbon</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project> 复制代码
application.yml文件是这样的,注意端口与其他项目分开
server:
port: 8810 # 你的端口
spring:
application:
name: ribbon
eureka:
client:
service-url:
defaultZone: http://admin:admin@server1:8761/eureka/,http://admin:admin@server2:8762/eureka/
复制代码
修改RibbonApplication类
package com.hewl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced //注解表明这个restRemplate开启负载均衡的功能
public RestTemplate restTemplate () {
return new RestTemplate();
};
}
复制代码
新增service类,调用之前写的cloud-demo-eureka-client服务
package com.hewl.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class TestService {
@Autowired
public RestTemplate restTemplate;
public static final String SERVICE_ADDRESS = "http://SERVICE-EUREKA-CLIENT/";
public String testService(String name) {
return restTemplate.getForObject(SERVICE_ADDRESS + "test?name=" + name, String.class);
}
}
复制代码
新增controller类,ribbon访问接口
package com.hewl.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.hewl.service.TestService;
@RestController
public class TestController {
@Autowired
TestService testService;
@RequestMapping(value = "/test") // 随便起个自己喜欢的访问名字
public String test(@RequestParam("name") String name) {
return testService.testService(name);
}
}
复制代码
启动项目:
- 先启动erueka server(这里需要注意的是,我们如果启动的是高可用的eureka要启动8761和8762两个服务,不然会一直报错)
- 再启动erueka client
- 修改erueka client端口,再启动
- 启动本次创建的ribbon项目
- 访问http://localhost:8762
这里可以看到服务已经启动成功,我们访问下http://localhost:8810/test?name=张三
这里可以看到负载均衡已经成功了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 如何搭建Nginx服务器做到负载均衡?
- Linux环境搭建Nginx+Tomcat负载均衡集群
- 阿里云支持使用 Keepalived 搭建负载均衡软件吗?
- 搭建Keepalived + Nginx + Tomcat的高可用负载均衡架构
- 如何搭建应对亿级流量的高可用负载均衡?
- 网易蜂巢使用云主机并搭建docker环境配置负载均衡
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Artificial Intelligence
Stuart Russell、Peter Norvig / Pearson / 2009-12-11 / USD 195.00
The long-anticipated revision of this #1 selling book offers the most comprehensive, state of the art introduction to the theory and practice of artificial intelligence for modern applications. Intell......一起来看看 《Artificial Intelligence》 这本书的介绍吧!