You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "beiwei30 (GitHub)" <gi...@apache.org> on 2018/10/29 09:53:35 UTC

[GitHub] [incubator-dubbo] beiwei30 commented on issue #2664: dubbo hystrix熔断集成问题,达不到熔断或降级的效果

1. consumer

```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <dubbo:application name="consumer"/>
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="echoService" interface="com.test.dubbo.service.EchoService" check="false" retries="0">
    </dubbo:reference>	
</beans>
```

2. provider

```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="provider"/>
    <!-- 和本地bean一样实现服务 -->
    <bean id="echoService" class="com.test.dubbo.service.EchoServiceImpl"/>
    
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.test.dubbo.service.EchoService" ref="echoService" retries="0" >
	</dubbo:service>
</beans>
```


3. pom.xml

```xml
 <!-- hystrix -->
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-core</artifactId>
            <version>1.5.9</version>
        </dependency>

 <dependency>
				<groupId>com.alibaba</groupId>
				<artifactId>dubbo</artifactId>
				<version>2.5.3</version>
		</dependency>

<dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.24</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.24</version>
            <scope>test</scope>
        </dependency>

<dependency>
		    <groupId>org.javassist</groupId>
		    <artifactId>javassist</artifactId>
		    <version>3.21.0-GA</version>
		</dependency>

		<dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.6.10.Final</version>
        </dependency>

<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
```

4. User

```java
package com.test.dubbo.service;

import java.io.Serializable;

public class User implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int id;
	private String username;
	private String password;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
```

5. EchoService

```java
package com.test.dubbo.service;

public interface EchoService {

	String sayHello();

	String echoWithTimeOut();

	String echoWithException();

	String sayHi();

	User getUser(int id);
}
```

6. EchoServiceImpl

```java
package com.test.dubbo.service;

import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

public class EchoServiceImpl implements EchoService {
	public String sayHello() {
		System.out.println("test");
		return "Hello World";
	}

	public String echoWithTimeOut() {
		try {
			for (int i = 1; i < 10; i++) {
				System.out.println("i==" + i);
				TimeUnit.SECONDS.sleep(i);
			}
			// Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("11111111...echoWithTimeOut");
		return "Hello World";
	}

	public String echoWithException() {
		System.out.println(String.format("%s", 10 / 0));
		System.out.println("2222222222.......echoWithException");
		return String.format("%s", 10 / 0);
	}

	public String sayHi() {
		String str = "Hi........DemoFallback!";
		System.out.println("-----------------" + str);
		return str;
	}

	public User getUser(int id) {
		System.out.println("getUser");
		System.out.println(String.format("%s", 10 / 0));
		User user=new User();
		user.setId(1);
		user.setUsername("test");
		user.setPassword("test");
		System.out.println("11111111562");
		return user;
	}
}
```

7. ConsumerTest

```java
package com.test.dubbo;

import com.test.dubbo.service.EchoService;
import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.concurrent.TimeUnit;

public class ConsumerTest {
	EchoService echoService;

	@Before
	public void before() {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "consumer.xml" });
		context.start();
		echoService = (EchoService) context.getBean("echoService", EchoService.class);
	}

	@Test
	public void test() {
		Assert.assertEquals("Hello World", echoService.sayHello());
		// System.out.println(echoService.sayHello());
	}

	@Test
	public void testTimeout() {
		// Assert.assertEquals("Hello World", echoService.echoWithTimeOut());
		System.out.println(echoService.echoWithTimeOut());
		System.out.println("555555555555555");
	}


	@Test
	public void testEchoException() {
		//echoService.echoWithException();
	//	try {
			echoService.getUser(1);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
		System.out.println("666666666666");
	}

	@Test
	public void testThreadPool() throws InterruptedException {
		for (int i = 0; i < 20; i++) {
			new Thread(() -> {
				echoService.sayHello();
			}).start();
			System.out.println("i==="+i);
			Thread.sleep(100);
		}
//		for (int i = 0; i < 5; i++) {
//            String res =echoService.sayHello();
//            System.out.println("test=" + i + ":" + res + " The time:" + (System.currentTimeMillis() / 1000));
//            Thread.sleep(200);
//        }
		
		System.out.println("test...");
	}
}
```

7. ProviderTest

```java
package com.test.dubbo;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderTest {

    @Test
    public void test() throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"provider.xml"});
        context.start();

        System.in.read(); // 按任意键退出
        context.close();

    }
}
```


[ Full content available at: https://github.com/apache/incubator-dubbo/issues/2664 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org