美文网首页
SpringCloud-3之Ribbon

SpringCloud-3之Ribbon

作者: 小螺丝钉cici | 来源:发表于2019-07-27 14:53 被阅读0次

使用Ribbon和SpringCloud实现客户端负载均衡
官网demo:https://spring.io/guides/gs/client-side-load-balancing/

1、Ribbon介绍

Ribbon是一个内置于服务消费者(客户端)的负载均衡器,能够对服务提供者发起调用时,实现负载均衡的处理。客户端访问服务提供者方式有三种(如图):

  • 1.直接访问 直接对服务提供者发起调用,
    特点:没有负载均衡的能力和容错保证。
  • 2.自己维护服务地址列表。
    特点:编程效率低,手动维护成本。
  • 3.通过eureka注册中心进行查找(推荐使用)
    特点:实现透明目的。
image.png

实战DEMO:

实现第二种方式。不同的eureka注册中心,自己维护服务地址列表。

一、创建say-hello模块

1.新建有ribbon组件的项目(sayHello 服务提供者):


image.png

2.Spring ribbon组件官网
https://spring.io/guides/gs/client-side-load-balancing/
复制maven的pom.xml到自己的项目中

image.png

3.sayHello 服务提供者程序(完善SayHelloApplication类)

@RestController
@SpringBootApplication
public class SayHelloApplication {

    @RequestMapping("/greeting")
    public String greet(){
        List<String> greetings = Arrays.asList("hello1","hello2" ,"hello2") ;
        Random r = new Random() ;
        String str = greetings.get(r.nextInt(3));
        System.out.println(str);
        return str ;
    }
    @RequestMapping("/")
    public String home(){
        return "Hi!!" ;
    }
    public static void main(String[] args) {
        SpringApplication.run(SayHelloApplication.class, args);
    }

}

4.编写application.properties文件
spring.application.name=say-hello
server.port=5555

二、创建user消费者模块

1.新建User客户端module代码,同1;
从官网复制user客户端的pom.xml到项目中。

image.png

2.完善UserApplication类

@RestController
@SpringBootApplication
@RibbonClient(name="say-hello" , configuration = SayHelloConfiguration.class)
public class UserApplication {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate() ;
    }

    @Autowired
    private RestTemplate restTemplate ;

    @RequestMapping("/hi")
    public String hi(@RequestParam(value="name" , defaultValue = "springcloud") String name){
        String greeting = this.restTemplate.getForObject("http://localhost:5555/greeting",String.class) ;
        return String.format("%s , %s!" ,greeting , name) ;
    }

    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }

}

3.编写application.properties文件
spring.application.name=user 2 server.port=4444

  1. 启动程序
    直连访问 http://localhost:5555/
image.png image.png
三.消费端静态服务列
image.png

1、启动多个say-hello服务实例 配置idea运行配置,指定不同端口:
1 --server.port=6666

image.png

2、修改user模块的属性文件 [resources/application.properties]

spring.application.name=user
server.port=4444
say-hello.ribbon.eureka.enabled=false
say-hello.ribbon.listOfServers=localhost:6666,localhost:6665,localhost:6664
say-hello.ribbon.ServerListRefreshInterval=15000

4.用say-hello 替换userApplication里的localhost:5555

String greeting = this.restTemplate.getForObject("http://say-hello/greeting",String.class) ;

此时会报这个错,因为say-hello使用的是模版。需要告诉它使用的是ribbon的客户端

image.png image.png

5.新建配置类

@RestController
@SpringBootApplication
@RibbonClient(name="say-hello" , configuration = SayHelloConfiguration.class)
public class UserApplication {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate() ;
    }

    @Autowired
    private RestTemplate restTemplate ;

    @RequestMapping("/hi")
    public String hi(@RequestParam(value="name" , defaultValue = "springcloud") String name){
        String greeting = this.restTemplate.getForObject("http://say-hello/greeting",String.class) ;
        return String.format("%s , %s!" ,greeting , name) ;
    }

    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }

}


/**
 * SayHello配置类
 */
public class SayHelloConfiguration {
    @Autowired
    IClientConfig ribbonClientConfig;

    @Bean
    public IPing ribbonPing(IClientConfig config) {
        return new PingUrl();
    }

    @Bean
    public IRule ribbonRule(IClientConfig config) {
        return new AvailabilityFilteringRule();
    }
}

6.配置application.yml文件
级别层数多的时候,减少配置代码
其实跟property文件一样

image.png

7.启动消费端
通过页面的结果,和使用的服务client发现,多个服务交替在执行请求

http://localhost:4444/hi?name=

image.png image.png image.png

断掉所有服务,只剩下一个6666的服务,会发现页面所有的呈现,全部来自于这个6666的服务。

实现第三种方式,通过Eureka服务器中获取服务列表

image.png

1.新建say-hello2 修改pom.xml(复制say-hello文件)

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cici</groupId>
    <artifactId>say-hello2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>say-hello2</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</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>

2.SayHelloApplication上添加EnableDiscoveryClient注解

3.修改application.properties文件

eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
spring.application.name=say-hello
server.port=5555

4.启动服务器
4.1 启动Eureka注册中心 运行eureka-server模拟。
4.2 启动say-hello2模块多次 每次指定一个不同的端口,形成一个服务实例,该服务实例会在eureka中心进行注册。

image.png image.png

5.user2 修改application.properties文件

eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
spring.application.name=user
server.port=4444

在注册中心中要看到所有的提供者服务(say-hello) 和 消费者(user-client)
一开始一直不能看到user-client 信息是因为pom中少加了这个引用

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
image.png image.png

此时访问会发现,消费者访问的信息,一直在三个提供者交替执行。

启动顺序:
1.启动eureka 注册中心 eureka-server
2.依次启动多个服务提供者 say-hello
3.启动消费者 user-client

相关文章

网友评论

      本文标题:SpringCloud-3之Ribbon

      本文链接:https://www.haomeiwen.com/subject/kbnwlctx.html