初步學習Hystrix

介紹:html

參考了http://www.tuicool.com/articles/AbiqEnn。比較詳細的有介紹  這裏只是演示一下demo。java

初步學習了一下Hystrix。在大中型分佈式系統中,一般系統不少依賴( HTTP ,hession,Netty,Dubbo)spring

在高併發訪問下,這些依賴的穩定性與否對系統的影響很是大,可是依賴有不少不可控問題:如網絡鏈接緩慢,資源繁忙,暫時不可用,服務脫機等.Hystrix就是用來解決這些問題的
這裏框架用的是http://www.cnblogs.com/luxiaoxun/p/4887452.html博客裏提供的基於zookeeper netty Protostuff的輕量級分佈式rpc框架。網絡

這裏的版本併發

<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
    <version>1.3.16</version>
</dependency>
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-metrics-event-stream</artifactId>
    <version>1.1.2</version>
</dependency>

提供服務的方面NettyRpcHuHaoSOA:app

1.首先咱們定義一個HelloPersonCommand 用來提供給服務端soa  服務先後調用的框架

package com.nettyrpc.test.Hystrix;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;

/**
 * Created by huhaosumail on 17/2/9.
 */
public class HelloPersonCommand extends HystrixCommand {

    private final String _name;
    public HelloPersonCommand(String  name) {
//        super(HystrixCommandGroupKey.Factory.asKey("HelloService"));
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HelloService")).andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(500)));
        //設置超市時間
        _name=new String(name);
    }

    @Override
    protected Object run() throws Exception {
        Thread.sleep(600);
        return String.format("Hello %s",_name);
    }

    @Override
    protected Object getFallback() {
        System.out.println("huhaoserver 超時");
        return "ss";
    }
}

2.首先咱們提供一個被Hystrix包裝的server的服務  例如HelloPersonServiceImpl的實現類分佈式

package com.nettyrpc.test.server;

import com.nettyrpc.server.RpcService;
import com.nettyrpc.test.Hystrix.HelloPersonCommand;
import com.nettyrpc.test.client.HelloPersonService;
import com.nettyrpc.test.client.Person;

import java.util.ArrayList;
import java.util.List;


@RpcService(HelloPersonService.class)
public class HelloPersonServiceImpl implements HelloPersonService {

    @Override
    public List<Person> GetTestPerson(String name, int num) {
        List<Person> persons = new ArrayList<>(num);
        for (int i = 0; i < num; ++i) {
            persons.add(new Person(Integer.toString(i), name));
        }
        //同步調用命令模式
        new HelloPersonCommand("ss").execute();
        return persons;
    }
}

客戶端訪問NettyRpcHuHaoSOA的HelloPersonService的服務時  ide

new HelloPersonCommand("ss").execute();

會去判斷這個訪問是否超過設定的超時時間  若是超時  調用 getfallBack方法。高併發

訪問的服務NettyRpcChenHaoSOA:

Test測試類

package com.nettyrpc.test.app;

import com.nettyrpc.client.RPCFuture;
import com.nettyrpc.client.RpcClient;
import com.nettyrpc.client.proxy.IAsyncObjectProxy;
import com.nettyrpc.test.client.HelloPersonService;
import com.nettyrpc.test.client.HelloService;
import com.nettyrpc.test.client.Person;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:client-spring.xml")
public class HelloServiceTest {

    @Autowired
    private RpcClient rpcClient;


    @Test
    public void helloPersonTest(){
        HelloPersonService helloPersonService = rpcClient.create(HelloPersonService.class);
        int num = 5;
        List<Person>  persons = helloPersonService.GetTestPerson("xiaoming",num);
        List<Person> expectedPersons = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            expectedPersons.add(new Person(Integer.toString(i), "xiaoming"));
        }
        assertThat(persons, equalTo(expectedPersons));

        for (int i = 0; i<persons.size(); ++i){
            System.out.println(persons.get(i));
        }
    }
}

模擬超時訪問服務端的被Hystrix包裝的GetTestPerson方法。

步驟以及效果:

1.開啓zksever

 

2.開啓服務端服務

3.客戶端模擬訪問 查看客戶端 服務端的控制檯結果

相關文章
相關標籤/搜索