spring-cloud: eureka之:ribbon负载均衡自定义配置
Posted 穆晟铭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-cloud: eureka之:ribbon负载均衡自定义配置相关的知识,希望对你有一定的参考价值。
spring-cloud: eureka之:ribbon负载均衡自定义配置(二)
有默认配置的话基本上就是轮询接口,现在我们改用自定义配置,同时支持:轮询,随机接口读取
准备工作:
1.eureka服务
2.两个user服务: 项目名:spring-cloud-user接口:7900/7901
3.两个user服务:项目名:spring-cloud-user2接口:8800/8801
4.movie服务,读取/调用user信息接口
eureka启动文件加入@EnableEurekaServer注解
@EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
user服务:
spring-cloud-user配置文件:
#port server.port=7901/7900 #data spring.jpa.generate-ddl=false spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=none spring.datasource.platform=h2 spring.datasource.schema=classpath:schema.sql spring.datasource.data=classpath:data.sql spring.application.name=spring-cloud-user #log logging.level.root=INFO logging.level.org.org.hibernate=INFO logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE logging.level.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE logging.level.com.muyang=debug #eureka eureka.client.serviceurl.defaultzone=http://localhost:8761/eureka eureka.instance.preferIpAddress=true
spring-cloud-user启动文件加入@EnableEurekaClient注解
@EnableEurekaClient @SpringBootApplication public class BootUserApplication { public static void main(String[] args) { SpringApplication.run(BootUserApplication.class, args); } }
spring-cloud-user的接口文件代码
@RestController public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/simple/{id}") public User findById(@PathVariable Long id) { return userRepository.findOne(id); } }
spring-cloud-user2的配置同上,请注意相应的端口号的修改.
movie服务:
新建一个testConfiguration.java配置文件,跟启动文件目录同级
然后再新建一个@interface的ExcludeFromComponentScan注解文件,作为testConfiguration.java的componetscan注解
ExcludeFromComponentScan.java
public @interface ExcludeFromComponentScan { }
TestConfiguration.java配置文件
引入componetScan,和随机接口读取
@Configuration @ExcludeFromComponentScan public class TestConfiguration { //@Autowired //IClientConfig config; //随机 @Bean public IRule ribbonIRule() { return new RandomRule(); } }
Application.java启动文件
再启动文件加入,@EnableEurekaClient注解,
同时设置spring-cloud-user接口为随机读取:@RibbonClient(name = "spring-cloud-user", configuration = TestConfiguration.class)
排除不合适的组件类型:excludeFilters:指定不适合组件扫描的类型
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = ExcludeFromComponentScan.class) })@EnableEurekaClient
@EnableEurekaClient @SpringBootApplication //随机spring-boot-user @RibbonClient(name = "spring-boot-user", configuration = TestConfiguration.class) @ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = ExcludeFromComponentScan.class) }) //spring-boot-user2负载均衡 public class Application { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
movieController.java文档
以/test为例,spring-cloud-user为随机读取接口,spring-cloud-user2位轮询读取接口
@RestController public class MovieController { @Autowired private RestTemplate restTemplate; @Autowired LoadBalancerClient loadBalancerClient; @GetMapping("/movie/{id}") public User findById(@PathVariable Long id) { //http://localhost:7900/simple/ return restTemplate.getForObject("http://spring-boot-user/simple/" + id, User.class); } @GetMapping("/test") public String testUser() { //随机 ServiceInstance serviceInstance = this.loadBalancerClient.choose("spring-boot-user"); System.out.println("111:"+serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort()); //负载均衡 ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("spring-boot-user2"); System.out.println("222:" + serviceInstance2.getServiceId() + ":" + serviceInstance2.getHost() + ":" + serviceInstance2.getPort()); return "1"; } }
以上是关于spring-cloud: eureka之:ribbon负载均衡自定义配置的主要内容,如果未能解决你的问题,请参考以下文章