Ribbon-4 Ribbon脱离Eureka使用
生活随笔
收集整理的這篇文章主要介紹了
Ribbon-4 Ribbon脱离Eureka使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ribbon-3使用配置文件自定义Rib
- 下一篇: Feign-1 Feign的简介及基础使