Appearance
Spring Cloud 2020.0.x (Ilford) 版本
官方介绍
Spring Cloud 2020.0.x (代号 Ilford) 是 Spring Cloud 的重要版本,于 2020 年底发布。这是第一个采用年度版本号命名的 Spring Cloud 版本,标志着项目向更规律的发布周期转变。该版本基于 Spring Boot 2.4.x 构建,重点关注云原生应用开发、响应式编程模型以及微服务架构的最佳实践。
Ilford 版本引入了许多现代化的微服务模式,加强了对 Kubernetes 等容器编排平台的支持。
版本特性
1. Spring Cloud Commons 重构
- 引入新的服务注册与发现抽象
- 改进的负载均衡器实现
- 更好的跨平台兼容性
2. Spring Cloud Gateway 3.x
- 响应式编程模型完全集成
- 更强大的路由匹配功能
- 改进的性能和内存管理
3. Spring Cloud LoadBalancer 增强
- 支持服务注册表感知的负载均衡
- 更多负载均衡算法
- 更好的可观察性支持
4. Spring Cloud Config 更新
- 支持更多的后端存储选项
- 增强的加密功能
- 更灵活的配置刷新机制
5. Spring Cloud Kubernetes 集成
- 原生 Kubernetes 服务发现
- 配置映射自动加载
- 更好的云原生支持
官方获取地址
Maven 依赖
xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Gradle 依赖
gradle
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2020.0.6"
}
}官方文档
环境要求
- Java 8 或更高版本 (Java 11+ 推荐)
- Spring Boot 2.4.x 或 2.5.x
- Maven 3.5+ 或 Gradle 6.8+
- 推荐使用 Spring Boot 2.5.12 以获得最佳兼容性
部署方法
1. 创建 Spring Cloud 2020.0.x 项目
bash
# 使用 Spring Initializr 创建项目
curl https://start.spring.io/starter.tgz \
-d groupId=com.example \
-d artifactId=spring-cloud-ilford-service \
-d name=ilford-service \
-d packaging=jar \
-d bootVersion=2.5.12 \
-d javaVersion=11 \
-d language=java \
-d type=maven-project \
-d baseDir=ilford-service \
-d packageName=com.example \
-d dependencies=cloud-discovery,cloud-config-client,cloud-openfeign,cloud-loadbalancer \
| tar -xzvf -2. 配置服务注册中心 (使用 Consul)
java
package com.example.ilford.discovery;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryApplication.class, args);
}
}application.yml:
yaml
server:
port: 8500
spring:
application:
name: discovery-service
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: discovery-service
health-check-path: /actuator/health
health-check-interval: 15s
instance-id: ${spring.application.name}:${random.value}
management:
endpoints:
web:
exposure:
include: health,info,metrics3. 配置服务消费者
java
package com.example.ilford.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Bean
public WebClient webClient() {
return WebClient.builder()
.codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(2 * 1024 * 1024))
.build();
}
@Bean
public UserService userService(WebClient.Builder webClientBuilder) {
HttpServiceProxyFactory factory = HttpServiceProxyFactory
.builder(WebClientAdapter.forClient(webClientBuilder.build())).build();
return factory.createClient(UserService.class);
}
interface UserService {
@GetExchange("/api/users/{id}")
String getUser(Long id);
}
}4. 部署到 Kubernetes
deployment.yaml:
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ilford-service
spec:
replicas: 2
selector:
matchLabels:
app: ilford-service
template:
metadata:
labels:
app: ilford-service
spec:
containers:
- name: ilford-service
image: spring-cloud-ilford:2020.0.x
ports:
- containerPort: 8080
env:
- name: SPRING_PROFILES_ACTIVE
value: kubernetes
- name: JAVA_OPTS
value: "-Xmx512m -Xms256m"
---
apiVersion: v1
kind: Service
metadata:
name: ilford-service
spec:
selector:
app: ilford-service
ports:
- protocol: TCP
port: 8080
targetPort: 8080
type: ClusterIP二次开发
1. 自定义服务发现事件监听
java
package com.example.ilford.event;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class DiscoveryEventListener {
@EventListener
public void handleInstanceRegistered(InstanceRegisteredEvent event) {
System.out.println("Service registered: " + event.getInstance().getServiceId());
}
@EventListener
public void handleHeartbeat(HeartbeatEvent event) {
// 处理心跳事件,可用于健康检查逻辑
if (event.getValue() instanceof Integer) {
int counter = (Integer) event.getValue();
if (counter % 10 == 0) {
System.out.println("Heartbeat count: " + counter);
}
}
}
}2. 自定义配置属性处理器
java
package com.example.ilford.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Component
@RefreshScope
@ConfigurationProperties(prefix = "custom.config")
public class CustomConfigProperties {
private String serviceUrl;
private int timeout;
private boolean enabled;
// getters and setters
public String getServiceUrl() {
return serviceUrl;
}
public void setServiceUrl(String serviceUrl) {
this.serviceUrl = serviceUrl;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void refreshConfig() {
System.out.println("Configuration refreshed: " + serviceUrl);
}
}配置示例
1. 服务消费者配置
application.yml:
yaml
server:
port: 8080
spring:
application:
name: ilford-consumer
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.port}
loadbalancer:
ribbon:
enabled: false
cache:
enabled: true
feign:
client:
config:
default:
connectTimeout: 10000
readTimeout: 30000
httpclient:
enabled: false
okhttp:
enabled: true
compression:
request:
enabled: true
mime-types: text/xml,application/xml,application/json
min-request-size: 1024
response:
enabled: true
resilience4j:
circuitbreaker:
instances:
backendA:
sliding-window-size: 10
failure-rate-threshold: 50
wait-duration-in-open-state: 1000ms
retry:
instances:
backendA:
max-attempts: 3
wait-duration: 1000ms
management:
endpoints:
web:
exposure:
include: health,info,metrics,circuitbreakers,refresh
endpoint:
health:
show-details: when-authorized
metrics:
export:
prometheus:
enabled: true
logging:
level:
org.springframework.cloud: DEBUG
org.springframework.web: DEBUG2. 服务提供者配置
application.yml:
yaml
server:
port: 8081
spring:
application:
name: user-service
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.port}
management:
endpoints:
web:
exposure:
include: health,info,metrics
endpoint:
health:
show-details: when-authorized
logging:
level:
com.example: INFO参考资源
版本历史
- 2020.0.0-M1 (2020-06-18): 初始里程碑版本
- 2020.0.0-RC1 (2020-09-24): 第一个候选发布版本
- 2020.0.0 (2020-11-12): 正式发布版本 (Ilford)
- 2020.0.1-2020.0.6 (2020-12-17 至 2021-09-08): 各个小版本修复更新