技术干货程序员微服务架构和实践

RestyPass,高性能HTTP调用客户端库

2017-08-03  本文已影响0人  化简为繁

RestyPass

高性能的Restful服务调用客户端库,完全兼容Spring MVC 注解,基于接口和注解自动代理客户端HTTP请求,支持服务发现,负载均衡,自动熔断,降级,重试,限流。覆盖Feign + Hystrix + Ribbon + ApacheHttpClient的功能

HOME

可与spring cloud/spring boot配套使用,帮助微服务架构更容易落地,解决服务间最后一公里的调用问题。
Github地址: https://github.com/darren-fu/RestyPass
码云地址: https://gitee.com/darren-fu/RestyPass

对比SpringCloud技术栈:Feign+Hystrix+Ribbon+ApacheHttpClient

示例(demo[调用方]+demo-serverside[服务端])

客户端代码

// 使用@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";
    }
}

客户端服务


//使用接口和注解定义并配置调用客户端
//RestyService注解定义服务
@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注解定义服务接口
    // 同步调用
    @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);


    // 异步调用形式: Future<?> 参数类型,出参
    @RestyMethod
    @RequestMapping(value = "/get_status", method = RequestMethod.GET)
    String getStatus(RestyFuture<String> future);

    // 异步调用形式: Future<?> 返回类型
    @RestyMethod
    @RequestMapping(value = "/get_user", method = RequestMethod.GET)
    Future<User> getUser();
    
    
}

服务实例定义

# resty-server.yaml
servers:
  - serviceName: server
    instances:
      - host: localhost
        port: 9201
      - host: localhost
        port: 9202

服务端代码


@RestController
@RequestMapping("/resty")
public class TestController {
    @RequestMapping(value = "/get_nothing", method = RequestMethod.GET)
    public void nothing() {
        System.out.println("############nothing");
    }
  }

核心接口

配置与注入

注入自己的实现类,实现特殊需求。

@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>
上一篇 下一篇

猜你喜欢

热点阅读