上一个例程我们配置如何在Feign中使用Hystrix,如果不使用Feign如何使用Hystrix,需要使用注解@HystrixCommand
参考下面例程:
注册中心例程
POM.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>eureka-server</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--
SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--
Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
比如最早的Release版本号为Angel,第二个Release版本为Brixton
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--
Eureka 服务依赖
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!--
SpringBoot集成Spring Security
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<!--
根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
mvn package spring-boot:repackage
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8110
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8110/eureka/
security.user.name=chenqx
security.user.password=123
启动类
package com.test.eureka_server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EureKaServer
{
public static void main( String[] args )
{
SpringApplication.run(EureKaServer.class, args);
}
}
服务器端例程
POM.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>eureka-service-order</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-service-order Maven Webapp</name>
<url>http://maven.apache.org</url>
<!--
SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<!--
Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
比如最早的Release版本号为Angel,第二个Release版本为Brixton
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--
根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
mvn package spring-boot:repackage
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=9100
spring.application.name=service-order
eureka.client.service-url.defaultZone=http://chenqx:123@localhost:8110/eureka
eureka.client.register-with-eureka=true
eureka.client.registry-fetch-interval-seconds=10
eureka.client.fetch-registry=true
服务实现类
package com.test.eureka_service_order;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Date;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderServiceApiImpl{
@RequestMapping(value = "/hello1", method = RequestMethod.GET)
public String hello(@RequestParam("name") String name) {
System.out.println("name="+name);
return "hello " + name;
}
@RequestMapping(value = "/hello2", method = RequestMethod.GET)
public OrderInfo hello(@RequestParam("name") String name,
@RequestParam("prdtName") String prdtName) {
System.out.println("hello2 name="+name+",prdtName="+prdtName);
try {
name= URLDecoder.decode(name,"UTF-8");
prdtName= URLDecoder.decode(prdtName,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return new OrderInfo(name, prdtName,1,1,null,null);
}
@RequestMapping(value = "/hello3", method = RequestMethod.GET)
public String hello(@RequestBody OrderInfo order) {
if (order == null) {
return "未知";
}
return order.toString();
}
}
实体类
package com.test.eureka_service_order;
import java.io.Serializable;
import java.sql.Date;
public class OrderInfo implements Serializable {
private static final long serialVersionUID = -7233238826463139634L;
private Long id;
private String name;
private String prdtName;
private Integer count;
private Integer amount;
private Date createDt = new Date(System.currentTimeMillis());
private String userId = null;
public OrderInfo() {
}
public OrderInfo(String name,String prdtName,Integer count,Integer amount,Date createDt,String userId) {
this.name = name;
this.prdtName = prdtName;
this.count = count;
this.amount = amount;
this.createDt = createDt;
this.userId = userId;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrdtName() {
return prdtName;
}
public void setPrdtName(String prdtName) {
this.prdtName = prdtName;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
public Date getCreateDt() {
return createDt;
}
public void setCreateDt(Date createDt) {
this.createDt = createDt;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
",prdtName=" + prdtName +
",count=" + count +
",amount=" + amount +
'}';
}
}
启动类
package com.test.eureka_service_order;
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;
@EnableDiscoveryClient
@SpringBootApplication
public class OrderServer
{
public static void main( String[] args )
{
SpringApplication.run(OrderServer.class, args);
}
}
客户端例程
POM.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--
SpringCloud是基于SpringBoot框架的,必须引入SpringBoot的依赖
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<groupId>com.test</groupId>
<artifactId>eureka-service-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-service-web</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
<!--
Spring Cloud的命名没有按照版本方式管理,是采用命名方式,
版本名称采用伦敦地铁站的名称命名,根据字母表的顺序对应版本时间顺序,
比如最早的Release版本号为Angel,第二个Release版本为Brixton
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!--
根据SpringCloud约定打成可以独立执行Jar包,使用Maven命令
mvn package spring-boot:repackage
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8887
spring.application.name=order-service-web
eureka.instance.hostname=localhost
eureka.instance.server.port=8110
eureka.client.serviceUrl.defaultZone=http://chenqx:123@${eureka.instance.hostname}:${eureka.instance.server.port}/eureka/
eureka.client.register-with-eureka=true
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.client.registry-fetch-interval-seconds=10
eureka.client.fetch-registry=true
feign.hystrix.enabled=true
Controller类
package com.test.eureka_service_web;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@RestController
public class OrderController {
@Autowired
@Qualifier(value="restTemplate")
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod="hellofallback")
@RequestMapping(value="/hello2",method=RequestMethod.GET)
public String hello6(String name,String prdtName)
{
String url = "http://SERVICE-ORDER/hello2?name="+name+"&prdtName="+prdtName;
return restTemplate.getForEntity(url, String.class).getBody();
}
public String hellofallback(String name,String prdtName)
{
return "FallBack invoke...name="+name+",prdtName="+prdtName;
}
}
启动类
package com.test.eureka_service_web;
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.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import feign.Logger;
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class OrderClient
{
@Bean
@LoadBalanced
RestTemplate restTemplate()
{
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(OrderClient.class, args);
}
}
测试效果
依次启动注册中心,服务端程序,客户端程序
在浏览器中输入地址http://localhost:8887/hello2?name=1&prdtName=Java
关闭服务端程序,再次刷新页面
添加Hystrix监控
在客户端程序的POM.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
重新启动客户端程序
通过浏览器输入http://localhost:8887/hystrix.stream
添加Dashboard依赖
在客户端程序的POM.xml中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
重新启动客户端程序
通过浏览器输入http://localhost:8887/hystrix
在输入框中输入http://localhost:8887/hystrix.stream