这一章节讲如何使用ribbon和hystrix.
在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写Group和Artifact等信息,
这里Artifact填写ribbonconsumer, 再次next,
这里选择如下图:
最后在Module Name中填写ribbon-consumer.
生成的pom.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.xum</groupId> <artifactId>ribbon-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ribbon-consumer</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </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-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> </project>
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8771
spring:
application:
name: ribbon-consumer
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
在RibbonConsumerApplication.java里的内容如下, 要加上@EnableCircuitBreaker @EnableHystrixDashboard @EnableDiscoveryClient这三个注解.
package com.xum.ribbonconsumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableCircuitBreaker @EnableHystrixDashboard @EnableDiscoveryClient @SpringBootApplication public class RibbonConsumerApplication { @Bean @LoadBalanced // 这里是加载平衡, 我这里使用RestTemplate RestTemplate restTemplate () { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(RibbonConsumerApplication.class, args); } }
然后在RibbonConsumerApplication.java文件夹同级目录下建立Controller文件夹, 里面写一个RibbonConsumerController.java, 内容如下:
package com.xum.ribbonconsumer.controller; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping(value = "/ribbonconsumer") public class RibbonConsumerController { private static final Logger LOG = LoggerFactory.getLogger(RibbonConsumerController.class); @Autowired RestTemplate restTemplate; @RequestMapping(value = "/test") @HystrixCommand(fallbackMethod = "testConsumer_ribbon") // 这里是HystrixCommand注解, 如果eureka-client项目没有启动, 那么就会转到本项目的fallbackMethod方法,即testConsumer_ribbon(), 就是下面的方法 public String testConsumer() { // 这里是通过getForEntity属性来调用eureka-client里的testone/test这条api LOG.info("RibbonConsumerController=>testConsumer"); return restTemplate.getForEntity("http://eureka-client/testone/test",String.class).getBody(); }
// 这里就是熔断器(Hystrix)自我保护的方法,其实是自定义的方法. public String testConsumer_ribbon() { LOG.info("RibbonConsumerController=>testConsumer_ribbon"); return "testConsumer_ribbon"; } }
下面是eureka-client项目下testone/test的api返回内容, 这里是简写了,具体的内容可以回看前面eureka-client项目搭建.
@RequestMapping(value = "/test") public String test(@RequestParam(value = "name", required = false, defaultValue = "testOneClient")String name) { String info = "Hi " + name + ", this is EurekaClient, port is " + port; return info; }
最后顺序启动如下项目:
1. eureka-server
2. config-server
3. eureka-client
4. ribbon-consumer
最后用post man发送api: http://localhost:8771/ribbonconsumer/test
或则浏览器打开这条api.
可以看到返回的内容就好像调用eureka-client下的api: http://localhost:8762/testone/test
如果eureka-client项目里没有启动, 那么Hystrix会实现自我保护, 会调用到(fallbackMethod = "testConsumer_ribbon")方法, 自定义的方法.
测试一下, 把eureka-client项目停止,
在post man或则浏览器中输入http://localhost:8771/ribbonconsumer/test, 内容如下:
可以看到, 会调用到testConsumer_ribbon()的保护方法里去.