使用Apache Camel构建微服务-spring cloud 入门教程

Apache Camel 是一个非常强大的基于规则的路由以及媒介引擎,该引擎提供了一个基于POJO的 企业应用模式(Enterprise Integration Patterns)的实现,你可以采用其异常强大且十分易用的API (可以说是一种Java的领域定义语言 Domain Specific Language)来配置其路由或者中介的规则。 通过这种领域定义语言,你可以在你的IDE中用简单的Java Code就可以写出一个类型安全并具有一定智能的规则描述文件。这与那种复杂的XML配置相比极大简化了规则定义开发。 当然Apache Camel也提供了一个对Spring 配置文件的支持,它还包含一组用于构建微服务的组件。比如它支持来自 Netflix OSS 的 Hystrix Circuit Breaker 和其他解决方案,如 Ribbon 负载均衡器。还有使用 Zipkin 组件的分布式消息跟踪,以及使用 Consul、etcd、Kubernetes 和 Netflix Ribbon 的服务注册和发现。 

其中最重要微服务支持的新关键组件是ServiceCall EIP,它允许在分布式系统中调用远程服务,该服务从 Kubernetes、Openshift、Cloud Foundry、Zuul、Consul、Zookeeper 等的服务注册表中查找

在下面的代码片段中,您可以看到支持 Hystrix 断路器、Ribbon 负载均衡器和 Consul 服务发现和注册的 DLS 路由配置。作为路由定义中的服务发现,您还可以使用其他一些解决方案来代替 Consul,例如 etcd ( etcServiceDiscovery) 或 Kubernetes ( kubernetesServiceDiscovery)。

from("direct:account")
   .to("bean:customerService?method=findById(${header.id})")
   .log("Msg: ${body}").enrich("direct:acc", new AggregationStrategyImpl());

from("direct:acc").setBody().constant(null)
   .hystrix()
      .hystrixConfiguration()
         .executionTimeoutInMilliseconds(2000)
      .end()
      .serviceCall()
         .name("account//account")
         .component("netty4-http")
         .ribbonLoadBalancer("ribbon-1")
         .consulServiceDiscovery("http://192.168.99.100:8500")
      .end()
      .unmarshal(format)
   .endHystrix()
   .onFallback()
   .to("bean:accountFallback?method=getAccounts");

我们只需调用hystrixConfiguration方法就可以轻松配置Hystrix的所有参数在上面的示例中,Hystrix 最多等待 2 秒以等待来自远程服务的响应。在超时回退的情况下@Bean调用。回退@Bean实现非常简单——它返回空列表。

@Service
public class AccountFallback {

   public List<Account> getAccounts() {
      return new ArrayList<>();
   }

}

或者,可以使用对象声明来实现配置。这是带有 Ribbon 和 Consul 的服务调用配置。此外,我们可以为 Ribbon 提供一些参数,例如客户端读取超时或最大重试次数。不幸的是,它们似乎在这个版本的 Apache Camel 中不起作用(您可以尝试自己测试)。我希望这会很快得到纠正。

ServiceCallConfigurationDefinition def = new ServiceCallConfigurationDefinition();

ConsulConfiguration config = new ConsulConfiguration();
config.setUrl("http://192.168.99.100:8500");
config.setComponent("netty4-http");
ConsulServiceDiscovery discovery = new ConsulServiceDiscovery(config);

RibbonConfiguration c = new RibbonConfiguration();
c.addProperty("MaxAutoRetries", "0");
c.addProperty("MaxAutoRetriesNextServer", "1");
c.addProperty("ReadTimeout", "1000");
c.setClientName("ribbon-1");
RibbonServiceLoadBalancer lb = new RibbonServiceLoadBalancer(c);
lb.setServiceDiscovery(discovery);

def.setComponent("netty4-http");
def.setLoadBalancer(lb);
def.setServiceDiscovery(discovery);
context.setServiceCallConfiguration(def);

我在之前的一篇文章《使用Hystrix 、Feign 和 Ribbon构建微服务-spring cloud 入门教程》中描述了 Spring Cloud 和 Netflix OSS 的类似案例。就像在那里提供的示例中一样,我还在 中设置了延迟account-service,这取决于启动微服务的端口。

@Value("${port}")
private int port;

public List<Account> findByCustomerId(Integer customerId) {
   List<Account> l = new ArrayList<>();
   l.add(new Account(1, "1234567890", 4321, customerId));
   l.add(new Account(2, "1234567891", 12346, customerId));
   if (port%2 == 0) {
      try {
         Thread.sleep(5000);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
   }
   return l;
}

Spring Cloud 示例的结果更加令人满意。引入的配置参数(例如 Ribbon 的读取超时)起作用,此外,Hystrix 能够自动将数量少得多的请求重定向到慢速服务——只有 2% 的其余请求在 5 秒内重定向到非阻塞线程实例。这表明 Apache Camel 如果想与 Sprint Cloud 框架在微服务支持方面竞争,还有一些需要改进的地方。

使用 Zuul、Ribbon、Feign、Eureka 和 Sleuth、Zipkin 创建简单spring cloud微服务用例-spring cloud 入门教程
微服务集成SPRING CLOUD SLEUTH、ELK 和 ZIPKIN 进行监控-spring cloud 入门教程
使用Hystrix 、Feign 和 Ribbon构建微服务-spring cloud 入门教程

使用 Spring Boot Admin 监控微服务-spring cloud 入门教程

基于Redis做Spring Cloud Gateway 中的速率限制实践-spring cloud 入门教程
集成SWAGGER2服务-spring cloud 入门教程
Hystrix 简介-spring cloud 入门教程
Hystrix 原理深入分析-spring cloud 入门教程 
使用Apache Camel构建微服务-spring cloud 入门教程
集成 Kubernetes 来构建微服务-spring cloud 入门教程
集成SPRINGDOC OPENAPI 的微服务实践-spring cloud 入门教程
SPRING CLOUD 微服务快速指南-spring cloud 入门教程
基于GraphQL的微服务实践-spring cloud 入门教程
最火的Spring Cloud Gateway 为经过身份验证的用户启用速率限制实践-spring cloud 入门教程
原文地址:https://www.cnblogs.com/BlogNetSpace/p/15149530.html