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 入門教程