Cloud Native雲原生是一種遵循“12元素應用”理念的應用開發的風格,Spring Cloud針對雲原生的概念,在Spring Boot的基礎上提供了兩個模塊: Spring Cloud Context和Spring Cloud Commons。
Spring Cloud Context:上下文
spring-cloud-context上下文模塊在Spring Boot的基礎添加了一些其他模塊會用到的共用功能:
Bootstrap Application Context:
用來在主context之前加載外部配置文件,相應的配置文件是bootstrap.yml
,可以通過
spring.cloud.bootstrap.enabled=false
來禁用這個過程。
Encryption/Decryption
Spring Cloud提供了Environment
pre-processor用於在本地解密被加密過的屬性值。如果需要加密功能則需要引入Spring Security RSA模塊 (Maven用戶的pom.xml 添加”org.springframework.security:spring-security-rsa”即可)
Endpoints
/env
用於更新Environment
和重新綁定@ConfigurationProperties
,修改log levels
/refresh
用於重載boot strap context並刷新@RefreshScope
beans
/restart
重啓ApplicationContext(默認禁用)
/pause
、/resume
調用ApplicationContext的Lifecycle方法(stop() and start())
Spring Cloud Commons: 通用抽象
spring-cloud-commons模塊提供了服務發現,負載均衡,斷路器等抽象層,獨立於具體的實現,被其他模塊使用,如服務註冊可以通過Eureka或Consul實現具體功能。
@EnableDiscoveryClient
該註解會在/META-INF/spring.factories查看DiscoveryClient接口的具體實現類,並添加自動配置。(如Eureka、Consul、Zookeeper)。同時commons模塊還有DiscoveryHealthIndicator用於健康監控。
ServiceRegistry
ServiceRegistry
接口定義了註冊和解除註冊的方法名,Registration
是個標記接口,表明一個註冊項。ServiceRegistry
的實現類默認會自動註冊當前的服務,可通過
spring.cloud.service-registry.auto-registration.enabled=false
禁用。
@LoadBalanced
通過該註解可以讓RestTemplate
通過Ribbon實現客戶端的負載均衡功能。spring-cloud-netflix-core的RibbonAutoConfiguration
實現ribbon自動配置。spring-cloud-netflix-eureka-client的RibbonEurekaAutoConfiguration
自動配置ribbon結合eureka客戶端。
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
public class MyClass {
@Autowired
private RestTemplate restTemplate;
public String doOtherStuff() {
String results = restTemplate.getForObject("http://stores/stores", String.class);
return results;
}
}
Fail Retry
如果你的classpath存在spring-retry依賴,Ribbon默認會配置請求失敗重試功能,可以使用Ribbon的my-client.ribbon.*
配置項。可以通過spring.cloud.loadbalancer.retry.enabled=false
禁用。可以配置自己的重試策略、回退策略、重試監聽器等bean:
@Configuration
public class MyConfiguration {
@Bean
LoadBalancedBackOffPolicyFactory backOffPolciyFactory() {
return new LoadBalancedBackOffPolicyFactory() {
@Override
public BackOffPolicy createBackOffPolicy(String service) {
return new ExponentialBackOffPolicy();
}
};
}
@Bean
LoadBalancedRetryListenerFactory retryListenerFactory() {
return new LoadBalancedRetryListenerFactory() {
@Override
public RetryListener[] createRetryListeners(String service) {
return new RetryListener[]{new RetryListener() {
@Override
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
//TODO Do you business...
return true;
}
@Override
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
//TODO Do you business...
}
@Override
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
//TODO Do you business...
}
}};
}
};
}
}
如果需要有多個RestTemplate實例,可以指定一個@Primary
@Configuration
public class MyConfiguration {
@LoadBalanced
@Bean
RestTemplate loadBalanced() {
return new RestTemplate();
}
@Primary
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
public class MyClass {
@Autowired
private RestTemplate restTemplate;
@Autowired
@LoadBalanced
private RestTemplate loadBalanced;
public String doOtherStuff() {
return loadBalanced.getForObject("http://stores/stores", String.class);
}
public String doStuff() {
return restTemplate.getForObject("http://example.com", String.class);
}
}
HTTP Client Factories
commons默認提供了兩種HTT client的工廠類(ApacheHttpClientFactory和OkHttpClientFactory),用於創建響應的HTTP客戶端。通過設置
spring.cloud.httpclientfactories.apache.enabled=false
或spring.cloud.httpclientfactories.ok.enabled=false
來禁用。