6.5 Using Ribbon with Eurekahttps://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html6.6 Example: How to Use Ribbon Without Eureka让Ribbon和Eureka连用,以及单独使用Ribbon,不使用Eureka,我们之前讲的案例都是让Ribbon和Eureka连用,所以这里的细节就不讨论了,只说一点注意点,如果没有其他的ZONE的数据源,然后他就会猜测,基于你客户端的配置,If there is no other source of zone data, then a guess is made, based on the client configuration (as opposed to the instance configuration). We take eureka.client.availabilityZones, which is a map from region name to a list of zones, and pull out the first zone for the instance’s own region (that is, the eureka.client.region, which defaults to "us-east-1", for compatibility with native Netflix).eureka他有Region和Zone,使用这个东西availabilityZones,这是一个从region到zone的映射,从list里面拉第一个zone,作为这个实例的region,其实就是eureka.client.region,当然也可以在配置文件里面配,这个默认是"us-east-1",/*** Gets the region (used in AWS datacenters) where this instance resides.*/
private String region = "us-east-1";Ribbon和Eureka连用我们就讨论到这里,下面我们讨论单独使用Ribbon,而不去使用Eureka呢,这个他其实分为两种情况,Eureka根本就不在我的classpath下面,6.6 Example: How to Use Ribbon Without EurekaEureka is a convenient way to abstract the discovery of remote servers so that you do not have to hard code their URLs in clients. However, if you prefer not to use Eureka, Ribbon and Feign also work. Suppose you have declared a @RibbonClient for "stores", and Eureka is not in use (and not even on the classpath). The Ribbon client defaults to a configured server list. You can supply the configuration as follows:application.yml. stores:ribbon:listOfServers: example.com,google.com就是根本没有引用Eureka,第二种是我又Eureka,那我要怎么去禁用Eureka呢,默认不是跟Eureka连用,我怎么样去禁用呢,如果说没有Eureka的依赖的话,我只要怎么玩就OK了,如果有的话,我就得这么玩6.7 Example: Disable Eureka Use in RibbonSetting the ribbon.eureka.enabled property to false explicitly disables the use of Eureka in Ribbon, as shown in the following example:application.yml. ribbon:eureka:enabled: false禁止Eureka的这种能力,禁止Ribbon Eureka的使用microservice-simple-provider-user.ribbon.NFLoadBalancerRuleClassName
=com.netflix.loadbalancer.RandomRule首先把这个干掉,那下面我们做一个测试,stores:ribbon:listOfServers: example.com,google.com我请求这个user微服务的时候,我只配置了7900stores.ribbon.listOfServers=10.40.8.144:7900那理论上来讲,理论上只访问7900,而不会去访问7901了,因为我先做Eureka上面是两个节点,但是我ribbon里面只配置了7900,那是不是只访问7900,而不访问7901,localhost:8010/movie/1都是7900,没有一次是7901的没有使用Eureka里面注册的信息,而是使用我listOfServersstores.ribbon.listOfServers=10.40.8.144:79006.8 Using the Ribbon API Directly直接使用Ribbon API,我之前写的就是Ribbon的APIpublic class MyClass {@Autowiredprivate LoadBalancerClient loadBalancer;public void doStuff() {ServiceInstance instance = loadBalancer.choose("stores");URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort()));// ... do something with the URI}
}构造成一个URI去访问,首先他是一个客户端的负载均衡,因为我们使用了Eureka,所以如果想要使用Ribbon的话,就不需要引用这个了,Eureka就自动带了这个依赖,Spring Cloud自动带了这个依赖,如果想要自定义Ribbon Client配置的话,需要这么玩@Configuration
@RibbonClient(name = "custom", configuration = CustomConfiguration.class)
public class TestConfiguration {
}使用配置文件去配置Ribbon Client,讲怎么脱离Eureka去使用Ribbon,还可以使用Ribbon原生的API
<?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><artifactId>microservice-consumer-movie-ribbon-without-eureka</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>microservice-simple-consumer-movie</name><description>Demo project for Spring Boot</description><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></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.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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
#debug=true
server.port=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=microservice-consumer-movie-ribbon
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
eureka.client.healthcheck.enabled=true
spring.redis.host=10.40.8.152
spring.redis.password=1234
spring.redis.port=6379stores.ribbon.listOfServers=10.40.8.144:7900
package com.learn.cloud.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import com.learn.cloud.entity.User;@RestController
public class MovieController {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate LoadBalancerClient loadBalancerClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {// http://localhost:7900/simple/// VIP virtual IP// HAProxy HeartbeatServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-simple-provider-user");System.out.println("==============" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());return this.restTemplate.getForObject("http://microservice-simple-provider-user/simple/" + id, User.class);}@GetMapping("/test")public String test() {
//    ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-simple-provider-user");
//    System.out.println("111" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("microservice-simple-provider-user2");System.out.println("222" + ":" + serviceInstance2.getServiceId() + ":" + serviceInstance2.getHost() + ":" + serviceInstance2.getPort());return "1";}}
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EnableEurekaClient
public class ConsumerMovieRibbonApplication {@Bean@LoadBalancedpublic RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(ConsumerMovieRibbonApplication.class, args);}
}

Ribbon-4 Ribbon脱离Eureka使用相关推荐

  1. 脱离Eureka使用Ribbon

    项目的github:https://github.com/simonsfan/SpringCloud.git Ribbon可以实现客户端的负载均衡,在使用Ribbon实现客户端负载均衡一文中,详细描述 ...

  2. SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)...

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册 ...

  3. SpringCloud版本Hoxton SR5 --- 第三讲:Ribbon 、Ribbon与Feign配合使用

    传送门:SpringCloud版本Hoxton SR5 --- 第一讲:认识 先看Ribbon.Fegin可以完成的功能,或者说他在项目中的定位和作用. 上篇文章主要讲:功能和作用都是用大白话,主要是 ...

  4. SpringCloud Netflix Ribbon

    文章目录 一. Ribbon简介 二. 使用Ribbon开发微服务 1 创建springcloud工程 和 commons子模块 2 开发服务提供者 - ribbonappservice 3 开发服务 ...

  5. springcloud必知功能使用教程

    springcloud Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均衡.断路 ...

  6. Spring Cloud 微服务技术栈

    Spring Cloud 简介 主要内容 微服务简介 SpringCloud 简介 SpringCloud 框架结构 SpringCloud 和 Dubbo 的对比 SpringCloud 版本号说明 ...

  7. SpringCloud:Ribbon负载均衡(基本使用、 负载均衡、自定义配置、禁用 Eureka 实现 Ribbon 调用)

    现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现在的问题,所有的微服务汇集到了 Eureka 之 ...

  8. Spring Cloud部分源码分析Eureka,Ribbon,Feign,Zuul

    Eureka SpringCloud Eureka使用NetFlix Eureka来实现的,它包括了服务端组件和客户端组件,并且都是用java 编写的. Eureka服务端就是服务注册中心, Eure ...

  9. 跟着狂神学SpringCloud(Rest环境搭建+Eureka服务注册与发现+ribbon+Feign负载均衡+Hystrix+服务熔断+Zuul路由网关+SpringCloud config分布)

    跟着狂神学SpringCloud SpringCloud 回顾之前的知识- JavaSE 数据库 前端 Servlet Http Mybatis Spring SpringMVC SpringBoot ...

最新文章

  1. mysql数据库优化课程---15、mysql优化步骤(mysql中最常用最立竿见影的优化是什么)...
  2. javascript和“主流大型语言”(c# JAVA C++等)的差异
  3. 来,拆一堆芯片看看!
  4. java文件替换一行数据_用Golang替换文件中的一行
  5. 电大1253c语言程序设计考试题,电大1253《C语言程序设计》开放大学期末考试试题2020年1月(含答案)...
  6. C++中,float double区别
  7. Linux用户(user)和用户组(group)的日常管理与操作教程概述
  8. 《Python语言程序设计》——3.4 实例研究:最小数量的硬币
  9. WINDOWS 2008的trustedinstallerexe占用过多CPU导致服务器性能下降的问题处理
  10. Kali 更新失败 签名无效
  11. oracle创建用户和表空间,授权,oracle用imp导入dmp文件
  12. 使用ActionForm的validate()进行验证
  13. 七彩安卓影视APP源码独立解析接口
  14. 企业微信网页应用开发 - 消息/事件回调接口
  15. golang中的错误fatal error: concurrent map writes
  16. lch 儿童围棋课堂 初级篇1 ( (李昌镐 著))
  17. 系统上电后 bootloader的执行流程
  18. 前端工作总结187-json校验工具
  19. 研习社实战营--朱老师、猫老师,手把手带你入场打猎!
  20. U9服务器显示不全,U9常见技术问题分析与解决.docx

热门文章

  1. iOS8 用AVAudioPlayer播放音乐(Swift)
  2. C#异步编程(一):异步基础
  3. CSS3实现漂亮ToolTips
  4. UVA 621 Secret Research
  5. Bullmind-在线UML软件工具箱
  6. CentOS7 命令行变成-bash-4.2$
  7. 2012自学CCNP路由与交换之四交换机初始化
  8. PostgreSQL数据类型-枚举类型、几何类型、网络地址类型和其他数据类型
  9. CSS之div和span标签
  10. JVM基础(6)-常用参数总结