修改Ribbon负载均衡规则为随机。
一、创建Ribbon的配置类,该类不应该在主应用程序上下文的@CompontScan所扫描的包中。
package com.vainglory.ribbonConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
/**
* 该类为Ribbon的配置类
* 注意:该类不应该在主应用程序上下文的@ComponentScan 中。
* @author 周立
*/
@Configuration
public class RibbonConfiguration {
@Bean
public IRule ribbonRule() {
// 负载均衡规则,改为随机
return new RandomRule();
}
}
二、创建Ribbon客户端类
@RibbonClient的name属性指定自定义的负载均衡客户端的名称,configuration属性指定为负载均衡客户端进行自定义的配置类
package com.vainglory.stySpringCloud.config;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Configuration;
import com.vainglory.ribbonConfig.RibbonConfiguration;
/**
* 使用RibbonClient,为特定name的Ribbon Client自定义配置.
* 使用@RibbonClient的configuration属性,指定Ribbon的配置类.
* 可参考的示例:
* http://spring.io/guides/gs/client-side-load-balancing/
* @author 周立
*/
@Configuration
@RibbonClient(name = "microservice-provider-user", configuration = RibbonConfiguration.class)
public class TestConfiguration {
}
三、控制器类
package com.vainglory.stySpringCloud.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
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;
import com.vainglory.stySpringCloud.entity.User;
@RestController
public class MovieController {
private static final Logger LOGGER = LoggerFactory.getLogger(MovieController.class);
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject("http://microservice-provider-user/" + id, User.class);
}
@GetMapping("/log-user-instance")
public void logUserInstance() {
ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-provider-user");
// 打印当前选择的是哪个节点
MovieController.LOGGER.info("{}:{}:{}", serviceInstance.getServiceId(), serviceInstance.getHost(), serviceInstance.getPort());
}
}
四、Entity Bean
package com.vainglory.stySpringCloud.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Integer age;
private BigDecimal balance;
//省略getter和setter
}
五、启动器
package com.vainglory.stySpringCloud;
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 MicroserviceConsumerMovieRibbonCustomizingApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerMovieRibbonCustomizingApplication.class, args);
}
}