springcloud学习-eureka服务消费者(Ribbon)
springcloud学习-eureka服务消费者(Ribbon)
接上篇博文,可以启动2个服务提供者,分别是端口号8010和8011,接下来开始创建服务消费者。
1 右键工程,进入“New Module”视图,如下:
2 选择jdk版本,一般情况默认自己的jdk即可。
3 点击“Next”,输入对应的工程名称(eurekaribbon),选择对应的工程构建方式,这里选择“Gradle Project”,如果gradle不是太熟悉,可以自行百度或者参考后续的博文。
4 点击“Next”,除了选择对应Cloud Discovery,并勾选Eureka Server 外,选择左侧的Cloud Routing,并勾选Ribbon
5 点击“Next”,然后Finish即可,这时idea已经帮助你完成一些基本的操作。展开对应的eurekaribbon module,找到resources目录,添加application.yml,工程文件目录:
application.yml配置如下:
server: port: 8180 sessionTimeout: 15 tomcat: max-threads: 800 uri-encoding: UTF-8 spring: application: name: serviceRibbon eureka: client: serviceUrl: defaultZone: http://root:[email protected]:8001/eureka/ EUREKASERVERPROVIDER: ribbon: NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
此处的配置说明:
spring.application.name为serviceRibbon;
对应的服务提供者的serviceId即EUREKASERVERPROVIDER,对应的ribbon的选择策略NFLoadBalancerRuleClassName为:
负载均衡策略主要如下:
AvailabilityFilteringRule:过滤掉因为一直连接失败的被标记为“电路跳闸”的后端server,且过滤掉高并发的的后端server(可连接数超过配置的阈值)
RandomRule:随机选择一个后端server
BestAvailabl:选择一个最小的并发请求的后端server,逐个考察server,如果server被标记为“电路跳闸”了,则忽略
RoundRobinRule:roundRobin方式轮询选择, 轮询index,选择index对应的后端server
WeightedResponseTimeRule:根据响应时间分配一个权重,响应时间越长,权重越小,被选中的可能性越低
RetryRule:对选定的负载均衡策略机上重试机制,在一个配置时间段内当选择server不成功,则一直尝试使用subRule的方式选择一个可用的server
这里我们选择RandomRule作为负载策略。
对应build配置文件如下:
比上两篇博文上的gradle文件多出此依赖。
6 对应的主程序类:
package com.example.eurekaribbon; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class EurekaribbonApplication { public static void main(String[] args) { SpringApplication.run(EurekaribbonApplication.class, args); } }
此处新增的是@EnableDiscoveryClient注释,表示对应的服务发现。
RestTemplateConfig类
package com.example.eurekaribbon.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } }
HelloService类
package com.example.eurekaribbon.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class HelloService { @Autowired RestTemplate restTemplate; @Autowired LoadBalancerClient loadBalancerClient; public String sayHello(String name){ ServiceInstance serviceInstance = loadBalancerClient.choose("EUREKASERVERPROVIDER"); System.out.println(serviceInstance.getHost() + ":" + serviceInstance.getPort()+"---------------------------"); return restTemplate.getForObject("http://EUREKASERVERPROVIDER/name/"+name,String.class); } }
HelloController类
package com.example.eurekaribbon.web; import com.example.eurekaribbon.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired HelloService helloService; @RequestMapping("/{who}") public String sayHello(@PathVariable String who){ return helloService.sayHello(who); } }
7 启动主程序类,此时看到对应的注册中心如下:
8 验证,在浏览器输入:http://localhost:8010/test,查看后台日志:
多次刷新可以查看更多日志。
以上是个人学习过程,如有错误,欢迎指正,谢谢大家!