【网关、RPC和Feign的成套示例代码】
网关、RPC和Feign的成套示例代码
分别给出基于Spring Boot/Cloud的网关、RPC(使用Dubbo)和Feign的完整示例代码。
1. Spring Cloud Gateway 网关示例
网关服务 (gateway-service)
application.yml
server:
port: 8080
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
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
GatewayApplication.java
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
2. Dubbo RPC 示例
服务提供者 (user-service-provider)
pom.xml 添加依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.15</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.15</version>
</dependency>
application.yml
dubbo:
application:
name: user-service-provider
registry:
address: nacos://localhost:8848
protocol:
name: dubbo
port: 20880
服务接口定义
public interface UserService {
User getUserById(Long id);
}
@Data
public class User implements Serializable {
private Long id;
private String name;
private String email;
}
服务实现
@Service
public class UserServiceImpl implements UserService {
@Override
public User getUserById(Long id) {
// 模拟数据库查询
User user = new User();
user.setId(id);
user.setName("User" + id);
user.setEmail("user" + id + "@example.com");
return user;
}
}
服务消费者 (order-service-consumer)
调用服务
@RestController
@RequestMapping("/orders")
public class OrderController {
@Reference
private UserService userService;
@GetMapping("/{id}/user")
public User getUserByOrderId(@PathVariable Long id) {
// 通过RPC调用用户服务
return userService.getUserById(id);
}
}
3. Feign 客户端示例
服务提供者 (user-service)
UserController.java
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
User user = new User();
user.setId(id);
user.setName("User" + id);
user.setEmail("user" + id + "@example.com");
return user;
}
}
服务消费者 (order-service)
pom.xml 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
application.yml
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
Feign客户端接口
@FeignClient(name = "user-service")
public interface UserServiceClient {
@GetMapping("/users/{id}")
User getUserById(@PathVariable Long id);
}
使用Feign客户端
@RestController
@RequestMapping("/orders")
public class OrderController {
@Autowired
private UserServiceClient userServiceClient;
@GetMapping("/{id}/user")
public User getUserByOrderId(@PathVariable Long id) {
// 通过Feign调用用户服务
return userServiceClient.getUserById(id);
}
}
启动类添加注解
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class OrderServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OrderServiceApplication.class, args);
}
}
总结对比
-
网关:
- 适合作为系统统一入口
- 配置路由规则即可,无需编码
- 示例展示了基于路径的路由配置
-
Dubbo RPC:
- 需要定义服务接口并添加@Reference引用
- 性能高,适合内部高性能调用
- 需要单独的注册中心(Nacos)
-
Feign:
- 声明式REST客户端
- 与Spring Cloud生态集成好
- 基于HTTP,适合RESTful风格服务
内部服务调用通常推荐使用Feign或Dubbo而不是通过网关中转。

浙公网安备 33010602011771号