Fork me on GitHub

SpringCloud 进阶之Ribbon和Feign(负载均衡)

1. Ribbon 负载均衡

  • Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端,负载均衡的工具;

1.1 Ribbon 配置初步

1.1.1 修改 microservicecloud-consumer-dept-80

// pom.xml
<!-- Ribbon相关 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-config</artifactId>
</dependency>


// 修改application.yml,追加eureka的服务注册地址
eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/


// ConfigBean 添加新注解 @LoadBalanced, 用于加入 Ribbon 配置
@Configuration
public class ConfigBean {
	@Bean
	@LoadBalanced
	public RestTemplate getRestTemplate() {
		return new RestTemplate();
	}
}


// 主启动类 DeptConsumer80_App添加 @EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient
public class DeptConsumer80_App {

	public static void main(String[] args) {

		SpringApplication.run(DeptConsumer80_App.class, args);
	}

}


// 修改 DeptController_Consumer 客户端访问类
private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";


// 测试访问:
// http://localhost:8082/consumer/dept/get/1
// http://localhost:8082/consumer/dept/list
// http://localhsot:8082/consumer/dept/add?dname=广告部

1.2 Ribbon 负载均衡

// 新建microservicecloud-provider-dept-8002
// 新建microservicecloud-provider-dept-8003

// 新建8002/8003数据库

// 修改8002/8003各自YML

1.3 Ribbon 核心组件IRule

  • 根据特定算法,从服务列表中选取一个要访问的服务;
    • RoundRobinRule:轮询
    • RandomRule:随机
    • AvailabilityFilteringRule: 会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,以及并发的连接数量
      超过阈值的服务,然后对剩余的服务列表按照轮询策略进行访问;
    • WeightedResponseTimeRule: 根据平均响应时间计算所有服务的权重,响应时间越快,服务权重越大,被选中的机率越高;
      刚启动时,如果统计信息不足,则使用RoundRobinRule策略,等统计信息足够时,会切换到WeightedResponseTimeRule
    • RetryRule: 先按照RoundRobinRule的策略获取服务,如果获取服务失败,则在指定时间内会进行重试,获取可用的服务;
    • BestAvailableRule: 会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务;
    • ZoneAvoidanceRule: 默认规则,复合判断server所在区域的性能和server的可用性选择服务器;
// 修改 microservicecloud-consumer-dept-80
// ConfigBean
@Configuration
public class ConfigBean {

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

	@Bean
	public IRule myRule() {
		return new RoundRobinRule(); // 显式的指定使用轮询算法
	}
}

1.4 自定义Ribbon的负载均衡策略

// 修改主启动类
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name="MICROSERVICECLOUD-DEPT", configuration=MySelfRule.class)  // 自定义Ribbon配置类
public class DeptConsumer80_App {

	public static void main(String[] args) {

		SpringApplication.run(DeptConsumer80_App.class, args);
	}

}


// com.noodles.myrule
// 自定义Robbin规则类
@Configuration
public class MySelfRule{
    @Bean
    public IRule myRule(){
        return new RandomRule(); //自定义均衡策略
    }
}

2. Feign 负载均衡

  • Feign 是一个声明式WebService客户端:
    • 使用方法:定义一个接口,然后在上面添加注解;

2.1 创建microservicecloud-consumer-dept-feign

// 参考 microservicecloud-consumer-dept-80

// pom.xml
<!-- Feign相关 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>


// 修改 microservicecloud-api 工程
    // pom.xml
    <!-- Feign相关 -->
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>


    // 新建DeptClientService接口,并新增注解@FeignClient
    @FeignClient(value="MICROSERVICECLOUD-DEPT")
    public interface DeptClientService {

    	@RequestMapping(value="/dept/get/{id}", method= RequestMethod.GET)
    	public Dept get(@PathVariable("id") long id);

    	@RequestMapping(value="/dept/list", method= RequestMethod.GET)
    	public List<Dept> list();

    	@RequestMapping(value="/dept/add", method= RequestMethod.POST)
    	public boolean add(Dept dept);
    }

    // mvn clean
    // mvn install


// microservice-consumer-dept-feign 工程修改Controller
@RestController
public class DeptController_Consumer {

	@Autowired
	private DeptClientService service;

	@RequestMapping(value="/consumer/dept/get/{id}")
	public Dept get(@PathVariable("id") Long id) {
		return this.service.get(id);
	}

	@RequestMapping(value="/consumer/dept/list")
	public List<Dept> list(){
		return this.service.list();
	}

	@RequestMapping(value="/consumer/dept/add")
	public Object add(Dept dept) {
		return this.service.add(dept);
	}
}


// 修改主启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.noodles.springcloud"})
@ComponentScan("com.noodles.springcloud")
public class DeptConsumer80_Feign_App {

	public static void main(String[] args) {

		SpringApplication.run(DeptConsumer80_Feign_App.class, args);
	}

}

参考资料:

posted @ 2018-06-16 19:18  小a的软件思考  阅读(29455)  评论(0编辑  收藏  举报