All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.df.restypass.base.DefaultRestyPassFactory Maven / Gradle / Ivy

The newest version!
package com.github.df.restypass.base;

import com.github.df.restypass.command.RestyCommandContext;
import com.github.df.restypass.executor.CommandExecutor;
import com.github.df.restypass.executor.FallbackExecutor;
import com.github.df.restypass.executor.RestyCommandExecutor;
import com.github.df.restypass.executor.RestyFallbackExecutor;
import com.github.df.restypass.filter.CommandFilter;
import com.github.df.restypass.filter.TrafficLimitFilter;
import com.github.df.restypass.lb.LoadBalancer;
import com.github.df.restypass.lb.server.CloudConsulServerContext;
import com.github.df.restypass.lb.server.CloudDiscoveryServerContext;
import com.github.df.restypass.lb.server.ConfigurableServerContext;
import com.github.df.restypass.lb.server.ServerContext;
import com.github.df.restypass.util.ClassTools;

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

/**
 * 默认工厂类
 * Created by darrenfu on 17-7-27.
 */
public class DefaultRestyPassFactory implements RestyPassFactory {
    public static RestyPassFactory INSTANCE = new DefaultRestyPassFactory();

    @Override
    public RestyCommandContext getRestyCommandContext() {
        return RestyCommandContext.getInstance();
    }

    @Override
    public ServerContext getServerContext() {
        // com.ecwid.consul.v1.ConsulClient org.springframework.cloud.consul.discovery.ConsulDiscoveryClient
        //org.apache.curator.x.discovery.ServiceDiscovery   org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryClient
        // com.netflix.discovery.EurekaClient  org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient
        boolean isUseCloudConsul = ClassTools.hasClass("org.springframework.cloud.consul.discovery.ConsulDiscoveryClient");
        boolean isUseCloudZookeeper = ClassTools.hasClass("org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryClient");
        boolean isUseCloudEureka = ClassTools.hasClass("org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient");
        if (isUseCloudConsul) {
            return new CloudConsulServerContext();
        }
        if (isUseCloudZookeeper) {
            return new CloudDiscoveryServerContext();
        }
        if (isUseCloudEureka) {
            return new CloudDiscoveryServerContext();
        } else {
            return new ConfigurableServerContext();
        }
    }

    @Override
    public CommandExecutor getCommandExecutor() {
        return new RestyCommandExecutor(getRestyCommandContext());
    }

    @Override
    public FallbackExecutor getFallbackExecutor() {
        return new RestyFallbackExecutor();
    }

    @Override
    public List getCommandFilter() {
        List commandFilterList = new ArrayList<>();
        commandFilterList.add(new TrafficLimitFilter());
        return commandFilterList;
    }


    public static  T getDefaultBean(Class clz) {
        if (clz.equals(ServerContext.class)) {
            return (T) INSTANCE.getServerContext();
        } else if (clz.equals(CommandExecutor.class)) {
            return (T) INSTANCE.getCommandExecutor();
        } else if (clz.equals(LoadBalancer.class)) {
            // loadbalancer使用注解单独配置
//            return (T) INSTANCE.getDefaultLoadBalancer();
        } else if (clz.equals(FallbackExecutor.class)) {
            return (T) INSTANCE.getFallbackExecutor();
        }

        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy