Ribbon-4 Ribbon脱离Eureka使用

6.5 Using Ribbon with Eureka

https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-ribbon.html

6.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 Eureka

Eureka 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 Ribbon

Setting 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微服务的时候,我只配置了7900

stores.ribbon.listOfServers=10.40.8.144:7900

那理论上来讲,理论上只访问7900,而不会去访问7901了,因为我先做Eureka上面是两个节点,但是我ribbon里面只配置了7900,

那是不是只访问7900,而不访问7901,

localhost:8010/movie/1

都是7900,没有一次是7901的

没有使用Eureka里面注册的信息,而是使用我listOfServers

stores.ribbon.listOfServers=10.40.8.144:7900

6.8 Using the Ribbon API Directly

直接使用Ribbon API,我之前写的就是Ribbon的API

public class MyClass {
    @Autowired
    private 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=8010

eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eureka

spring.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=6379

stores.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 {
  @Autowired
  private RestTemplate restTemplate;
  
  @Autowired
  private LoadBalancerClient loadBalancerClient;

  @GetMapping("/movie/{id}")
  public User findById(@PathVariable Long id) {
    // http://localhost:7900/simple/
    // VIP virtual IP
    // HAProxy Heartbeat
	 ServiceInstance 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
  @LoadBalanced
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值