重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
spring-cloud by teachingai/full-stack-skills
npx skills add https://github.com/teachingai/full-stack-skills --skill spring-cloudSpring Cloud 是一套完整的微服务解决方案,提供了服务注册与发现、配置管理、网关、负载均衡、熔断器等组件。
Eureka Server :
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
application.yml :
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Eureka Client :
@SpringBootApplication
@EnableEurekaClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
application.yml :
spring:
application:
name: user-service
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
Config Server :
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
application.yml :
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/example/config-repo
search-paths: '{application}'
Config Client :
spring:
application:
name: user-service
cloud:
config:
uri: http://localhost:8888
name: user-service
profile: dev
Gateway 配置 :
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
application.yml :
spring:
cloud:
gateway:
routes:
- id: user-service
uri: lb://user-service
predicates:
- Path=/api/users/**
filters:
- StripPrefix=1
- id: order-service
uri: lb://order-service
predicates:
- Path=/api/orders/**
filters:
- StripPrefix=1
路由配置类 :
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("user-service", r -> r
.path("/api/users/**")
.uri("lb://user-service"))
.route("order-service", r -> r
.path("/api/orders/**")
.uri("lb://order-service"))
.build();
}
}
使用 RestTemplate :
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
使用 WebClient :
@Configuration
public class WebClientConfig {
@Bean
@LoadBalanced
public WebClient.Builder webClientBuilder() {
return WebClient.builder();
}
}
服务调用 :
@Service
public class OrderService {
private final RestTemplate restTemplate;
public OrderService(@LoadBalanced RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public User getUser(Long userId) {
return restTemplate.getForObject(
"http://user-service/api/users/{id}",
User.class,
userId
);
}
}
依赖 :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
配置 :
resilience4j:
circuitbreaker:
instances:
userService:
registerHealthIndicator: true
slidingWindowSize: 10
failureRateThreshold: 50
waitDurationInOpenState: 10000
使用 :
@Service
public class OrderService {
private final CircuitBreaker circuitBreaker;
private final RestTemplate restTemplate;
public OrderService(
CircuitBreakerRegistry circuitBreakerRegistry,
RestTemplate restTemplate
) {
this.circuitBreaker = circuitBreakerRegistry.circuitBreaker("userService");
this.restTemplate = restTemplate;
}
public User getUser(Long userId) {
return circuitBreaker.executeSupplier(() ->
restTemplate.getForObject(
"http://user-service/api/users/{id}",
User.class,
userId
)
);
}
}
依赖 :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启用 Feign :
@SpringBootApplication
@EnableFeignClients
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
定义 Feign Client :
@FeignClient(name = "user-service")
public interface UserServiceClient {
@GetMapping("/api/users/{id}")
User getUserById(@PathVariable Long id);
@PostMapping("/api/users")
User createUser(@RequestBody User user);
}
使用 :
@Service
public class OrderService {
private final UserServiceClient userServiceClient;
public OrderService(UserServiceClient userServiceClient) {
this.userServiceClient = userServiceClient;
}
public Order createOrder(Long userId, Order order) {
User user = userServiceClient.getUserById(userId);
// 创建订单逻辑
return order;
}
}
依赖 :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
配置 :
spring:
sleuth:
sampler:
probability: 1.0
microservices/
├── eureka-server/ # 服务注册中心
├── config-server/ # 配置中心
├── gateway/ # API 网关
├── user-service/ # 用户服务
├── order-service/ # 订单服务
└── product-service/ # 商品服务
同步调用(Feign) :
@FeignClient(name = "user-service")
public interface UserServiceClient {
@GetMapping("/api/users/{id}")
User getUserById(@PathVariable Long id);
}
异步调用(消息队列) :
@Service
public class OrderService {
private final RabbitTemplate rabbitTemplate;
public void publishOrderEvent(Order order) {
rabbitTemplate.convertAndSend("order.exchange", "order.created", order);
}
}
<!-- Eureka Client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Circuit Breaker -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
Weekly Installs
63
Repository
GitHub Stars
226
First Seen
Jan 24, 2026
Security Audits
Installed on
opencode59
gemini-cli57
codex56
github-copilot53
cursor52
amp47
Spring Boot JWT安全认证:Spring Security 6.x与JJWT实现无状态API保护
543 周安装