04、SpringCloud Ribbon

一、Client代码示例

说明:此处使用的SpringBoot版本为2.1.13.RELEASE,SpringCloud版本为Greenwich.SR5
为了演示负载均衡就需要有多个client,此处我们只需要将之前的client复制一份,稍作修改即可
1、maven依赖


        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

2、application.yml
client

server:
  port: 8001
spring:
  application:
    name: client
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: client-8001
  client:
    service-url:
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
  app:
    name: client-8001
  company:
    name: www.xxx.com
  build:
    artifactId: ${project.artifactId}
    version: ${project.version}

client2

server:
  port: 8002
spring:
  application:
    name: client
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: client-8002
  client:
    service-url:
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
  app:
    name: client-8002
  company:
    name: www.xxx.com
  build:
    artifactId: ${project.artifactId}
    version: ${project.version}

3、启动类
启动类是一样的,此处代码就只贴了一份

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

4、其他java类
Controller类

package org.example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name){
        return "hello,"+name;
    }
}
package example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name){
        return "hello2,"+name;
    }
}

两个服务的Controller类实际上应该是一样的,此处为了演示负载均衡,稍作修改一个为“return "hello,"+name;”,另一个为“return "hello2,"+name;”

二、代码示例

说明:此处使用的SpringBoot版本为2.1.13.RELEASE,SpringCloud版本为Greenwich.SR5
SpringCloud实现负载均衡比较简单 ,只需要在RestTemplate类上加上一个注解@LoadBalanced,并修改下RestTemplate调用方式,原始使用的是url直接调用,现在改为通过服务调用
1、maven依赖

    
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

2、application.yml

server:
  port: 9002
spring:
  application:
    name: ribbon
eureka:
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ribbon-9002
  client:
    service-url:
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
  app:
    name: ribbon-9002
  company:
    name: www.xxx.com
  build:
    artifactId: ${project.artifactId}
    version: ${project.version}

3、启动类

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class Ribbon9002Application {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

4、其他java类
Controller
调用地址格式:http://{服务}/hello/name

package org.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
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 HelloController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable String name){
        return restTemplate.getForObject("http://client/hello/"+name,String.class);
    }
}

2、测试验证

先后启动server、client、consumer,访问http://localhost:7001/

image.png

多次访问http://localhost:9002/hello/zs
交替出现
image.png

image.png

SpringCloud默认使用轮询策略进行负载均衡,如果想修改策略,只需要增加IRue注入到Spring容器

    @Bean
    IRule iRule(){
        return new RandomRule();//IRule实现类,此处为随机
    }

再次多次访问http://localhost:9002/hello/zs,不再试交替出现“hello,zs”和“hello2,zs”,而是随机出现
我们也可以自己定义负载均衡算法,大家可以参照:https://blog.csdn.net/www1056481167/article/details/81151064

github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
参考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
还有尚硅谷周阳老师的视频

你可能感兴趣的:(04、SpringCloud Ribbon)