Feign动态请求的方式比较好用的有两种,一种是通过注解+URI类实现,另一种是通过builder构建Api的方式实现。
注解 + URI
1.定义接口路径
FeignClient注解中的url是默认路径,实际上调用已URI中的url为准。
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.net.URI;
@FeignClient(name = "system", url = "http://${server.haproxy.ip}:${server.haproxy.port}")
public interface VbngSystemApi {
@GetMapping("/api/v2/system")
Object getSystem(URI uri);
@GetMapping("/api/v2/aaasss")
Object getAaa(URI uri);
}
2.调用接口
我们只需要设置好URI就会请求我们想要的接口啦~
/**
* @ClassName SystemInfoService
* @Author Lijw
* @Date 2021/3/8 10:35
* @Description
**/
@Slf4j
@Service
public class SystemInfoService<T> {
@Autowired
VbngSystemApi vbngSystemApi;
public static URI getUri(String ip, Integer port) {
try {
return new URI("https", null, ip, port, null, null, null);
} catch (URISyntaxException e) {
String msg = String.format("URI is wrong!,ip:[%s],port:[%s],e:[%s]", ip, port, e);
log.error(msg);
throw new AemsException(msg);
}
}
public void get() {
URI uri = getUri("127.0.0.1", 8080);
Object system = vbngSystemApi.getSystem(uri);
}
}
第二种
1.定义接口
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient;
import java.util.HashMap;
/**
* @ClassName VbngPerformanceApi
* @Author Lijw
* @Date 2021/2/20 16:06
* @Description
**/
@FeignClient(name = "vBNG")
public interface VbngPerformanceApi {
@RequestLine("GET /api/v2/cpuMemory")
JSONObject cpuMemory();
@RequestLine("GET /api/v2/system")
HashMap system();
}
2.调用接口
skipSSLClient客户端是自己实现的跳过SSL认证用于请求Https接口的类。(如果不需要使用https则可以构建世不设置client)
FeignClientsConfiguration需要导入其中包含编码器,解码器等一系列默认参数。
因为可能会频繁的创建Feign客户端所以用guava的cache做了一个缓存,不需要也可以不用。
/**
* @ClassName PerformanceDataHandle
* @Author Lijw
* @Date 2021/2/20 15:59
* @Description
**/
@Slf4j
@Component
@Import(FeignClientsConfiguration.class)
public class PerformanceDataHandle {
@Autowired
Decoder decoder;
@Autowired
Encoder encoder;
@Autowired
Client skipSSLClient;
@Autowired
PerformanceServiceApi performanceServiceApi;
public final static Cache<String, VbngPerformanceApi> FEIGN_CACHE_MAP = CacheBuilder.newBuilder()
.maximumSize(100000)
.expireAfterWrite(3, TimeUnit.HOURS)
.build();
public VbngPerformanceApi getFeign(String url) {
return Optional.ofNullable(FEIGN_CACHE_MAP.getIfPresent(url))
.orElseGet(() -> {
var api = Feign.builder()
.client(skipSSLClient)
.encoder(encoder).decoder(decoder)
.options(new Request.Options(60000, 60000))
.retryer(new Retryer.Default(5000, 5000, 3))
.logLevel(Logger.Level.BASIC)
.target(VbngPerformanceApi.class, url);
FEIGN_CACHE_MAP.put(url, api);
return api;
});
}
public static String getUrl(String ip, Integer port) {
Assert.isTrue(IpV4Util.ipV4Validate(ip), String.format("IP address not conforming to specification! ip:[%s]", ip));
return "https://" + ip + ":" + port;
}
public void get() {
String url = getUrl("127.0.0.1", 8080);
VbngPerformanceApi feign = getFeign(url);
HashMap system = feign.system();
}
}
代码有待改进,欢迎提出不足🤡