添加降级

This commit is contained in:
zhuyijun 2021-10-23 20:22:26 +08:00
parent 8e555a761f
commit 61bcba66b0
21 changed files with 142 additions and 17 deletions

View File

@ -37,6 +37,11 @@
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>

View File

@ -2,7 +2,9 @@ package cn.zyjblogs.dubbo.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@EnableHystrix
@SpringBootApplication
public class ConsumerApplication {

View File

@ -1,5 +1,6 @@
package cn.zyjblogs.dubbo.consumer.controller;
import cn.zyjblogs.dubbo.mail.mailapi.po.UserAddress;
import cn.zyjblogs.dubbo.mail.mailapi.service.OrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -8,6 +9,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/test")
@ -18,7 +20,8 @@ public class TestController {
private OrderService orderService;
@GetMapping("/get/{id}")
public void getAdderss(@PathVariable("id") String id){
orderService.initOrder(id);
@ResponseBody
public List<UserAddress> getAdderss(@PathVariable("id") String id){
return orderService.initOrder(id);
};
}

View File

@ -4,28 +4,48 @@ package cn.zyjblogs.dubbo.consumer.service.impl;
import cn.zyjblogs.dubbo.mail.mailapi.po.UserAddress;
import cn.zyjblogs.dubbo.mail.mailapi.service.OrderService;
import cn.zyjblogs.dubbo.mail.mailapi.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.List;
@DubboService
@DubboService(version = "1.0.0")
@Service
public class OrderServiceImpl implements OrderService {
/**
* loadbalance: 负载均衡算法 roundrobin轮询
*/
// @DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:2181")
@DubboReference(version = "1.0.0")
@DubboReference(version = "1.0.0", stub = "cn.zyjblogs.dubbo.mail.mailapi.service.impl.UserServiceStub", methods = {
@Method(name = "getUserAddressList", timeout = 3000)
}, loadbalance = "roundrobin")
// @DubboReference(version = "*",stub = "true")
private UserService userService;
@HystrixCommand(fallbackMethod = "fallback")
@Override
public void initOrder(String id) {
public List<UserAddress> initOrder(String id) {
long start = System.currentTimeMillis();
System.out.println("用户id: "+id);
System.out.println("用户id: " + id);
Assert.notNull(userService,"userService must not be null");
//查询用户的收获地址
List<UserAddress> userAddressList = userService.getUserAddressList(id);
userAddressList.forEach(userAddress -> {
System.out.println(userAddress.toString());
});
System.out.println("调用完成");
System.out.println("耗时:"+(System.currentTimeMillis() - start) +" 毫秒");
System.out.println("耗时:" + (System.currentTimeMillis() - start) + " 毫秒");
return userAddressList;
}
public List<UserAddress> fallback(String id) {
System.err.println("用户id" + id + " 进行降级,容错");
return new ArrayList<>();
}
}

View File

@ -26,6 +26,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>cn.zyjblogs.dubbo.mail</groupId>
<artifactId>mail-api</artifactId>

View File

@ -3,8 +3,9 @@ package cn.zyjblogs.dubbo.consumer2;
import cn.zyjblogs.dubbo.mail.mailapi.service.OrderService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@EnableHystrix
public class ConsumerApplication {
@SuppressWarnings("resource")

View File

@ -1,5 +1,6 @@
package cn.zyjblogs.dubbo.consumer2.controller;
import cn.zyjblogs.dubbo.mail.mailapi.po.UserAddress;
import cn.zyjblogs.dubbo.mail.mailapi.service.OrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -8,6 +9,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/test")
@ -18,7 +20,8 @@ public class TestController {
private OrderService orderService;
@GetMapping("/get/{id}")
public void getAdderss(@PathVariable("id") String id){
orderService.initOrder(id);
@ResponseBody
public List<UserAddress> getAdderss(@PathVariable("id") String id){
return orderService.initOrder(id);
};
}

View File

@ -15,7 +15,7 @@ public class OrderServiceImpl implements OrderService {
private UserService userService;
@Override
public void initOrder(String id) {
public List<UserAddress> initOrder(String id) {
long start = System.currentTimeMillis();
System.out.println("用户id: "+id);
//查询用户的收获地址
@ -25,5 +25,6 @@ public class OrderServiceImpl implements OrderService {
});
System.out.println("调用完成");
System.out.println("耗时:"+(System.currentTimeMillis() - start) +" 毫秒");
return userAddressList;
}
}

View File

@ -22,5 +22,7 @@
<dubbo:reference check="false" protocol="zookeeper" interface="cn.zyjblogs.dubbo.mail.mailapi.service.UserService"
id="userService"
version="1.0.0"/>
version="1.0.0" stub="cn.zyjblogs.dubbo.mail.mailapi.service.impl.UserServiceStub">
<dubbo:method name="getUserAddressList" timeout="3000"/>
</dubbo:reference>
</beans>

View File

@ -12,4 +12,14 @@
<version>0.0.1-SNAPSHOT</version>
<name>mail-api</name>
<description>mail-api</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -1,10 +1,14 @@
package cn.zyjblogs.dubbo.mail.mailapi.service;
import cn.zyjblogs.dubbo.mail.mailapi.po.UserAddress;
import java.util.List;
public interface OrderService {
/**
* 初始化订单
* @param user
*/
void initOrder(String user);
List<UserAddress> initOrder(String user);
}

View File

@ -0,0 +1,36 @@
package cn.zyjblogs.dubbo.mail.mailapi.service.impl;
import cn.zyjblogs.dubbo.mail.mailapi.po.UserAddress;
import cn.zyjblogs.dubbo.mail.mailapi.service.UserService;
import org.springframework.util.StringUtils;
import java.util.List;
/**
* 本地存根
*/
public class UserServiceStub implements UserService {
private final UserService userService;
/**
* 传入的是userService远程代理对象
* @param userService
*/
public UserServiceStub(UserService userService) {
this.userService = userService;
}
/**
* 本地存根
* @param userId
* @return
*/
@Override
public List<UserAddress> getUserAddressList(String userId) {
System.out.println("本地存根被调用");
if (!StringUtils.isEmpty(userId)){
return userService.getUserAddressList(userId);
}
return null;
}
}

View File

@ -32,6 +32,11 @@
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>

View File

@ -20,7 +20,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>cn.zyjblogs.dubbo.mail</groupId>
<artifactId>mail-api</artifactId>

View File

@ -2,7 +2,10 @@ package cn.zyjblogs.dubbo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@SpringBootApplication
@EnableHystrix
public class ProviderApplication {
public static void main(String[] args) {

View File

@ -3,19 +3,28 @@ package cn.zyjblogs.dubbo.provider.service.impl;
import cn.zyjblogs.dubbo.mail.mailapi.po.UserAddress;
import cn.zyjblogs.dubbo.mail.mailapi.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.DubboService;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@DubboService(version = "1.0.0")
//@DubboService
public class UserServiceImpl implements UserService {
@HystrixCommand
@Override
public List<UserAddress> getUserAddressList(String userId) {
System.out.println("provider被调用");
if (Math.random() > 0.5){
throw new RuntimeException("模拟异常");
}
List<UserAddress> list = new ArrayList<>();
list.add(new UserAddress(1,"A地址","1","张三","13456123451","Y"));
list.add(new UserAddress(2,"B地址","2","李四","13456123431","Y"));
System.out.println("provider被调用");
return list;
}
}

View File

@ -26,6 +26,13 @@
<artifactId>mail-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>

View File

@ -1,8 +1,9 @@
package cn.zyjblogs.dubbo.provider2;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@EnableHystrix
public class ProviderApplication {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {

View File

@ -3,12 +3,14 @@ package cn.zyjblogs.dubbo.provider2.service.impl;
import cn.zyjblogs.dubbo.mail.mailapi.po.UserAddress;
import cn.zyjblogs.dubbo.mail.mailapi.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.apache.dubbo.config.annotation.DubboService;
import java.util.ArrayList;
import java.util.List;
@DubboService
//@DubboService(version = "1.0.0")
public class UserServiceImpl implements UserService {
@HystrixCommand
@Override
public List<UserAddress> getUserAddressList(String userId) {
List<UserAddress> list = new ArrayList<>();

View File

@ -6,11 +6,13 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- <context:component-scan base-package="cn.zyjblogs.dubbo.provider2.service.impl"/>-->
<!-- 指定服务名称/引用名称-->
<dubbo:application name="provider" version="1.0.0"/>
<!-- 指定注册中心-->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"/>-->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" simplified="true" />
<dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
<!-- 通讯协议-->
<dubbo:protocol name="dubbo" port="20882"/>