欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

2.3负载均衡:Ribbon

程序员文章站 2024-03-31 08:53:34
基于上一篇文章的工程,启动eureka-server 工程;启动service-hi工程,它的端口为8765;将service-hi的配置文件的端口改为8763,并启动,这时你会发现:service-hi在eureka-server注册了2个实例,这就相当于一个小的集群。 重新新建一个spring- ......

基于上一篇文章的工程,启动eureka-server 工程;启动service-hi工程,它的端口为8765;将service-hi的配置文件的端口改为8763,并启动,这时你会发现:service-hi在eureka-server注册了2个实例,这就相当于一个小的集群。

重新新建一个spring-boot工程,取名为:service-ribbon;
在它的pom.xml文件分别引入起步依赖spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web,代码如下:

<?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>service-ribbon</artifactid>
<version>0.0.1-snapshot</version>
<packaging>jar</packaging>

<name>service-ribbon</name>
<description>demo project for spring boot</description>

<parent>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-parent</artifactid>
<version>1.5.2.release</version>
<relativepath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceencoding>utf-8</project.build.sourceencoding>
<project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-eureka</artifactid>
</dependency>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-starter-ribbon</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-test</artifactid>
<scope>test</scope>
</dependency>
</dependencies>

<dependencymanagement>
<dependencies>
<dependency>
<groupid>org.springframework.cloud</groupid>
<artifactid>spring-cloud-dependencies</artifactid>
<version>dalston.rc1</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>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>


</project>

service-ribbon,程序端口为8764。配置文件application.yml如下:

eureka:
client:
serviceurl:
defaultzone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon

在工程的启动类中,通过@enablediscoveryclient向服务中心注册;并且向程序的ioc注入一个bean: resttemplate;并通过@loadbalanced注解表明这个restremplate开启负载均衡的功能。

@springbootapplication
@enablediscoveryclient
public class serviceribbonapplication {

public static void main(string[] args) {
springapplication.run(serviceribbonapplication.class, args);
}

@bean
@loadbalanced
resttemplate resttemplate() {
return new resttemplate();
}

}

写一个测试类helloservice,通过之前注入ioc容器的resttemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

@service
public class helloservice {

@autowired
resttemplate resttemplate;

public string hiservice(string name) {
return resttemplate.getforobject("http://service-hi/hi?name="+name,string.class);
}

}

写一个controller,在controller中用调用helloservice 的方法,代码如下:


@restcontroller
public class hellocontroler {

@autowired
helloservice helloservice;
@requestmapping(value = "/hi")
public string hi(@requestparam string name){
return helloservice.hiservice(name);
}


}

在浏览器上多次访问,浏览器交替显示:

hi forezp,i am from port:8762

hi forezp,i am from port:8763

这说明当我们通过调用resttemplate.getforobject(“http://service-hi/hi?name=“+name,string.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。

此时的架构

  • 一个服务注册中心,eureka server,端口为8765
  • service-hi工程跑了两个实例,端口分别为8762,8763,分别向服务注册中心注册
  • sercvice-ribbon端口为8764,向服务注册中心注册
  • 当sercvice-ribbon通过resttemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口;