1. 程式人生 > >SpringCloud系列(五)——Ribbon實現

SpringCloud系列(五)——Ribbon實現

  • 新建一個簡單的Springboot專案——ribbon-service,然後編寫一個方法,開啟兩個埠啟動專案。
@RequestMapping(value = "/person", method = RequestMethod.GET, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Person getPerson(HttpServletRequest request){
        Person p = new Person();
        p.setId(1);
        p.setName
("小米"); p.setMessage(request.getRequestURL().toString()); return p; }
  • 新建一個簡單的maven專案,通過簡單的main方法訪問ribbon-service:
public static void main(String[] args) throws Exception {
        ConfigurationManager.getConfigInstance().setProperty(
                "my-client.ribbon.listOfServers"
, "localhost:8080,localhost:8081"); RestClient client = (RestClient) ClientFactory.getNamedClient("my-client"); HttpRequest request = HttpRequest.newBuilder().uri(("/person")).build(); for(int i = 0;i<10;i++){ HttpResponse response = client.executeWithLoadBalancer(request);
String json = response.getEntity(String.class); System.out.println(json); } }
  • 輸出,可以看到Ribbon的預設策略就是RoundRobinRule(輪換):
{"id":1,"name":"小米","message":"http://localhost:8081/person"}
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
{"id":1,"name":"小米","message":"http://localhost:8081/person"}
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
{"id":1,"name":"小米","message":"http://localhost:8081/person"}
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
{"id":1,"name":"小米","message":"http://localhost:8081/person"}
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
{"id":1,"name":"小米","message":"http://localhost:8081/person"}
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
  • 接下來自定義策略,新建一個類,實現IRule,重寫choose():
public class MyRule implements IRule {

    private ILoadBalancer lb;

    public Server choose(Object key) {
        Random r= new Random();
        int num = r.nextInt(10);

        List<Server> servers =  lb.getAllServers();

        if(num > 7){
            return getServerByPost(servers,8081);
        }else{
            return getServerByPost(servers,8080);
        }

    }

    private Server getServerByPost(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 this.lb;
    }

}
  • main方法使用MyRule策略:
ConfigurationManager.getConfigInstance().setProperty(
                "my-client.ribbon.NFLoadBalancerRuleClassName", MyRule.class.getName());
  • 測試,可以看到,呼叫8080埠比8081埠的多:
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
{"id":1,"name":"小米","message":"http://localhost:8081/person"}
{"id":1,"name":"小米","message":"http://localhost:8081/person"}
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
{"id":1,"name":"小米","message":"http://localhost:8080/person"}
{"id":1,"name":"小米","message":"http://localhost:8081/person"}