微服务笔记之Spring Cloud 客户端负载均衡器Ribbon

本文介绍了如何使用Ribbon作为Spring Cloud的客户端负载均衡器,结合Eureka服务注册中心,实现对服务提供者的消费。通过配置pom.xml、创建项目结构、编写启动类和API,以及设置应用配置,启动并验证了服务。当有两个或多个servicehi实例时,通过service-ribbon访问,请求会被轮询分配到不同的实例处理。
摘要由CSDN通过智能技术生成

Ribbon是一个客户端的负载均衡器,它提供对HTTP和TCP客户端的访问控制; 

结合上篇笔记中的示例:eurekaserver作为注册中心,servicehi作为服务提供者(eureka client);

下面直接创建一个项目 service-ribbon,基于 Ribbon 作为负载均衡,来消费servicehi提供的服务。

1. 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">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.springcloud.lcl</groupId>
  <artifactId>service-ribbon</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>service-ribbon</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
      <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
  </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/>
    </parent>

    <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.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

</project>

注意此处使用的spring-boot、spring-cloud版本必须兼容;

2.   项目目录及代码

ServiceRibbonApplication 启动类:

package com.springcloud.lcl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(ServiceRibbonApplication.class,args);
    }

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

 api定义类HelloControler:  

package com.springcloud.lcl.controller;

import com.springcloud.lcl.service.HelloService;
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;

/**
 * @author lcl
 * @version 0.1
 * @date 2018/10/15
 **/
@RestController
public class HelloControler {
    @Autowired
    HelloService helloService;

    @RequestMapping(value = "/hi")
    public String hi(@RequestParam String name){
        return helloService.hiService(name);
    }
}

 service层HelloService类:

package com.springcloud.lcl.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author lcl
 * @version 0.1
 * @date 2018/10/15
 **/
@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name){
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }


}

3.  配置文件application.properties(也可用yml方式) ;

spring.application.name=service-ribbon

server.port=8764

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

4.  启动及验证

     启动eurekaserver,再依次启动servicehi,service-ribbon;其中servicehi服务可修改端口号后再起一个实例(例:端口8763)

    访问eureka http://localhost:8761/  可看到如下,预期的几个服务已启动并注册;

    访问service-ribbon提供的服务: http://localhost:8764/hi?name=name1

    

 再次请求

  

   会看到两个servicehi服务提供者会轮番处理请求;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值