springCloud 學習筆記2 ribbon 實現客戶端負載均衡

ribbon基本介紹

Ribbon是Netflix發佈的負載均衡器,一個基於HTTP和TCP的客戶端負載均衡工具,Spring Cloud集成了Ribbon,結合Eureka,可實現客戶端的負載均衡

項目案例 客戶端實現負載均衡結構圖

客戶端負責均衡結構圖:

準備工作

  1. 搭建eureak服務端,作爲服務註冊中心使用
  2. 搭建eureak客戶端,作爲服務提供者
  3. 搭建eureak客戶端,作爲服務消費者,集成ribbon模塊

搭建eureak 服務端

1, pom文件配置

<dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
      </dependency>
**屬性文件配置**
 #服務端口
server.port=8761
#服務名稱serviceCenter
eureka.instance.hostname=yihongyuan
#禁止本身註冊
eureka.client.register-with-eureka=false
#禁止本身註冊
eureka.client.fetch-registry=false
#服務中心地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

2, 啓動類

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

搭建eureak客戶端,服務提供者

1, pom文件配置

<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </dependency>
**屬性文件配置**
spring:
 application:
   name: polices
eureka:
client:
 eureka-server-url:
   defautZone: http://localhost:8761/eureka/

2, 啓動類

@SpringBootApplication
@EnableEurekaClient
@ComponentScan(value = "com")
public class EureakRibbonProductApplication {

 public static void main(String[] args) {
     Scanner scan = new Scanner(System.in);
     String port = scan.nextLine();
     new SpringApplicationBuilder(EureakRibbonProductApplication.class).properties("server.port=" + port).run(args);
 }

}

3, 業務類

@RestController
public class PoliceController {

 @RequestMapping(value = "/call/{id}", method = RequestMethod.GET, 
 		produces = MediaType.APPLICATION_JSON_VALUE)
 public Police call(@PathVariable Integer id, HttpServletRequest request) {
 	Police p = new Police();
 	p.setId(id);
 	p.setName("angus");
 	p.setMessage(request.getRequestURL().toString());
 	return p;
 }
}

4, 註冊中心查看
啓動了兩個客戶端實例
5, 業務類測試
訪問端口8081的客戶端示例
訪問端口號爲8080的客戶端示例

搭建eureak客戶端,服務消費者,集成ribbon模塊

1, pom文件配置

<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
     </dependency>
  **屬性文件配置**
server:
port: 9000
spring:
application:
 name: erueka_ribbon_customer
eureka:
client:
 eureka-server-url:
   defautZone: http://localhost:8761/eureka/

2, 啓動類

@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(value = "com")
@Configuration
public class EurekaRibbonCustomerApplication {

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

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

3, 業務類

@Controller
public class MyController {

 @Autowired
 RestTemplate tpl;

 @GetMapping("/router")
 @ResponseBody
 public String router() {
     System.out.println("rout...");
  //   RestTemplate tpl = getRestTemplate();
     String json = tpl.getForObject("http://polices/call/1", String.class);
     return json;
 }

4, 註冊中心查看
如圖見
在這裏插入圖片描述
5, 業務類測試
客戶端負載均衡訪問8080服務提供者
客戶端負載均衡訪問8080服務提供者
客戶端默認使用輪詢的方式訪問8080,,8081實例

測試自定義負載均衡器,註解實現

1,自定義配置類

規則類 rule
public class MyRule implements IRule {

 private ILoadBalancer lb;

 public Server choose(Object key) {
     System.out.println("這是自定義的規則類");
     Random r = new Random();
     int randomNum = r.nextInt(10);
     List<Server> servers = lb.getAllServers();
     if (randomNum > 7) {
         Server s = getServerByPort(servers, 8081);
         return s;
     }
     return getServerByPort(servers, 8080);
 }

 private Server getServerByPort(List<Server> servers, int port) {
     for (Server s : servers) {
         if (s.getPort() == port) {
             return s;
         }
     }
     return null;
 }

 public void setLoadBalancer(ILoadBalancer lb) {
     this.lb = lb;
 }

 public ILoadBalancer getLoadBalancer() {
     return lb;
 }
} 
配置類
public class MyConfig {

  @Bean
  public IRule getRule() {
  	return new MyRule();
  }
}
ribbon客戶端類
@RibbonClient(name = "polices", configuration = MyConfig.class)
public class MyClient {

}

2,測試
在訪問http://127.0.0.1:9000/router 時候,服務提供着實例 8080被優先訪問到

在控制檯上可以看到使用了自定義規則類

測試自定義負載均衡器,配置實現實現

1,在配置文件中使用配置類

## ribbon自定義規則類
polices:
ribbon:
 NFLoadBalancerRuleClassName: com.bw.rule.MyRule

2,測試
自定義規則類被使用到

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