SpringCloud------Eureka客戶端負載均衡器入門介紹

 

客戶端負載均衡器

在SpringCloud中Ribbon負載均衡客戶端,會從eureka註冊中心服務器端上獲取服務註冊信息列表,緩存到本地。然後在本地實現輪訓負載均衡策略。

Ribbon與Nginx區別

服務器端負載均衡Nginx

 nginx是客戶端所有請求統一交給nginx,由nginx進行實現負載均衡請求轉發,屬於服務器端負載均衡。

 既請求有nginx服務器端進行轉發。

客戶端負載均衡Ribbon

Ribbon是從eureka註冊中心服務器端上獲取服務註冊信息列表,緩存到本地,讓後在本地實現輪訓負載均衡策略。

 既在客戶端實現負載均衡

應用場景的區別:

Nginx適合於服務器端實現負載均衡 比如Tomcat ,Ribbon適合與在微服務中RPC遠程調用實現本地服務負載均衡,比如Dubbo、SpringCloud中都是採用本地負載均衡。

環境搭建

1、搭建eureka集羣略,詳情見Eureka高可用註冊中心搭建

2、兩個服務生產者

     (1)memberProvider1

application.yml

#啓動服務端口
server: 
  port: 8001
#服務名稱(服務註冊到Eureka註冊中心)
spring:
  application:
    name: zxf-lancoo-member
#服務註冊到Eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8100/eureka,http://127.0.0.1:8200/eureka
     
#因爲是註冊中心,不會自己註冊
    register-with-eureka: true
#是否需要從eureka上獲取註冊信息
    fetch-registry: true

 pom.xml 主要部分

<!-- springCloud管理依賴 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.M7</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	
	<dependencies>
		<!-- SpringBoot整合Web組件 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- SpringBoot整合eureka客戶端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<!-- 注意: 這裏必須要添加, 否者各種依賴有問題 -->
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/libs-milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

memberController1

@RequestMapping("/discover") 
public String discover() {

    	return  "this is discover-----1";
    }

啓動

@SpringBootApplication
@EnableEurekaClient
public class AppMember {
	
	public static void main(String[] args) {
		SpringApplication.run(AppMember.class, args);
	}

}

 

    (2)memberProvider2

application.yml

###服務啓動端口號
server:
  port: 8000
###服務名稱(服務註冊到eureka名稱)  
spring:
    application:
        name: zxf-lancoo-member
###服務註冊到eureka地址   單機
#eureka:
#  client:
#    service-url:
#          defaultZone: http://localhost:8100/eureka

###集羣地址
eureka:
  client:
    service-url:
          defaultZone: http://127.0.0.1:8100/eureka,http://127.0.0.1:8200/eureka

###因爲該應用爲註冊中心,不會註冊自己
    register-with-eureka: true
###是否需要從eureka上獲取註冊信息
    fetch-registry: true

pom.xml

同memberProvider2的pom.xml一樣 略

memberController2

 @RequestMapping("/discover")
 public String discover() {
	    	
   return  "this is discover-----2";
 }

啓動

@SpringBootApplication
@EnableEurekaClient
public class ApplicationProvider {

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

 

3、消費者

application.yml

#啓動服務端口
server: 
  port: 8021
#服務名稱(服務註冊到Eureka註冊中心)
spring:
  application:
    name: zxf-lancoo-order
#服務註冊到Eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka
     
#因爲是註冊中心,不會自己註冊
    register-with-eureka: true
#是否需要從eureka上獲取註冊信息
    fetch-registry: true
    

pom.xml 同上面的生產者的一樣  這兒略過

controller

    @Autowired
	private LoadBalancerClient loadBalancerClient;

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

 /**
     * 使用負載均衡輪訓訪問
     * 
     */
    @RequestMapping("/call")
    public Object call() {
    	ServiceInstance serviceInstance = loadBalancerClient.choose("zxf-lancoo-member");
    	RestTemplate restTemplate = getRestTemplate();
        String result = restTemplate.getForObject(serviceInstance.getUri()+"/discover" , String.class);
    	return  result;
    }

啓動

@SpringBootApplication
@EnableEurekaClient
public class AppOrder {
	
	public static void main(String[] args) {
		SpringApplication.run(AppOrder.class, args);
	}

}

註冊中心,訪問127.0.0.1:8100

瀏覽器訪問消費者:http://127.0.0.1:8021/call   

多刷新幾次發現   this is discover-----1  和this is discover-----1  一直交替出現

致次Eureak中客戶端的負載均衡已經實現,

注:這種負載均衡是Eureak默認的輪訓發,Eureka中還有其讓的負載均衡,這裏不一一做演示,感興趣的可以自己去了解!

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