Spring Cloud 2025 使用体验
架构
- 注册中心 Spring Cloud Netflix Eureka/Spring Cloud Consul
- 配置中心 Spring Cloud Config/Spring Cloud Consul
- 网关 Spring Cloud Gateway
- 链路监控 zipkin
Spring Cloud Alibaba Nacos 暂未适配
体验一
- Spring Cloud Netflix Eureka
- Spring Cloud Config
- Spring Cloud Gateway
Spring Cloud Netflix Eureka
相关依赖
xml
<dependencies>
<!-- 服务信息监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Eureka 注册中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>服务参数
application.yml
yaml
spring:
application:
# 注册的名称
name: eureka-server
server:
# eureka服务默认端口建议不要修改
port: 8761
eureka:
instance:
appname: ${spring.application.name} #默认 应用名称
environment: test # 默认 test启动配置
java
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}启动即可 无需其他参数
Spring Cloud Config
相关依赖
xml
<dependencies>
<!-- 配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- redis驱动:配置存储位置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 监控信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- eureka 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>服务参数
yaml
spring:
application:
name: configserver
cloud:
config:
discovery:
enabled: true
data:
redis:
host: localhost
port: 6379
profiles:
active: redis #激活redis环境 启用redis 存储数据
server:
port: 8888 #默认配置中心端口启动配置
java
@EnableConfigServer //开启配置中心
@EnableDiscoveryClient // 服务注册
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}配置存储
git(default)
yaml
spring:
cloud:
config:
server:
git:
uri: http or ssh #仓库地址 file:///d:/test 也支持
default-label: master # 默认分支 main master
# host-key: ---
# host-key-algorithm: ssh-rsa
# private-key: ---
username: ??
password: ??仓库创建文件app-dev.yml {应用名称}-{环境名称}.yml label 分支
filesystem(native)
yaml
spring:
cloud:
config:
server:
native:
search-locations: file:D:/test # classpath:/ ...等格式
add-label-locations: false #关闭label 文件夹
profiles:
active: native文件夹下创建 ~/app-dev.yml {应用名称}-{激活环境}.yml ~/dev/app-dev.yml
{label}/{应用名称}-{激活环境}.yml
redis
xml
<!--redis 驱动-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>yaml
spring:
data:
redis:
hostname: localhost
port: 6379
password: #
profiles:
active: redis配置采用hash存储
bash
HMSET sample-app server.port "8100" sample.topic.name "test" test.property1 "property1"
HMSET {app}-{profile}jdbc
xml
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--数据库驱动 MySQL 可以使用其他 PG ...-->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>yaml
spring:
cloud:
config:
server:
jdbc:
# sql SELECT "KEY", "VALUE" from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
# 根据sql 创建表 注意列名
sql: SELECT config_key, config_value from prop where APPLICATION=? and PROFILE=? and LABEL=?
# SELECT "KEY", "VALUE" from PROPERTIES where APPLICATION=? and PROFILE is null and LABEL=?
sql-without-profile: SELECT config_key, config_value from prop where APPLICATION=? and PROFILE is null and LABEL=?
profiles:
active: jdbclabel 默认 master 表结构存储 保存相应 key value profile application label 就好