package com.ywt.alipaympapi.core.configs;

import com.ywt.alipaympapi.core.redis.RedisCacheManager;
import com.ywt.alipaympapi.core.redis.ShardedJedisProxy;
import com.ywt.rpc.core.RestCaller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import redis.clients.jedis.BinaryJedisCommands;
import redis.clients.jedis.JedisCommands;

import java.lang.reflect.Proxy;

/**
 * Created by huangguoping.
 */
@Component
public class Beans {
    @Autowired
    RedisCacheManager redisCacheManager;

    @Value("${service.agent.rest.addr}")
    private String restServiceAgent;

    @Bean(name = "jedisCommandsClient")
    public JedisCommands getjedisCommands() {
        ShardedJedisProxy shardedJedisProxy = new ShardedJedisProxy(redisCacheManager);
        JedisCommands jedisProxyInstance = (JedisCommands) Proxy.newProxyInstance(shardedJedisProxy.getClass().getClassLoader(), new Class[]{JedisCommands.class, BinaryJedisCommands.class}, shardedJedisProxy);
        return jedisProxyInstance;
    }

    @Bean(name = "restCaller")
    public RestCaller restCaller(){
        return new RestCaller(restServiceAgent);
    }
}
