anyuan2002.com - vwin网

查找: 您的方位主页 > 网络频道 > 阅览资讯:微服务架构-Ribbon
vwin娱乐场

微服务架构-Ribbon

2019-04-04 08:19:43 来历:www.anyuan2002.com 【

Ribbon介绍

Spring Cloud Ribbon是依据Netflix Ribbon完结的一套客户端 负载均衡的东西。

简略的说,Ribbon是Netflix发布的开源项目,主要功能是供给客户端的软件负载均衡算法,将Netflix的中间层服务衔接在一。Ribbon客户端组件供给一系列完善的装备项如衔接超时,重试等。简略的说,便是在装备文件中列出Load Balancer(简称LB)后边一切的机器,Ribbon会主动的协助你依据某种规矩(如简略轮询,随机衔接等)去衔接这些机器。咱们也很简单运用Ribbon完结自界说的负载均衡算法。

 

过程:

1.修正cloud-consumer-80 pom文件

 

<?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>cloud-parent</artifactId>
    <groupId>com.el.cloud</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.el.cloud</groupId>
  <artifactId>cloud-consumer-80</artifactId>

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

  <description>部分微服务顾客</description>

  <dependencies>
    <dependency><!-- 自己界说的api -->
      <groupId>com.el.cloud</groupId>
      <artifactId>cloud-api</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- 修正后当即收效,热布置 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>springloaded</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
    </dependency>

    <!-- Ribbon相关 -->
    <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.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  </dependencies>
</project>

修正application.yml 追加eureka的服务注册地址

server:
 port: 80
eureka:
 client:
  register-with-eureka: false
  service-url:
   defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

对ConfigBean进行新注解@LoadBalanced 取得Rest时参加Ribbon的装备

package com.el.consumer.cloud.configure;

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;

/**
 * @Auther: roman.zhang
 * @Date: 2019/3/19 13:59
 * @Version:V1.0
 * @Description:ConfigBean
 */
@Configuration
public class ConfigBean {

  @Bean
  @LoadBalanced
  public RestTemplate getRestTemplate(){
    return new RestTemplate();
  }

}

主发动类DeptConsumer80_App增加@EnableEurekaClient

 

package com.el.consumer.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

修正DeptController_Consumer客户端拜访类

 

package com.el.consumer.cloud.controller;

import com.el.consumer.cloud.entities.Dept;
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;

import java.util.List;

/**
 * @Auther: roman.zhang
 * @Date: 2019/3/19 14:02
 * @Version:V1.0
 * @Description:DeptController_Consumer
 */
@RestController
public class DeptController_Consumer {
  //private static final String REST_URL_PREFIX = "http://localhost:8002";
  private static final String REST_URL_PREFIX = "http://cloud-dept";
  @Autowired
  private RestTemplate restTemplate;

  @RequestMapping(value = "/consumer/dept/add")
  public boolean add(Dept dept){
    return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,boolean.class);
  }

  @RequestMapping(value="/consumer/dept/get/{id}")
  public Dept get(@PathVariable("id") Long id){
    return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);
  }

  @RequestMapping(value="/consumer/dept/list")
  public List<Dept> list(){
    return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class);
  }
}

测验:

1. 先发动3个eureka集群后,再发动cloud-provider-8001并注册进eureka。

2.发动cloud-consumer-80

3.拜访接口:http://localhost/consumer/dept/get/1 http://localhost/consumer/dept/list http://localhost/consumer/dept/add?dname=大数据部

总结:

Ribbon和Eureka整合后Consumer能够直接调用服务而不必再关怀地址和端口号

 

Ribbon负载均衡

架构阐明:

微服务架构-Ribbon

Ribbon在作业时分红两步
第一步先挑选 EurekaServer ,它优先挑选在同一个区域内负载较少的server.
第二步再依据用户指定的战略,在从server取到的服务注册列表中挑选一个地址。
其间Ribbon供给了多种战略:比方轮询、随机和依据呼应时刻加权。

过程:

1.参阅cloud-provider-dept-8001,新建两份,别离命名为8002,8003

2.新建8002/8003数据库,各自微服务别离连各自的数据库

3.修正8002/8003各自YML

4.发动3个eureka集群装备区

5.发动3个Dept微服务发动并各自测验经过

6.发动cloud-consumer-80

7.客户端经过Ribbo完结负载均衡并拜访上一步的Dept微服务

总结:Ribbon其实便是一个软负载均衡的客户端组件,
他能够和其他所需恳求的客户端结合运用,和eureka结合仅仅其间的一个实例。

 

 
 

本文地址:http://www.anyuan2002.com/a/question/100257.html
Tags: 服务 架构 -Ribbon
修改:vwin网
关于咱们 | 联络咱们 | 友情链接 | 网站地图 | Sitemap | App | 回来顶部