SpringCloud(第 011 篇)電影Ribbon微服務(wù)撵儿,脫離Eureka使用配置listOfServers進(jìn)行客戶端負(fù)載均衡調(diào)度
一、大致介紹
1惶桐、通過(guò)嘗試脫離服務(wù)治理框架撮弧,脫離 eureka 生態(tài)圈潘懊,單獨(dú)操作客戶端負(fù)載均衡調(diào)度;
2贿衍、本章節(jié)僅僅只是使用了 restTemplate.getForObject 來(lái)測(cè)試客戶端負(fù)載均衡算法授舟;
二、實(shí)現(xiàn)步驟
2.1 添加 maven 引用包
<?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>springms-consumer-movie-ribbon-properties-without-eureka</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.springms.cloud</groupId>
<artifactId>springms-spring-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- web模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 客戶端發(fā)現(xiàn)模塊 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 監(jiān)控和管理生產(chǎn)環(huán)境的模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
2.2 添加應(yīng)用配置文件(springms-consumer-movie-ribbon-properties-without-eureka\src\main\resources\application.yml)
spring:
application:
name: springms-consumer-movie-ribbon-properties-without-eureka
server:
port: 8040
#做負(fù)載均衡的時(shí)候贸辈,不需要這個(gè)動(dòng)態(tài)配置的地址
#user:
# userServicePath: http://localhost:7900/simple/
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://admin:admin@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
ribbon:
eureka:
enabled: false # 禁用 eureka
springms-provider-user:
ribbon:
# 測(cè)試一
listOfServers: localhost:7899
# # 測(cè)試二
# listOfServers: localhost:7898,localhost:7899,localhost:7900
2.3 添加實(shí)體用戶類(lèi)User(springms-consumer-movie-ribbon-properties-without-eureka\src\main\java\com\springms\cloud\entity\User.java)
package com.springms.cloud.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return this.age;
}
public void setAge(Short age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
2.4 添加Web訪問(wèn)層Controller(springms-consumer-movie-ribbon-properties\src\main\java\com\springms\cloud\controller\MovieRibbonPropertiesController.java)
package com.springms.cloud.controller;
import com.springms.cloud.entity.User;
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;
@RestController
public class MoviePropertiesWithoutEurekaController {
@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("springms-provider-user");
System.out.println(">>>>>" + " " + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());
return this.restTemplate.getForObject("http://springms-provider-user/simple/" + id, User.class);
}
}
2.5 添加電影微服務(wù)啟動(dòng)類(lèi)(springms-consumer-movie-ribbon-properties-without-eureka\src\main\java\com\springms\cloud\MsConsumerMoviePropertiesWithoutEurekaApplication.java)
package com.springms.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;
/**
* 電影Ribbon微服務(wù)释树,脫離Eureka使用配置listOfServers進(jìn)行客戶端負(fù)載均衡調(diào)度。
*
* LoadBalanced:該負(fù)載均衡注解擎淤,已經(jīng)整合了 Ribbon躏哩;
*
* Ribbon 的默認(rèn)負(fù)載均衡的算法為:輪詢(xún);
*
* 配置文件優(yōu)先級(jí)最高揉燃,Java代碼設(shè)置的配置其次筋栋,默認(rèn)的配置優(yōu)先級(jí)最低;
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 2017/9/18
*
*/
@SpringBootApplication
@EnableEurekaClient
public class MsConsumerMoviePropertiesWithoutEurekaApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MsConsumerMoviePropertiesWithoutEurekaApplication.class, args);
System.out.println("【【【【【【 電影微服務(wù)-PropertiesWithoutEureka定制Ribbon 】】】】】】已啟動(dòng).");
}
}
三弊攘、測(cè)試
/****************************************************************************************
一、電影Ribbon微服務(wù)襟交,脫離Eureka使用配置listOfServers進(jìn)行客戶端負(fù)載均衡調(diào)度(正常使用服務(wù)測(cè)試):
1、application.yml 配置:ribbon.eureka.enabled: false
2捣域、application.yml 配置:springms-provider-user.ribbon.listOfServers: localhost:7900
3、啟動(dòng) springms-provider-user 模塊服務(wù)焕梅,啟動(dòng)3個(gè)端口(7900迹鹅、7899贞言、7898);
4该窗、啟動(dòng) springms-provider-user2 模塊服務(wù),啟動(dòng)2個(gè)端口(7997酗失、7996)(直接將用戶微服務(wù) spring.application.name 改了個(gè)名字為 springms-provider-user2 再啟動(dòng)而已);
5级零、啟動(dòng) springms-consumer-movie-ribbon-properties-without-eureka 模塊服務(wù)滞乙;
6、在瀏覽器輸入地址 http://localhost:8040/movie/1鉴嗤,連續(xù)刷新9次,然后看看 springms-provider-user醉锅、springms-provider-user2 的這幾個(gè)端口的服務(wù)打印日志情況,正常情況下只會(huì)有7999該端口才會(huì)有9條用戶信息日志打印出來(lái)硬耍;
總結(jié):之所以只有用戶微服務(wù)7999端口打印日志垄琐,首先禁用了eureka的使用,如果沒(méi)禁用的話经柴,3個(gè)端口按道理都會(huì)打印日志狸窘;其次配置 listOfServers 僅僅只選擇了7999一個(gè)端口的微服務(wù);
****************************************************************************************/
/****************************************************************************************
二坯认、電影Ribbon微服務(wù)翻擒,脫離Eureka使用配置listOfServers進(jìn)行客戶端負(fù)載均衡調(diào)度(負(fù)載均衡調(diào)度):
1、application.yml 配置:ribbon.eureka.enabled: false
2牛哺、application.yml 配置:springms-provider-user.ribbon.listOfServers: localhost:7898,localhost:7899,localhost:7900
3陋气、啟動(dòng) springms-provider-user 模塊服務(wù),啟動(dòng)3個(gè)端口(7900、7899、7898)撰茎;
4、啟動(dòng) springms-provider-user2 模塊服務(wù)议慰,啟動(dòng)2個(gè)端口(7997、7996)(直接將用戶微服務(wù) spring.application.name 改了個(gè)名字為 springms-provider-user2 再啟動(dòng)而已)奴曙;
5褒脯、啟動(dòng) springms-consumer-movie-ribbon-properties-without-eureka 模塊服務(wù);
6缆毁、在瀏覽器輸入地址 http://localhost:8040/movie/1番川,連續(xù)刷新9次,然后看看 springms-provider-user脊框、springms-provider-user2 的這幾個(gè)端口的服務(wù)打印日志情況颁督,正常情況下springms-provider-user的3個(gè)端口都會(huì)打印日志,而且是輪詢(xún)打咏奖ⅰ沉御;
總結(jié):之所以7900、7899昭灵、7898三個(gè)端口輪詢(xún)打印吠裆,是因?yàn)闆](méi)有配置任何調(diào)度算法伐谈,默認(rèn)的調(diào)度算法是輪詢(xún),所以3個(gè)端口當(dāng)然會(huì)輪詢(xún)打印用戶信息试疙;
****************************************************************************************/
四诵棵、下載地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接
歡迎關(guān)注,您的肯定是對(duì)我最大的支持!!!