RemoteService.java.ftl 4.2 KB
Newer Older
sq3536's avatar
sq3536 committed
1 2 3 4 5 6 7
<#ibiztemplate>
TARGET=PSSYSTEM
</#ibiztemplate>
package ${pub.getPKGCodeName()}.util.service;


import com.alibaba.fastjson.JSONObject;
sq3536's avatar
sq3536 committed
8
import com.fasterxml.jackson.databind.ObjectMapper;
sq3536's avatar
sq3536 committed
9 10
import feign.*;
import feign.codec.Decoder;
sq3536's avatar
sq3536 committed
11
import feign.codec.Encoder;
sq3536's avatar
sq3536 committed
12 13 14 15 16
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignContext;
import org.springframework.cloud.openfeign.support.SpringDecoder;
sq3536's avatar
sq3536 committed
17 18 19
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
sq3536's avatar
sq3536 committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 自定义feigen客户端配置
 *
 * @author
 */
@Service
public class RemoteService {

    /**
sq3536's avatar
sq3536 committed
36
     * FeignClientFactoryBean
sq3536's avatar
sq3536 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
     */
    @Autowired
    protected FeignContext feignContext;

    /**
     * FeignClient 默认LoadBalancerFeignClient
     */
    @Autowired
    private Client feignClient;


    private static final Map<String, Object> FEIGN_CLIENTS = new ConcurrentHashMap<>();

    /**
     * 定义远程通用接口
     */
    public interface RemoteFeignClient {
        @RequestMapping(method = RequestMethod.POST, value = "/{path}")
        JSONObject post( @PathVariable("path") String path, @RequestHeader("Authorization") String token,@RequestBody Map  param);

        @RequestMapping(method = RequestMethod.GET, value = "/{path}")
        JSONObject request( @PathVariable("path") String path, @RequestHeader("Authorization") String token,Map  param);

        @RequestMapping(method = RequestMethod.GET, value = "/{path}")
        JSONObject get( @PathVariable("path") String path, @RequestHeader("Authorization") String token);

        @RequestMapping(method = RequestMethod.PUT, value = "/{path}")
        JSONObject put( @PathVariable("path") String path, @RequestHeader("Authorization") String token,@RequestBody Map  param);

        @RequestMapping(method = RequestMethod.DELETE, value = "/{path}")
        JSONObject delete( @PathVariable("path") String path, @RequestHeader("Authorization") String token);
    }

    /**
     * @param serverId
     * @return
     */
    public RemoteFeignClient getClient(String serverId) {
        return this.create(RemoteFeignClient.class, serverId);
    }

    /**
     * 设置编码解码器为FastJson
     *
     * @param clazz
     * @param serviceId
     * @param <T>
     * @return
     */
sq3536's avatar
sq3536 committed
86
    private synchronized <T> T create(Class<T> clazz, String serviceId) {
sq3536's avatar
sq3536 committed
87 88 89 90 91
        Object object = FEIGN_CLIENTS.get(serviceId);
        if (Objects.isNull(object)) {
            object = Feign.builder()
                    //decoder指定对象解码方式
                    .decoder(this.feignDecoder())
sq3536's avatar
sq3536 committed
92
                    .encoder(this.feignEncoder())
sq3536's avatar
sq3536 committed
93 94
                    .client(feignClient)
                    //options方法指定连接超时时长及响应超时时长
sq3536's avatar
sq3536 committed
95
                    .options(new Request.Options(5000, 50000))
sq3536's avatar
sq3536 committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                    //retryer方法指定重试策略
                    //.retryer(new Retryer.Default(5000, 5000, 3))
                    .contract(feignContext.getInstance(serviceId, Contract.class))
                    //target方法绑定接口与服务端地址。返回类型为绑定的接口类型。
                    .target(clazz, "http://"+serviceId);
            FEIGN_CLIENTS.put(serviceId, object);
        }

        return (T) object;
    }

    private Decoder feignDecoder() {
        return new SpringDecoder(feignHttpMessageConverter());
    }

sq3536's avatar
sq3536 committed
111 112
    private Encoder feignEncoder() {
        return new SpringEncoder(feignHttpMessageConverter());
sq3536's avatar
sq3536 committed
113 114 115
    }


sq3536's avatar
sq3536 committed
116 117 118 119
    private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
        HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(new ObjectMapper());
        final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(jsonConverter);
        return () -> httpMessageConverters;
sq3536's avatar
sq3536 committed
120 121
    }

sq3536's avatar
sq3536 committed
122

sq3536's avatar
sq3536 committed
123
}