RestyPass
高性能的Restful服務(wù)調(diào)用客戶端庫(kù)疑枯,完全兼容Spring MVC 注解,基于接口和注解自動(dòng)代理客戶端HTTP請(qǐng)求昏鹃,支持服務(wù)發(fā)現(xiàn),負(fù)載均衡,自動(dòng)熔斷凹炸,降級(jí),重試昼弟,限流啤它。覆蓋Feign + Hystrix + Ribbon + ApacheHttpClient的功能
HOME
可與spring cloud/spring boot配套使用,幫助微服務(wù)架構(gòu)更容易落地,解決服務(wù)間最后一公里的調(diào)用問(wèn)題舱痘。
Github地址: https://github.com/darren-fu/RestyPass
碼云地址: https://gitee.com/darren-fu/RestyPass
對(duì)比SpringCloud技術(shù)棧:Feign+Hystrix+Ribbon+ApacheHttpClient
- Http連接池性能提升变骡,RestyPass采用基于Netty實(shí)現(xiàn)的AsyncHttpClient連接池,性能測(cè)試比ApacheHttpClient高30%芭逝。
- 減少對(duì)象生成塌碌,F(xiàn)eign+Hystrix+Ribbon+ApacheHttpClient,多個(gè)庫(kù)組合完成一個(gè)完整的http客戶端調(diào)用請(qǐng)求铝耻,一個(gè)調(diào)用鏈中創(chuàng)建很多多余對(duì)象誊爹。
- 減少線程切換蹬刷,如Hystrix,ApacheHttpClient中都有自己的線程池频丘,一個(gè)請(qǐng)求的完成往往要經(jīng)過(guò)多次的線程切換办成,損耗性能。
- 更易配置搂漠,RestyPass使用注解的方式配置各個(gè)接口請(qǐng)求;而使用Feign+Hystrix+Ribbon+ApacheHttpClient迂卢,則面臨每個(gè)庫(kù)都有自己的配置項(xiàng),配置繁多而且容易發(fā)生沖突桐汤,親身實(shí)踐而克,想把這一套配置好是一件不容易的事情。
- 實(shí)時(shí)更新配置怔毛,RestyPass支持實(shí)時(shí)更新部分配置员萍,比如實(shí)時(shí)關(guān)閉/啟用降級(jí)服務(wù),實(shí)時(shí)熔斷/恢復(fù)服務(wù)拣度,且粒度可以精確到接口級(jí)碎绎。
- 易開發(fā),可自由開發(fā)大部分核心接口的自定義實(shí)現(xiàn)抗果,并直接注入即可啟用(Spring容器)筋帖。
- 支持過(guò)濾器,并可以自定義過(guò)濾器并注入
- 支持限流
示例(demo[調(diào)用方]+demo-serverside[服務(wù)端])
客戶端代碼
- 啟用spring cloud 服務(wù)發(fā)現(xiàn)則RestyPass自動(dòng)使用spring的服務(wù)發(fā)現(xiàn)方式冤馏,
- 否則默認(rèn)讀取resty-server.yaml來(lái)獲取服務(wù)實(shí)例
- 可自定義其它發(fā)現(xiàn)服務(wù)的方式日麸,實(shí)現(xiàn)ServerContext接口并注入即可
// 使用@EnableRestyPass注解啟用RestyPass
@SpringBootApplication
@EnableRestyPass(basePackages = {"com.github.df"})
@RestController
//@EnableDiscoveryClient
public class TestClientApplication {
public static void main(String[] args) {
SpringApplication.run(TestClientApplication.class, args);
}
@Autowired
private ProxyService proxyService;
@RequestMapping(value = "nothing")
public String callNothing() {
proxyService.getNothing();
return "OK";
}
}
客戶端服務(wù)
//使用接口和注解定義并配置調(diào)用客戶端
//RestyService注解定義服務(wù)
@RestyService(serviceName = "server",
fallbackEnabled = true,
fallbackClass = ProxyServiceImpl.class,
forceBreakEnabled = false,
circuitBreakEnabled = false,
loadBalancer = RandomLoadBalancer.NAME,
retry = 1,
requestTimeout = 10000,
limit = 1000 //限流
)
@RequestMapping(value = "/resty")
public interface ProxyService extends ApplicationService {
// RestyMethod注解定義服務(wù)接口
// 同步調(diào)用
@RestyMethod(retry = 2,
fallbackEnabled = "false",
circuitBreakEnabled = "false",
forceBreakEnabled = "false",
limit = 10)
@RequestMapping(value = "/get_nothing", method = RequestMethod.GET, headers = "Client=RestyProxy", params = "Param1=val1")
void getNothing();
//支持spring mvc注解
@RestyMethod()
@RequestMapping(value = "/get_age/{name}", method = RequestMethod.GET)
Response<String> getAge(@RequestParam("id") Long id, @PathVariable(value = "name") String name, @RequestHeader(value="TOKEN") String token);
// 異步調(diào)用形式: Future<?> 參數(shù)類型,出參
@RestyMethod
@RequestMapping(value = "/get_status", method = RequestMethod.GET)
String getStatus(RestyFuture<String> future);
// 異步調(diào)用形式: Future<?> 返回類型
@RestyMethod
@RequestMapping(value = "/get_user", method = RequestMethod.GET)
Future<User> getUser();
}
服務(wù)實(shí)例定義
- 支持SC自動(dòng)服務(wù)發(fā)現(xiàn)逮光,yaml配置等多種方式代箭;
- 可實(shí)現(xiàn)接口ServerContext自定義服務(wù)發(fā)現(xiàn)機(jī)制(如多注冊(cè)中心)
# resty-server.yaml
servers:
- serviceName: server
instances:
- host: localhost
port: 9201
- host: localhost
port: 9202
服務(wù)端代碼
@RestController
@RequestMapping("/resty")
public class TestController {
@RequestMapping(value = "/get_nothing", method = RequestMethod.GET)
public void nothing() {
System.out.println("############nothing");
}
}
核心接口
- RestyCommand: 包含單次所有信息。
- RestyCommandExecutor: command執(zhí)行器涕刚,返回執(zhí)行結(jié)果梢卸。
- ServerContext: 服務(wù)實(shí)例容器,負(fù)責(zé)服務(wù)發(fā)現(xiàn)和更新副女。
- LoadBalancer: 負(fù)載均衡器,實(shí)現(xiàn)接口或者繼承抽象類可以輕易的實(shí)現(xiàn)想要的LB蚣旱,并且很容易在RestyService中配置并使用
- CommandFilter: command過(guò)濾器
- FallbackExecutor: 降級(jí)服務(wù)類執(zhí)行器碑幅。
配置與注入
注入自己的實(shí)現(xiàn)類,實(shí)現(xiàn)特殊需求塞绿。
@Configuration
public class RestyPassConfig {
@Bean
public FallbackExecutor fallbackExecutor() {
return new RestyFallbackExecutor();
}
@Bean
public ServerContext serverContext() {
return new ConfigurableServerContext();
}
@SuppressWarnings("SpringJavaAutowiringInspection")
@Bean
public CommandExecutor commandExecutor(RestyCommandContext commandContext) {
return new RestyCommandExecutor(commandContext);
}
@Bean
public CommandFilter CustomCommandFilter() {
return new CustomCommandFilter();
}
private static class CustomCommandFilter implements CommandFilter {
@Override
public int order() {
return 0;
}
@Override
public boolean shouldFilter(RestyCommand restyCommand) {
return true;
}
@Override
public CommandFilterType getFilterType() {
return CommandFilterType.BEFOR_EXECUTE;
}
@Override
public void before(RestyCommand restyCommand) throws FilterException {
System.out.println("custom command filter");
}
@Override
public void after(RestyCommand restyCommand, Object result) {
}
@Override
public void error(RestyCommand restyCommand, RestyException ex) {
}
}
}
引入jar
<dependency>
<groupId>com.github.darren-fu</groupId>
<artifactId>resty-pass</artifactId>
<version>1.3.0</version>
</dependency>