springcloud demo---ribbon
1.概念
Spring Cloud Ribbon 是一个基于 Http 和 TCP 的客服端负载均衡工具,它是基于 Netflix Ribbon 实现的。客户端负载均衡即是当浏览器向后台发出请求的时候,客户端会向 Eureka Server读取注册到服务器的可用服务信息列表,然后根据设定的负载均衡策略(没有设置即用默认的,轮询 RoundRobinRule、随机 RandomRule策略),抉择出向哪台服务器发送请求。
2.ribbon案例
2.1 ribbon-client(和eureka-client类似)
server: port: 8764 spring: application: name: ribbon-provider eureka: instance: lease-renewal-interval-in-seconds: 5 lease-expiration-duration-in-seconds: 15 client: healthcheck: enabled: true service-url: defaultZone: http://admin:123@localhost:8761/eureka/ management: endpoints: web: exposure: include: '*' endpoint: health: show-details: ALWAYS
2.2 依赖
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> <version>2.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- 修改后立即生效,热部署相关 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
3.启动类
package com.transsion; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.RandomRule; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker//熔断的注解 public class RibbonProviderApplication { //同时通过 @LoadBalanced 开启 restTemplate 负载均衡功能。 @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(RibbonProviderApplication.class, args); } /*@Bean public ServletRegistrationBean getServlet() { HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }*/ }
4.CallService和CallController
创建 Service,通过 restTemplate 消费 Client 服务的接口return restTemplate.getForObject("http://PROVIDER/test", String.class);
其中:PROVIDER为已经注册的client的applicationName,test为该client的接口
package com.transsion; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.ribbon.proxy.annotation.Hystrix; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class CallService { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "helloFallback") public String port(){ return restTemplate.getForObject("http://PROVIDER/test", String.class); }
/*熔断相关,后续会进行讲解*/ public String helloFallback() { return "error"; } }
package com.transsion; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController //@RequestMapping("/call") public class CallController { @Autowired CallService callService; @RequestMapping("test") public String port() { return callService.port(); } }
5.访问地址
首先开启eureke-server,接着启动两个client,访问地址http://localhost:8764/test,页面内容会在根据两个client中的test方法切换。当其中一个client停止是,会发生熔断,显示error。而另一个client则显示正常的内容。