SpringCloud之旅(二)Riddon負載均衡

  • Springcloud之Riddon

  • Riddon爲負載均衡而生

  • Spring Cloud Ribbon 是基於HTTP和TCP調用的負載均衡工具,是基於Netflix Ribbon實現的。

  • 使用Ribbon
    1、引入Ribbon的依賴spring-cloud-starter-ribbon
    2、消費通過用@LoadBalanced修飾的RestTemplete來調用進行面向服務接口REST
    3、服務端建立多個實例


測試的項目結構

  • eureka Server
  • client1
  • client2
  • Riddon

創建步驟
1、eureka server創建我上文有可以直接對照
2、client1和client2創建如同創建eureka server步驟相同,只是配置文件和部分註解不相同
application.properties

server.port=9001
eureka.client.service-url.defultZone=http://localhost:8761/eureka/
spring.application.name=client1

controller

package com.example.client.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class User {
    @RequestMapping("/user/findById")
    public String findById(@RequestParam("id")String id){
        return "這個是Spring客戶端1爲"+id;
    }
}

client2部分代碼
application.properties

server.port=9003
eureka.client.service-url.defultZone=http://localhost:8761/eureka/
spring.application.name=client1

controller

package com.example.client1.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class User {
    @RequestMapping("/user/findById")
    public String findById(@RequestParam("id")String id){
        return "這個是Spring客戶端2爲"+id;
    }
}

3、Riddon:負載均衡
application.properties

eureka.client.service-url.defultZone=http://localhost:8761/eureka/
server.port=7000
spring.application.name=service-ribbon


controller

package com.example.ribbon.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {
    @Autowired
    RestTemplate restTemplate;
    @RequestMapping("/hi")
    public String hi(@RequestParam("id")String id){
        return restTemplate.getForObject("http://CLIENT1/user/findById?id="+id,String.class);
    }
}

啓動類需要修改,如下

package com.example.ribbon;

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向服務中心註冊
@EnableEurekaClient
public class RibbonApplication {

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

    //並且向程序的ioc注入一個bean: restTemplate;並通過@LoadBalanced註解表明這個restRemplate開啓負載均衡的功能。
    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

最後依次運行Server、client1、client2、riddon
輸入localhost:8761可以看到三個都註冊進來了
在這裏插入圖片描述
通過http://localhost:7000/hi?id=wl進行訪問
在這裏插入圖片描述
在這裏插入圖片描述
刷新會出現兩種不一樣的文字,說明負載均衡起到了作用

最後附上我這個GitHub項目的地址https://github.com/crh12345/SpringCloud/tree/master/SpringCloudRiddon歡迎star

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