|
| 1 | +--- |
| 2 | +outline: deep |
| 3 | +--- |
| 4 | + |
| 5 | +# Advanced Topics |
| 6 | + |
| 7 | +This section covers advanced Continuum patterns and configurations for complex architectures. |
| 8 | + |
| 9 | +## Multi-Service Architecture |
| 10 | + |
| 11 | +Continuum excels at building distributed systems with multiple separate Java microservices. Each service is a separate Spring Boot application that can communicate with other services directly. |
| 12 | + |
| 13 | +### Architecture Overview |
| 14 | + |
| 15 | +In a multi-service architecture: |
| 16 | +- Each service is a separate Spring Boot application |
| 17 | +- Services communicate directly with each other (peer-to-peer) |
| 18 | +- Gateway is typically embedded in one service or runs as a separate service for frontend access |
| 19 | +- Services can be deployed independently and scaled separately |
| 20 | + |
| 21 | +### CoolCommerce Example |
| 22 | + |
| 23 | +The [CoolCommerce](https://github.com/Kinotic-Foundation/continuum-examples/tree/main/CoolCommerce) project demonstrates a complete multi-service architecture: |
| 24 | + |
| 25 | +**Service Structure:** |
| 26 | +- `ecommerce-main`: Main store functionality (`StoreService`) |
| 27 | +- `ecommerce-payment`: Payment processing (`MerchantServices`) |
| 28 | +- `ecommerce-gateway`: Separate Gateway server for frontend access |
| 29 | +- `ecommerce-frontend`: TypeScript/Vue frontend |
| 30 | + |
| 31 | +**Each Service:** |
| 32 | +```java |
| 33 | +// ecommerce-main/src/main/groovy/.../EcommerceMainApplication.groovy |
| 34 | +@SpringBootApplication |
| 35 | +@EnableContinuum // Service-to-service communication |
| 36 | +class EcommerceMainApplication { |
| 37 | + // ... |
| 38 | +} |
| 39 | + |
| 40 | +// ecommerce-payment/src/main/groovy/.../EcommercePaymentApplication.groovy |
| 41 | +@SpringBootApplication |
| 42 | +@EnableContinuum // Service-to-service communication |
| 43 | +class EcommercePaymentApplication { |
| 44 | + // ... |
| 45 | +} |
| 46 | + |
| 47 | +// ecommerce-gateway/src/main/groovy/.../EcommerceGatewayApplication.groovy |
| 48 | +@SpringBootApplication |
| 49 | +@EnableContinuum |
| 50 | +@EnableContinuumGateway // Gateway for frontend access |
| 51 | +class EcommerceGatewayApplication { |
| 52 | + // ... |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### Service-to-Service Communication |
| 57 | + |
| 58 | +Services communicate by sharing interface definitions and using `@Proxy`: |
| 59 | + |
| 60 | +**Shared Interface** (in `ecommerce-payment-api` module): |
| 61 | +```java |
| 62 | +@Publish |
| 63 | +@Proxy // Makes this available to other services |
| 64 | +public interface MerchantServices { |
| 65 | + Mono<UUID> processPayment(CustomerInfo info, PaymentInfo payment, BigDecimal amount); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +**Service Implementation** (in `ecommerce-payment`): |
| 70 | +```java |
| 71 | +@Component |
| 72 | +public class DefaultMerchantServices implements MerchantServices { |
| 73 | + // Implementation |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +**Service Consumer** (in `ecommerce-main`): |
| 78 | +```java |
| 79 | +@Component |
| 80 | +public class DefaultStoreService implements StoreService { |
| 81 | + |
| 82 | + // Inject the other service - Continuum creates a proxy automatically |
| 83 | + private final MerchantServices merchantServices; |
| 84 | + |
| 85 | + public DefaultStoreService(MerchantServices merchantServices) { |
| 86 | + this.merchantServices = merchantServices; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Mono<UUID> checkout(CheckoutInfo checkoutInfo) { |
| 91 | + // Call the remote service as if it were local |
| 92 | + return merchantServices.processPayment(...); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Gateway Deployment Options |
| 98 | + |
| 99 | +**Option 1: Embedded Gateway** (like Structures) |
| 100 | +```java |
| 101 | +@SpringBootApplication |
| 102 | +@EnableContinuum |
| 103 | +@EnableContinuumGateway // Gateway embedded in main service |
| 104 | +public class MyApplication { |
| 105 | + // Frontend connects to this service |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +**Option 2: Separate Gateway** (like CoolCommerce) |
| 110 | +```java |
| 111 | +// Gateway service |
| 112 | +@SpringBootApplication |
| 113 | +@EnableContinuum |
| 114 | +@EnableContinuumGateway |
| 115 | +public class GatewayApplication { |
| 116 | + // Frontend connects here |
| 117 | +} |
| 118 | + |
| 119 | +// Other services |
| 120 | +@SpringBootApplication |
| 121 | +@EnableContinuum // No Gateway - services only |
| 122 | +public class MainServiceApplication { |
| 123 | + // Services communicate directly, no Gateway needed |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### When to Use Multi-Service Architecture |
| 128 | + |
| 129 | +Use this pattern when: |
| 130 | +- You need to scale services independently |
| 131 | +- Different services have different responsibilities |
| 132 | +- Teams want to deploy services separately |
| 133 | +- You need service isolation for security or compliance |
| 134 | +- You want to use different technologies per service (future: different languages) |
| 135 | + |
| 136 | +### Deployment Considerations |
| 137 | + |
| 138 | +- **Service Discovery**: Continuum handles automatic service discovery through clustering |
| 139 | +- **Independent Deployment**: Each service can be deployed/updated independently |
| 140 | +- **Shared Interfaces**: Services need access to interface definitions (shared library/module) |
| 141 | +- **Gateway Access**: Frontend needs to connect through Gateway (embedded or separate) |
| 142 | + |
| 143 | +## Security and Participant Context |
| 144 | + |
| 145 | +Continuum provides security context through the `Participant` interface. |
| 146 | + |
| 147 | +### Participant Parameter |
| 148 | + |
| 149 | +Services can receive security context via `Participant`: |
| 150 | + |
| 151 | +```java |
| 152 | +@Publish |
| 153 | +public interface SecureService { |
| 154 | + List<Data> getData(Participant participant); |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +The `Participant` contains: |
| 159 | +- User identification |
| 160 | +- Tenant information (for multi-tenancy) |
| 161 | +- Roles and permissions |
| 162 | +- Additional metadata |
| 163 | + |
| 164 | +### Participant Injection |
| 165 | + |
| 166 | +The framework automatically injects `Participant` based on the current security context: |
| 167 | + |
| 168 | +```java |
| 169 | +@Component |
| 170 | +public class DefaultSecureService implements SecureService { |
| 171 | + |
| 172 | + @Override |
| 173 | + public List<Data> getData(Participant participant) { |
| 174 | + // Use participant for authorization, tenant filtering, etc. |
| 175 | + String tenantId = participant.getTenantId(); |
| 176 | + Set<String> roles = participant.getRoles(); |
| 177 | + |
| 178 | + return dataRepository.findByTenantAndRoles(tenantId, roles); |
| 179 | + } |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +### Security Best Practices |
| 184 | + |
| 185 | +- Always validate participant context in service methods |
| 186 | +- Use tenant isolation for multi-tenant applications |
| 187 | +- Check roles/permissions before performing operations |
| 188 | +- Never trust client-provided security information—always use the injected `Participant` |
| 189 | + |
| 190 | +## Error Handling Strategies |
| 191 | + |
| 192 | +### Service-Level Error Handling |
| 193 | + |
| 194 | +Handle errors in your service implementations: |
| 195 | + |
| 196 | +```java |
| 197 | +@Component |
| 198 | +public class DefaultStoreService implements StoreService { |
| 199 | + |
| 200 | + @Override |
| 201 | + public Product getProduct(Long productId) { |
| 202 | + return productRepository.findById(productId) |
| 203 | + .orElseThrow(() -> new ProductNotFoundException(productId)); |
| 204 | + } |
| 205 | +} |
| 206 | +``` |
| 207 | + |
| 208 | +### Reactive Error Handling |
| 209 | + |
| 210 | +For reactive types, handle errors in the reactive chain: |
| 211 | + |
| 212 | +```java |
| 213 | +@Override |
| 214 | +public Mono<UUID> checkout(CheckoutInfo checkoutInfo) { |
| 215 | + return merchantServices.processPayment(...) |
| 216 | + .onErrorMap(PaymentException.class, e -> new CheckoutException("Payment failed", e)) |
| 217 | + .doOnError(e -> logger.error("Checkout failed", e)); |
| 218 | +} |
| 219 | +``` |
| 220 | + |
| 221 | +### Client-Side Error Handling |
| 222 | + |
| 223 | +Clients should always handle errors: |
| 224 | + |
| 225 | +```typescript |
| 226 | +try { |
| 227 | + const product = await storeService.invoke('getProduct', [productId]) |
| 228 | + // Use product |
| 229 | +} catch (error) { |
| 230 | + if (error instanceof ProductNotFoundException) { |
| 231 | + // Handle not found |
| 232 | + } else { |
| 233 | + // Handle other errors |
| 234 | + } |
| 235 | +} |
| 236 | +``` |
| 237 | + |
| 238 | +### Async Operations |
| 239 | + |
| 240 | +Use reactive types (`Mono`, `Flux`) for non-blocking operations: |
| 241 | + |
| 242 | +```java |
| 243 | +@Publish |
| 244 | +public interface DataService { |
| 245 | + // Non-blocking async operation |
| 246 | + Mono<Result> processData(Data data); |
| 247 | +} |
| 248 | +``` |
| 249 | + |
| 250 | +## Extension Points |
| 251 | + |
| 252 | +Continuum is designed with extension points for advanced use cases: |
| 253 | + |
| 254 | +- **Custom Transports**: Implement custom transport layers (future) |
| 255 | +- **Custom Serialization**: Add support for additional serialization formats (future) |
| 256 | +- **Context Interceptors**: Intercept and modify request context |
| 257 | +- **Service Interceptors**: Add cross-cutting concerns to service calls |
| 258 | + |
| 259 | +## Summary |
| 260 | + |
| 261 | +Advanced Continuum patterns enable: |
| 262 | +- **Multi-service architectures** with independent deployment |
| 263 | +- **Secure, multi-tenant** applications with participant context |
| 264 | +- **High-performance** systems with clustering and async operations |
| 265 | +- **Testable** applications with comprehensive testing strategies |
| 266 | + |
| 267 | +## What's Next? |
| 268 | + |
| 269 | +- Check out [Examples](./examples) for complete working examples |
| 270 | +- See the [CoolCommerce project](https://github.com/Kinotic-Foundation/continuum-examples/tree/main/CoolCommerce) for a full multi-service implementation |
0 commit comments