在分布式 或 微服务架构中,服务的提供者往往有多个实例 会注册到服务注册中心中,服务消费者需要使用服务时,需要决定使用哪个服务提供者,这正是负载均衡的体现。
负载均衡也可以理解为:将用户的请求平摊分配到每个服务器上,尽可能的压榨每台服务器的带宽、性能。
负载均衡的方式有两种:
像以前的老架构,通过nginx对请求做负载均衡,然后下发到指定的tomcat。
client获取到所有的service实例,然后做负载均衡规则。例如:Ribbon、Feign
本文我们针对SpringCloud如何集成Ribbon实现负载均衡做一个讨论。
注:(版本信息)–
整体项目目录包括三个Module,分别为:eureka-server、ribbon-feign-sample、ribbon-feign-sample-consumer。

其中eureka-server作为服务注册中心、ribbon-feign-sample作为服务提供者、ribbon-feign-sample-consumer作为服务消费者。
Ribbon 与 RestTemplate 配合使用,实现了微服务之间的调用。
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<!--子模块-->
<modules>
<module>ribbon-feign-sample</module>
<module>ribbon-feign-sample-consumer</module>
<module>eureka-server</module>
</modules>
<artifactId>spring-cloud-center</artifactId>
<groupId>com.saint</groupId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-center</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--整合spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--整合spring cloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
关于Spring-cloud和SpringBoot的版本对应关系,参考博文:SpringBoot、SpringCloud、SpringCloudAlibaba的版本对应关系。
eureka-server整体代码结构目录如下:

其整体很简单、仅仅包含一个pom.xml文件、一个配置文件、一个启动类。
<?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">
<parent>
<artifactId>spring-cloud-center</artifactId>
<groupId>com.saint</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>eureka-server</description>
<dependencies>
<!--集成Eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
server:
port: 10010
spring:
application:
name: eureka-server
eureka:
client:
# 把自身注册到Eureka-server中
register-with-eureka: true
# 服务注册中心不需要去检索其他服务
fetch-registry: false
# 指定服务注册中心的位置
service-url:
defaultZone: http://localhost:10010/eureka
instance:
hostname: localhost
package com.saint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author Saint
*/
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
这里和普通的启动有一个区别:需要加上 @EnableEurekaServer 注解开启Eureka-Server。
启动成功后,控制台输出如下:

进入到eureka-server 的dashboard,可以看到eureka-server已经上线:

ribbon-feign-sample整体代码结构目录如下:

其中包含一个pom.xml文件、一个配置文件、一个启动类、一个Controller。
<?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">
<parent>
<artifactId>spring-cloud-center</artifactId>
<groupId>com.saint</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ribbon-feign-sample</artifactId>
<groupId>com.saint</groupId>
<version>0.0.1-SNAPSHOT</version>
<description>ribbon test service provider</description>
<dependencies>
<!--集成eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
server:
port: 8081
spring:
application:
name: greeting-service
eureka:
client:
# 将当前服务注册到服务注册中心
service-url:
defaultZone: http://localhost:10010/eureka
package com.saint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* Saint
*/
@SpringBootApplication
@EnableEurekaClient
public class RibbonFeignSampleApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonFeignSampleApplication.class, args);
}
}
这里和普通的启动有一个区别:需要加上 @EnableEurekaClient 注解开启eureka-client。
package com.saint.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Saint
*/
@RestController
@RequestMapping("/greeting")
public class GreetingController {
@GetMapping("/sayHello/{name}")
public String sayHello(@PathVariable("name") String name) {
System.out.println("接收到了请求调用 + " + name);
return "Hello, " + name;
}
}
服务启动成功后,控制台输出如下:

再看eureka-server dashboard中多了一个 GREETING-SERVICE 服务,并且其有一个实例 192.168.3.32:greeting-service:8081。

1> 修改RibbonFeignSampleApplication的配置:

2> 复制出一个RibbonFeignSampleApplication配置:

3> 修改第二启动类配置名为:RibbonFeignSampleApplication-8082,启动端口为8082:

4> 运行RibbonFeignSampleApplication-8082:

5> 启动之后,看eureka-server dashboard中GREETING-SERVICE 服务多了一个实例 192.168.3.32:greeting-service:8082:

ribbon-feign-sample-consumer整体代码结构目录如下:

其包含一个pom.xml文件、一个application配置文件、一个启动类、一个Http负载均衡配置类、一个Controller。
<?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">
<parent>
<artifactId>spring-cloud-center</artifactId>
<groupId>com.saint</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ribbon-feign-sample-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>ribbon test service provider</description>
<dependencies>
<!--集成ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!--集成eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
server:
port: 9090
spring:
application:
name: consumer-service
eureka:
client:
# 将当前服务注册到服务注册中心
service-url:
defaultZone: http://localhost:10010/eureka
注:服务端口为9090,后面我们进行接口调用的时候会用到。
package com.saint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* Saint
*/
@SpringBootApplication
@EnableEurekaClient
public class RibbonFeignSampleConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonFeignSampleConsumerApplication.class, args);
}
}
package com.saint.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* Http配置类
*
* @author Saint
*/
@Configuration
public class HttpConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
HttpConfig类的作用:对RestTemple的调用使用Ribbon做负载均衡,将RestTemplate注入到Spring IOC容器中。
package com.saint.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author Saint
*/
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/say/{name}")
public String getHello(@PathVariable("name") String name) {
String res = restTemplate.getForObject("http://GREETING-SERVICE/greeting/sayHello/" + name, String.class);
return res;
}
}
ConsumerController中通过RestTemplate调用http://GREETING-SERVICE/greeting/sayHello/{name}。
启动成功后,看eureka-server dashboard中多了一个 CONSUMER-SERVICE 服务,并且其有一个实例 192.168.3.32:consumer-service:9090。

上述步骤中,我们已经依次启动了eureka-server、ribbon-feign-sample-8081、ribbon-feign-sample-8082、ribbon-feign-sample-consumer;三个服务、四个实例。
此处我们针对服务消费者ribbon-feign-sample-consumer做四次接口调用,分别为:
- http://localhost:9090/say/saint
- http://localhost:9090/say/saint2
- http://localhost:9090/say/saint3
- http://localhost:9090/say/saint4

然后我们去看ribbon-feign-sample-8081、ribbon-feign-sample-8082的控制台输出:
1> ribbon-feign-sample-8081控制台输出:

2> ribbon-feign-sample-8082控制台输出:

3> 结果说明:
我们可以发现,四个请求,ribbon-feign-sample-8081和ribbon-feign-sample-8082各分担了两个请求。其中ribbon-feign-sample-8081服务实例承担了请求:http://localhost:9090/say/saint2、http://localhost:9090/say/saint4,而ribbon-feign-sample-8082服务实例承担了请求:http://localhost:9090/say/saint、http://localhost:9090/say/saint3。
从现象上来看,已经Ribbon实现了负载均衡,并且默认是按照轮询的方式。
下文我们接着讨论 Ribbon是如何实现负载均衡(源码分析)?
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳