学习SpringCloud之负载均衡Ribbon

版权声明:本文为博主czb1n(https://www.zbin.tech)原创文章,未经博主允许不得转载。 https://blog.csdn.net/czb1n/article/details/89173984

简介

  • 什么是负载均衡?
    负载均衡是分布式架构中不可或缺的一个组件,其意义在于通过一定的规则或者算法去将请求分摊到各个服务提供者。

  • Ribbon是一个客户端的负载均衡器,它提供一系列让你控制HTTP和TCP客户端的能力。

以下示例均基于SpringCloud的Greenwich.SR1版本,且需要依赖到之前介绍Eureka的文章

基础依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

        <!-- 使用Eureka来配合Ribbon -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

Ribbon

需要测试Ribbon的功能,需要以下几个步骤。

  1. 先运行Eureka-Server,启动注册中心。
  2. 再运行两个不同端口(6603、6604)的Eureka-Client
    需要改一下之前的代码,在 /hello 接口中增加端口的输出来区分服务。
    @Value("\${server.port}")
    var port: String? = null

    @RequestMapping("/hello")
    fun hello(@RequestParam("name") name: String): String {
        return "response from $port: hello $name."
    }
  1. 配置启动Ribbon测试应用
    因为我们这里需要依赖到Eureka,所以要指定注册中心的地址。
    application.yml
    server:
      port: 6605

    eureka:
      instance:
        hostname: localhost
      client:
        serviceUrl:
          defaultZone: http://localhost:6600/eureka/

    spring:
      application:
        name: ribbon-client

在启动类中,要增加 @EnableDiscoveryClient 注解。

    @SpringBootApplication
    @EnableEurekaClient
    @EnableDiscoveryClient
    class RibbonClientStarter

    fun main(args: Array<String>) {
        runApplication<RibbonClientStarter>(*args)
    }

启动之后,打开 http://localhost:6600 可以看到现在启动的服务。

    EUREKA-CLIENT	UP (2) - 192.168.1.135:eureka-client:6603 , 192.168.1.135:eureka-client:6604
    RIBBON-CLIENT	UP (1) - 192.168.1.135:ribbon-client:6605
  1. 创建一个Controller去调用Eureka-Client的服务。
    先配置创建一个RestTemplate的Bean去负责调用。
    @Configuration
    class RestTemplateConfiguration {

        // 使用 @LoadBalance 开启负载均衡。
        @Bean
        @LoadBalanced
        fun restTemplate(): RestTemplate {
            return RestTemplate()
        }

    }

再在Controller中使用RestTemplate。

    @RestController
    class DemoController {

        @Autowired
        lateinit var restTemplate: RestTemplate

        @RequestMapping("/hello")
        fun hello(@RequestParam("name") name: String): String? {
            // 其中 EUREKA-CLIENT 为application.yml中的应用名的大写形式,也是注册中心中显示的名字。
            // hello 则为服务提供的接口名。
            return restTemplate.getForObject("http://EUREKA-CLIENT/hello?name=$name", String::class.java)
        }

    }

多次访问 http://localhost:6605/hello?name=czb1n,会轮流显示:response from 6603: hello czb1n.response from 6604: hello czb1n.

其他

示例代码地址: https://github.com/czb1n/learn-spring-cloud-with-kotlin

猜你喜欢

转载自blog.csdn.net/czb1n/article/details/89173984