Spring Cloud 中文文档 第一部分 (第一次发博客,有什么错误请指出,谢谢)


声明:中文文档都是由软件翻译,翻译内容未检查校对,文档内容仅供参考)

Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线)的工具。 分布式系统的协调导致 锅炉板模式,使用Spring Cloud开发人员可以快速站出实现这些模式的服务和应用程序。 它们可以在任何分布式环境中运行良好,包括开发人员自己的笔记本电脑,裸机数 据中心以及Cloud Foundry等托管平台。

一、 Features

Spring Cloud专注于为典型用例和可扩展性机制提供良好的即时体验,以覆盖其他人。
Distributed/versioned configuration
Service registration and discovery
Routing
Service-to-service calls
Load balancing
Circuit Breakers
Distributed messaging

二、Part I. Cloud Native Applications译 :第一部分云本地应用程序

Cloud Native是一种应用程序开发风格,鼓励在持续交付和价值驱动开发领域轻松采用最佳实践。 一个相关的学科是建设12-factor Applications ,其中开发实践与交付和运
营目标相一致 - 例如,通过使用声明式编程和管理以及监控。 Spring Cloud以许多特定的方式促进了这些开发风格。 起点是分布式系统中的所有组件都需要轻松访问的一组
功能。
Spring Cloud构建的Spring Boot涵盖了其中的许多功能。 Spring Cloud还提供了两个库:Spring Cloud Context和Spring Cloud Commons。 Spring Cloud Context为Spring
Cloud应用程序(引导上下文,加密,刷新范围和环境端点)的 ApplicationContext 提供实用程序和特殊服务。 Spring Cloud Commons是一组用于不同Spring Cloud实
现(如Spring Cloud Netflix和Spring Cloud Consul)的抽象和常用类。
如果由于“非法密钥大小”而导致异常并且您使用Sun的JDK,则需要安装Java加密扩展(JCE)无限制强制管辖权策略文件。 有关更多信息,请参阅以下链接:
Java 6 JCE
Java 7 JCE
Java 8 JCE
将文件解压缩到您使用的JRE / JDK x64 / x86版本的JDK / jre / lib / security文件夹中。
Spring Cloud是在非限制性的Apache 2.0许可下发布的。 如果您想为本文档的这一部分做出贡献,或者如果您发现错误,您可以在github找到项目的源代码和
问题跟踪器。

Spring Cloud Context: Application Context Services译 :

2. SpringCloud上下文:应用程序上下文服务 上

Spring Boot对如何使用Spring构建应用程序有着自己的观点。 例如,它具有常规配置文件的常规位置,并具有用于常见管理和监视任务的端点。 Spring云建立在此基础之
上,并添加了一些可能系统中所有组件都会使用或偶尔需要的功能。

2.1 The Bootstrap Application Context译

: 2.1Bootstrap








Spring Cloud应用程序通过创建一个“bootstrap”上下文来运行,该上下文是主应用程序的父上下文。 它负责从外部源加载配置属性,并负责解密本地外部配置文件中的属
性。 这两个上下文共享一个 Environment ,这是任何Spring应用程序的外部属性的来源。 默认情况下,引导属性(不是 bootstrap.properties 但是在引导阶段加载
的属性)以高优先级添加,因此它们不能被本地配置覆盖。
引导程序上下文使用不同的约定来定位外部配置,而不是主应用程序上下文。 而不是 application.yml (或 .properties ),您可以使用 bootstrap.yml ,保持
bootstrap的外部配置和主环境很好地分离。 以下列表显示了一个示例:
bootstrap.yml。
spring:
application:
name: foo
cloud:
config:
uri: ${SPRING_CONFIG_URI:http://localhost:8888}
如果您的应用程序需要来自服务器的任何特定于应用程序的配置,则最好设置 spring.application.name ( bootstrap.yml 或 application.yml )。您可以通过设置 spring.cloud.bootstrap.enabled=false (例如,在系统属性中)完全禁用引导过程。

2.2 Application Context Hierarchies译

: 2.2












如果您从 SpringApplication 或 SpringApplicationBuilder 构建应用程序上下文,则引导程序上下文将作为该上下文的父级添加。 Spring的一个特性是子级上下文
从父级继承属性来源和配置文件,因此与不使用Spring Cloud Config构建相同上下文相比,“main”应用程序上下文包含其他属性来源。 额外的财产来源是:
“bootstrap”: If any PropertySourceLocators are found in the Bootstrap context and if they have non-empty properties, an optional CompositePropertySource
appears with high priority. An example would be properties from the Spring Cloud Config Server. See “Section 2.6, “Customizing the Bootstrap Property Sources”” for
instructions on how to customize the contents of this property source.
“applicationConfig: [classpath:bootstrap.yml]” (and related files if Spring profiles are active): If you have a bootstrap.yml (or .properties ), those properties are
used to configure the Bootstrap context. Then they get added to the child context when its parent is set. They have lower precedence than the application.yml (or
.properties ) and any other property sources that are added to the child as a normal part of the process of creating a Spring Boot application. See “Section 2.3,
“Changing the Location of Bootstrap Properties”” for instructions on how to customize the contents of these property sources.
由于属性来源的排序规则,“bootstrap”条目优先。 但是请注意,这些数据不包含 bootstrap.yml 中的任何数据,它们的优先级非常低,但可用于设置默认值。
您可以通过设置任何父上下文扩展上下文结构 ApplicationContext 你create €” 例如,通过使用自己的接口或与 SpringApplicationBuilder 方便的方法(
parent() , child() 和 sibling() )。 引导程序上下文是您自己创建的最高级祖先的父级。 层次结构中的每个上下文都有自己的“底层”(可能为空)属性来源,以避
免无意中将价值从父母推到其后代。 如果有配置服务器,则层次结构中的每个上下文也可以(原则上)具有不同的 spring.application.name ,因此也是不同的远程属
性源。 普通的Spring应用上下文行为规则适用于属性解析:来自子上下文的属性覆盖父类中的属性,按名称和属性源名称。 (如果孩子的姓名与父母姓名相同,则父母的
值不包含在孩子中)。
请注意, SpringApplicationBuilder 可让您在整个层次结构中共享 Environment ,但这不是默认设置。 因此,兄弟情境尤其不需要具有相同的概况或财产来源,即
使它们可能与其父母分享共同的价值。

2.3 Changing the Location of Bootstrap Properties译

: 2.3










例如,在系统属性中,可以通过设置 spring.cloud.bootstrap.name (默认值: bootstrap )或 spring.cloud.bootstrap.location (默认值:空)来指
定 bootstrap.yml (或 .properties )位置。 这些属性的行为与具有相同名称的 spring.config.* 变体类似。 实际上,它们用于通过在 ApplicationContext 设
置这些属性来设置引导程序 Environment 。 如果有活动的配置文件(来自 spring.profiles.active 或通过您正在构建的上下文中的 Environment API),那么该配
置文件中的属性也会加载,与常规的Spring Boot应用程序相同 - 例如从 bootstrap-development.properties 获取 development 个人资料。

2.4 Overriding the Values of Remote Properties译

: 2.4









通过引导上下文添加到应用程序的属性源通常是“远程”(来自Spring Cloud Config Server的示例)。 默认情况下,它们不能在本地覆盖。 如果您想让应用程序使用自己的
系统属性或配置文件覆盖远程属性,那么远程属性源必须通过设置 spring.cloud.config.allowOverride=true (无法在本地进行设置)授予其权限。 一旦设置了该
标志,两个更细粒度的设置将控制远程属性相对于系统属性和应用程序的本地配置的位置:
spring.cloud.config.overrideNone=true : Override from any local property source.
spring.cloud.config.overrideSystemProperties=false : Only system properties, command line arguments, and environment variables (but not the local config
files) should override the remote settings.

2.5 Customizing the Bootstrap Configuration译

: 2.5








通过在名为 org.springframework.cloud.bootstrap.BootstrapConfiguration 的密钥下向 /META-INF/spring.factories 添加条目,可以将引导程序上下文设
置为执行任何您喜欢的 org.springframework.cloud.bootstrap.BootstrapConfiguration 。 这包含用于创建上下文的Spring @Configuration 类的逗号分隔列
表。 您可以在此处创建任何要用于自动装配的主应用程序上下文的bean。 @Beans 类型的 ApplicationContextInitializer 有一个特殊的合同。 如果要控制启动顺
序,可以使用 @Order 注释标记类(默认顺序为 last )。
当添加自定义 BootstrapConfiguration ,小心你添加类不是 @ComponentScanned 误到您的A€œmainâ€应用程序上下文,这里可能并不需要它们。 为引
导配置类使用单独的软件包名称,并确保该名称未被您的 @ComponentScan 或 @SpringBootApplication 注释的配置类所覆盖。
引导进程通过注入初始化到主结束 SpringApplication 实例(这是一般的弹簧引导启动序列,无论是运行作为独立应用程序或部署在应用服务器)。 首先,
从 spring.factories 的类中创建引导程序上下文。 然后,所有 @Beans 型 ApplicationContextInitializer 被添加到主 SpringApplication 它开始之前。

2.6 Customizing the Bootstrap Property Sources译

: 2.6









通过引导进程添加的外部配置的默认属性源是Spring Cloud Config Server,但可以通过将 PropertySourceLocator 类型的 PropertySourceLocator 添加到引导上下文
(通过 spring.factories )来添加其他源。 例如,您可以从其他服务器或数据库插入其他属性。
作为一个例子,考虑下面的自定义定位器:
@Configuration
public class CustomPropertySourceLocator implements PropertySourceLocator {
@Override
public PropertySource<?> locate(Environment environment) {
return new MapPropertySource(“customProperty”,
Collections.<String, Object>singletonMap(“property.from.sample.custom.source”, “worked as intended”));
}
}
该 Environment 传递进来的是一个为 ApplicationContext 即将被created €” 换句话说,对于我们供应附加属性源之一。 它已经有了普通的Spring Boot提供的
属性资源,因此您可以使用它们来定位特定于此 Environment 的属性源(例如,通过在 spring.application.name 上键入它,如在默认的Spring Cloud Config Server
属性源定位器中所做的那样) 。
如果你创建一个罐子这个类,然后添加一个 META-INF/spring.factories 包含以下, customProperty PropertySource 出现在任何应用程序,包括在其类路径的
jar:org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator

2.7 Logging Configuration译

: 2.7





如果你打算使用Spring Boot来配置日志设置,那么你应该把这个配置放在`bootstrap。[yml |中 属性],如果你想它适用于所有事件。

2.8 Environment Changes译

: 2.8





应用程序监听 EnvironmentChangeEvent 并以一些标准方式对更改作出反应( ApplicationListeners 可以通过正常方式将其他 ApplicationListeners 添加
为 @Beans )。 当观察到 EnvironmentChangeEvent ,它具有已更改的键值列表,并且应用程序使用它们来:
Re-bind any @ConfigurationProperties beans in the context
Set the logger levels for any properties in logging.level.*
请注意,默认情况下,Config Client不会轮询 Environment 中的 Environment 。 一般来说,我们不会推荐用于检测更改的方法(尽管您可以使用 @Scheduled 注释进行
设置)。 如果您有扩展的客户端应用程序,则最好将 EnvironmentChangeEvent 广播到所有实例,而不是让他们轮询更改(例如,通过使用Spring Cloud Bus )。
EnvironmentChangeEvent 涵盖了一大类刷新用例,只要您可以实际更改 Environment 并发布该事件。 请注意,这些API是公开的,并且是核心Spring的一部分)。 您
可以通过访问 /configprops 端点(普通的Spring Boot执行器功能)来验证更改是否与 @ConfigurationProperties Bean绑定。 例如, DataSource 可以在运行时更
改其 maxPoolSize (由Spring Boot创建的默认 DataSource 是 @ConfigurationProperties bean)并动态增长容量。 重新绑定 @ConfigurationProperties 不包含
另一大类用例,您需要对刷新进行更多控制,并且需要对整个 ApplicationContext 进行原子级更改。 为了解决这些问题,我们有 @RefreshScope 。

2.9 Refresh Scope译

: 2.9





当有配置更改,弹簧 @Bean 标记为 @RefreshScope 得到特殊待遇。 该功能解决了初始化时仅注入配置的有状态Bean的问题。 例如,如果 DataSource 在通
过 Environment 更改数据库URL时已打开连接,则可能希望这些连接的持有者能够完成他们正在执行的操作。 然后,下一次从池中借用一个连接时,它将获得一个新的
URL。
有时,甚至可能必须将 @RefreshScope 注释应用于一些只能初始化一次的bean。 如果一个bean是“不可变的”,你必须用 @RefreshScope 注释这个bean或者在属性关键
字 spring.cloud.refresh.extra-refreshable 下面指定类 spring.cloud.refresh.extra-refreshable 。
刷新范围bean是使用它们进行初始化的惰性代理(即,调用方法时),范围充当初始化值的缓存。 要强制一个bean在下一次方法调用时重新初始化,必须使其缓存项无
效。
RefreshScope 是上下文中的bean,具有公共方法 refreshAll() ,通过清除目标缓存来刷新作用域中的所有Bean。 /refresh 端点公开了此功能(通过HTTP或
JMX)。 要按名称刷新单个bean,还有一个 refresh(String) 方法。
要公开 /refresh 端点,您需要将以下配置添加到您的应用程序中:
management:
endpoints:
web:
exposure:
include: refresh
@RefreshScope (技术上)在 @Configuration 类上工作,但可能会导致令人惊讶的行为。 例如,这并不意味着 @Beans 定义的所有 @Beans 本身都
在 @RefreshScope 。 具体来说,除非它本身在 @RefreshScope ,否则依赖于这些bean的任何东西都不能依赖它们在刷新时被更新。 在这种情况下,它将
在刷新时重建,并且它的依赖关系会被重新注入。 此时,它们从刷新的 @Configuration 重新初始化)。

2.10 Encryption and Decryption译

: 2.10






Spring Cloud拥有用于本地解密属性值的 Environment 预处理器。 它遵循与配置服务器相同的规则,并具有通过 encrypt.* 的相同外部配置。 因此,您可以使
用 {cipher}* 形式的加密值,并且只要有一个有效的密钥,它们在主应用程序上下文获取 Environment 设置之前被解密。 要在应用程序中使用加密功能,您需要在您的
类路径中包含Spring Security RSA(Maven协调:“org.springframework.security:spring-security-rsa”),并且您还需要完整强度的JCE扩展你的JVM。
如果由于“非法密钥大小”而导致异常并且您使用Sun的JDK,则需要安装Java加密扩展(JCE)无限制强制管辖权策略文件。 有关更多信息,请参阅以下链接:
Java 6 JCE
Java 7 JCE
Java 8 JCE
将文件解压缩到您使用的JRE / JDK x64 / x86版本的JDK / jre / lib / security文件夹中。

2.11 Endpoints译

: 2.11



对于Spring Boot Actuator应用程序,可以使用其他一些管理端点。 您可以使用:
POST to /actuator/env to update the Environment and rebind @ConfigurationProperties and log levels.
/actuator/refresh to re-load the boot strap context and refresh the @RefreshScope beans.
/actuator/restart to close the ApplicationContext and restart it (disabled by default).
/actuator/pause and /actuator/resume for calling the Lifecycle methods ( stop() and start() on the ApplicationContext ).
如果禁用 /actuator/restart 端点,那么 /actuator/pause 和 /actuator/resume 端点也将被禁用,因为它们只是 /actuator/restart 。

3. Spring Cloud Commons: Common Abstractions译

: 3.











诸如服务发现,负载均衡和断路器等模式适合于所有Spring Cloud客户端都可以使用的通用抽象层,而与实现无关(例如,使用Eureka或Consul进行发现)。

3.1 @EnableDiscoveryClient译

: 3.1@EnableDiscoveryClient
Spring Cloud Commons提供了 @EnableDiscoveryClient 注释。 这将查找与 META-INF/spring.factories 的 DiscoveryClient 接口
的 META-INF/spring.factories 。 在发现客户端的实现添加配置
类 spring.factories 下 org.springframework.cloud.client.discovery.EnableDiscoveryClient 关键。 的实例 DiscoveryClient 实现包括Spring Cloud
Netflix Eureka , Spring Cloud Consul Discovery ,和Spring Cloud Zookeeper Discovery 。默认情况下, DiscoveryClient 实现自动注册本地Spring Boot服务器和远程发现服务器。 此行为可通过在 @EnableDiscoveryClient 设置 autoRegister=false 来
禁用。
@EnableDiscoveryClient 不再需要。 您可以在类路径上放置 DiscoveryClient 实现,以使Spring Boot应用程序向 服务发现服务器注册。

3.1.1 Health Indicator译

: 3.1.1





共享创建一个春天引导 HealthIndicator 是 DiscoveryClient 实现可以通过实施参与 DiscoveryHealthIndicator 。 要禁用复合 HealthIndicator ,请设
置 spring.cloud.discovery.client.composite-indicator.enabled=false 。 自动配置基于 DiscoveryClient 通用 HealthIndicator (
DiscoveryClientHealthIndicator )。 要禁用它,请设置 spring.cloud.discovery.client.health-indicator.enabled=false 。 要禁
用 DiscoveryClientHealthIndicator 的说明字段,请设置 spring.cloud.discovery.client.health-indicator.include-description=false 。 否则,它可
以冒泡为 description 的卷起的 HealthIndicator 。

3.2 ServiceRegistry译

: 3.2ServiceRegistry
Commons现在提供了一个 ServiceRegistry 接口,该接口提供了诸如 register(Registration) 和 deregister(Registration) ,它们允许您提供自定义注册服
务。 Registration 是标记界面。
以下示例显示正在使用的 ServiceRegistry :
@Configuration
@EnableDiscoveryClient(autoRegister=false)
public class MyConfiguration {
private ServiceRegistry registry;
public MyConfiguration(ServiceRegistry registry) {
this.registry = registry;
}
// called through some external process, such as an event or a custom actuator endpoint
public void register() {
Registration registration = constructRegistration();
this.registry.register(registration);
}
}
每个 ServiceRegistry 实现都有其自己的 Registry 实现。
ZookeeperRegistration used with ZookeeperServiceRegistry
EurekaRegistration used with EurekaServiceRegistry
ConsulRegistration used with ConsulServiceRegistry
如果您使用的是 ServiceRegistry 接口,则需要为您正在使用的 ServiceRegistry 实现传递正确的 Registry 实现。

3.2.1 ServiceRegistry Auto-Registration译

: 3.2.1ServiceRegistry





默认情况下, ServiceRegistry 实现会自动注册正在运行的服务。 要禁用该行为,您可以设置:* @EnableDiscoveryClient(autoRegister=false) 以永久禁用自
动注册。 * spring.cloud.service-registry.auto-registration.enabled=false 通过配置禁用行为。

3.2.2 Service Registry Actuator Endpoint 译

: 3.2.2










Spring Cloud Commons提供了一个 /service-registry 执行器端点。 该端点依赖于Spring应用程序上下文中的 Registration bean。 调用 /service-registry 用
GET返回的状态 Registration 。 将POST用于具有JSON主体的相同端点会将当前 Registration 的状态更改为新值。 JSON正文必须包含具有首选值的 status 字
段。 在更新状态和返回状态值时,请参阅 ServiceRegistry 实施的文档,以 ServiceRegistry 允许的值。 例如,Eureka’的支持状态为 UP , DOWN ,
OUT_OF_SERVICE ,并 UNKNOWN 。

3.3 Spring RestTemplate as a Load Balancer Client译

: 3.3SpringRestTemplate











RestTemplate 可以自动配置为使用功能区。 要创建负载平衡 RestTemplate ,请创建 RestTemplate @Bean 并使用 @LoadBalanced 限定符,如下例所示:
@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;
}
}
Caution
一个 RestTemplate bean不再通过自动配置创建。 个人应用程序必须创建它。
该URI需要使用虚拟主机名称(即服务名称,而不是主机名称)。 功能区客户端用于创建完整的物理地址。 见RibbonAutoConfiguration为的是如何细节 RestTemplate 设
置。

3.4 Spring WebClient as a Load Balancer Client译

: 3.4Spring WebClient











WebClient 可以自动配置为使用 LoadBalancerClient 。 要创建负载平衡的 WebClient ,请创建 WebClient.Builder @Bean 并使用 @LoadBalanced 限定符,如下
例所示:
@Configuration
public class MyConfiguration {
@Bean
@LoadBalanced
public WebClient.Builder loadBalancedWebClientBuilder() {
return WebClient.builder();
}
}
public class MyClass {
@Autowired
private WebClient.Builder webClientBuilder;
public Mono doOtherStuff() {
return webClientBuilder.build().get().uri(“http://stores/stores”)
.retrieve().bodyToMono(String.class);
}
}
该URI需要使用虚拟主机名称(即服务名称,而不是主机名称)。 功能区客户端用于创建完整的物理地址。

3.4.1 Retrying Failed Requests译

: 3.4.1








负载均衡 RestTemplate 可以配置为重试失败的请求。 默认情况下,该逻辑被禁用。 您可以通过将Spring Retry添加到应用程序的类路径来启用它。 负载均
衡 RestTemplate 尊重与重试失败请求相关的某些功能区配置值。 您可以使用 client.ribbon.MaxAutoRetries , client.ribbon.MaxAutoRetriesNextServer
,并 client.ribbon.OkToRetryOnAllOperations 性能。 如果您希望在类路径中使用Spring重试禁用重试逻辑,则可以设
置 spring.cloud.loadbalancer.retry.enabled=false 。 请参阅Ribbon documentation以了解这些属性的用途 。
如果您想在重试中实现 BackOffPolicy ,则需要创建类型为 LoadBalancedBackOffPolicyFactory 的bean,并返回您希望用于给定服务的 BackOffPolicy ,如以
下示例所示:
@Configuration
public class MyConfiguration {
@Bean
LoadBalancedBackOffPolicyFactory backOffPolciyFactory() {
return new LoadBalancedBackOffPolicyFactory() {
@Override
public BackOffPolicy createBackOffPolicy(String service) {
return new ExponentialBackOffPolicy();
}
};
}
}
上述示例中的 client 应替换为您的Ribbon客户端的名称。
如果要将一个或多个 RetryListener 实现添加到重试功能中,则需要创建类型为 LoadBalancedRetryListenerFactory 的bean,并返回您希望用于给定服务的
RetryListener 阵列,如以下示例所示:
@Configuration
public class MyConfiguration {
@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…
}
}};
}
};
}
}

3.5 Multiple RestTemplate objects译

: 3.5



RestTemplate



如果你想要一个 RestTemplate 未负载平衡,营造 RestTemplate bean并注入它。 要访问负载平衡的 RestTemplate ,请在创建 @Bean 时使用 @LoadBalanced 限定
符,如以下示例所示:@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);
}
}
Important
请注意,在前面的示例中, @Primary 注释在 RestTemplate 声明中的使用可以消除不合格的 @Autowired 注入的歧义。
如果您看到错误(例如
java.lang.IllegalArgumentException: Can not set org.springframework.web.client.RestTemplate field com.my.app.Foo.restTemplate to com.sun.proxy.$Proxy89
,请尝试注入 RestOperations 或设置 spring.aop.proxyTargetClass=true 。

3.6 Spring WebFlux WebClient as a Load Balancer Client译

: 3.6Spring WebFluxWebClient











WebClient 可以配置为使用 LoadBalancerClient 。 LoadBalancerExchangeFilterFunction 自动配置如果 spring-webflux 是在classpath。 以下示例显示如何
配置 WebClient 以使用负载均衡器:
public class MyClass {
@Autowired
private LoadBalancerExchangeFilterFunction lbFunction;
public Mono doOtherStuff() {
return WebClient.builder().baseUrl(“http://stores”)
.filter(lbFunction)
.build()
.get()
.uri("/stores")
.retrieve()
.bodyToMono(String.class);
}
}
该URI需要使用虚拟主机名称(即服务名称,而不是主机名称)。 LoadBalancerClient 用于创建完整的物理地址。

3.7 Ignore Network Interfaces译

: 3.7







有时候,忽略某些命名的网络接口是有用的,这样可以将它们从服务发现注册中排除(例如,在Docker容器中运行时)。 正则表达式列表可以设置为使所需的网络接口被
忽略。 以下配置将忽略 docker0 接口以及以 veth 开头的所有接口:
application.yml。
spring:
cloud:
inetutils:
ignoredInterfaces:

  • docker0
  • veth.*
    您还可以使用正则表达式列表强制仅使用指定的网络地址,如下例所示:
    bootstrap.yml。
    spring:
    cloud:
    inetutils:
    preferredNetworks:
  • 192.168
  • 10.0
    您还可以强制使用站点本地地址,如以下示例所示:.application.ymlspring:
    cloud:
    inetutils:
    useOnlySiteLocalInterfaces: true
    有关构成站点本地地址的更多详细信息,请参阅 Inet4Address.html.isSiteLocalAddress() 。

3.8 HTTP Client Factories译

: 3.8HTTP






Spring Cloud Commons提供了用于创建Apache HTTP客户端( ApacheHttpClientFactory )和OK HTTP客户端( OkHttpClientFactory )
的 OkHttpClientFactory 。 仅当OK HTTP jar位于类路径中时,才会创建 OkHttpClientFactory bean。 另外,Spring Cloud Commons提供了用于创建两个客户端使
用的连接管理器的bean: ApacheHttpClientConnectionManagerFactory 用于Apache HTTP客户端, OkHttpClientConnectionPoolFactory 用于OK HTTP客户
端。 如果您想定制在下游项目中如何创建HTTP客户端,则可以提供您自己的这些bean的实现。 另外,如果您提供 HttpClientBuilder 或 OkHttpClient.Builder 类型
的bean,默认工厂会使用这些构建器作为构建器返回到下游项目的基础。 您还可以通过设置来禁用这些bean创
建 spring.cloud.httpclientfactories.apache.enabled 或者 spring.cloud.httpclientfactories.ok.enabled 至 false 。

3.9 Enabled Features译

: 3.9






Spring Cloud Commons提供了一个 /features 执行器端点。 此端点返回类路径上可用的功能以及它们是否已启用。 返回的信息包括功能类型,名称,版本和供应商。

3.9.1 Feature types译

: 3.9.1





有两种类型的“功能”:抽象和命名。
抽象特征是其中的接口或抽象类定义,并且一个实施创建功能,如 DiscoveryClient , LoadBalancerClient ,或 LockService 。 抽象类或接口用于在上下文中查
找该类型的bean。 显示的版本是 bean.getClass().getPackage().getImplementationVersion() 。
已命名的功能是没有实现特定类的功能,例如“断路器”,“API网关”,“春季云总线”等。 这些功能需要一个名称和一个bean类型。

3.9.2 Declaring features译

: 3.9.2





任何模块都可以声明任意数量的 HasFeature bean,如以下示例中所示:
@Bean
public HasFeatures commonsFeatures() {
return HasFeatures.abstractFeatures(DiscoveryClient.class, LoadBalancerClient.class);
}
@Bean
public HasFeatures consulFeatures() {
return HasFeatures.namedFeatures(
new NamedFeature(“Spring Cloud Bus”, ConsulBusAutoConfiguration.class),
new NamedFeature(“Circuit Breaker”, HystrixCommandAspect.class));
}
@Bean
HasFeatures localFeatures() {
return HasFeatures.builder()
.abstractFeature(Foo.class)
.namedFeature(new NamedFeature(“Bar Feature”, Bar.class))
.abstractFeature(Baz.class)
.build();
}
这些豆中的每一个都应该进入适当的守卫 @Configuration 。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值