三、Spring Cloud 的 Ribbon 負載均衡

一、Eureka Server 端的YML 文件配置加了一個禁止自我保護


        與Ribbon無關

        在此只加了一個enable-self-preservation爲false,禁止Eureka Server 端自我保護

spring:
  application:  
    name: microservice-eureka-server-ha1
  # 指定 profile=peer1
  profiles: peer1
server:
  port: 8761
eureka:
  server:
    enable-self-preservation: false  #禁用自我保護模式, 默認爲true
  instance:
    hostname: peer1
  client:
    #register-with-eureka: false   #表示是否將自己註冊到Eureka Server中,默認爲true, 由於當前應用就是 Eureka Server, 故而設置爲false
    #fetch-registry: false         #表示是否從 Eureka Server中獲取註冊信息, 默認爲true, 因爲這是一個單點的 Eureka Server, 不需要同步其它的 Eureka Server 節點的數據, 故而設置爲 false
    service-url:
      defaultZone: http://user:password123@peer2:8762/eureka/  # 設置與Eureka Server交互的地址, 查詢服務和註冊服務都需要依賴這個地址. 默認http://localhost:8761/eureka; 多個地址可以使用","分隔
security:
  basic:
    enabled: true
  user:
    name: user
    password: password123   

---
spring:
  application:  
    name: microservice-eureka-server-ha2
  # 指定 profile=peer2
  profiles: peer2
server:
  port: 8762
eureka:
  server:
    enable-self-preservation: false  #禁用自我保護模式, 默認爲true
  instance:
    hostname: peer1
  client:
    #register-with-eureka: false   #表示是否將自己註冊到Eureka Server中,默認爲true, 由於當前應用就是 Eureka Server, 故而設置爲false
    #fetch-registry: false         #表示是否從 Eureka Server中獲取註冊信息, 默認爲true, 因爲這是一個單點的 Eureka Server, 不需要同步其它的 Eureka Server 節點的數據, 故而設置爲 false
    service-url:
      defaultZone: http://user:password123@peer1:8761/eureka/  # 設置與Eureka Server交互的地址, 查詢服務和註冊服務都需要依賴這個地址. 默認http://localhost:8761/eureka; 多個地址可以使用","分隔
security:
  basic:
    enabled: true
  user:
    name: user
    password: password123


二、Ribbon Eureka Client 客戶端實現

         1) Eureka 客戶端的 pom.xml 文件配置

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-ribbon</artifactId>
		</dependency>


         2) 主程序文件添加註解@LoadBalanced

package com.itmuch.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 CloudConsumerApplication {

	@Bean          // 等價於 RestTemplate restTemplate = new RestTemplate();
	@LoadBalanced  // Ribbon 負載均衡
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
	public static void main(String[] args) {
		SpringApplication.run(CloudConsumerApplication.class, args);
	}
	
}

        3) Controller 控制檯調用

package com.itmuch.cloud;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.ServiceInstance;

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;

@RestController
public class UserController {
	
	@Value("${user.userServiceUrl}")   // 在yml文件中已經配置, 解決它的硬編碼問題
	private String userServiceUrl;
	
	@Value("${user.userEurekaService}")
	private String userEurekaService;
	
	@Value("${user.userEurekaName}")    
	private String userEurekaName;

	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private DiscoveryClient discoveryClient;
	
	@Autowired
	private LoadBalancerClient loadBalancerClient;
	
	@GetMapping("/load-instance")
	public String loadEurekaInstance() {
		ServiceInstance serviceInstance = this.loadBalancerClient.choose("cloud-service");
		String rs = "{" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "}";
		// 打印當前選擇的是哪個節點
		System.out.println(rs);
		return rs;
	}
	
	/**
	 * #1 第一種   基於Eureka服務方式來調用 
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/eureka/{id}")  
	public User findByIdEureka(@PathVariable Long id) {
		User user = this.restTemplate.getForObject("http://cloud-service/get/" + id, User.class);
		
		String rs = loadEurekaInstance();
		user.setEurekaServiceName(rs);
		
		return user;
	}
	
}


三、測試

   1) 啓動HA的Eureka服務

   2) 啓動3-5個提供者

   3) 啓動消費者去調用

   4) 殺掉一個提供者,測試一下Ribbon會不會再訪問它, 在本文第一節禁止了Eureka Server自我保護功能








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章