Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions grpc-communication/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Seata GRPC Communication Samples

This directory contains samples demonstrating GRPC communication between Seata clients and Seata server.

## Overview

These samples showcase different aspects of GRPC communication in Seata, from basic connectivity to advanced features like load balancing, streaming, and monitoring.

## Directory Structure

```
grpc-communication/
├── basic/ # Basic GRPC communication examples
│ ├── client/ # Simple GRPC client
│ └── config/ # Configuration files
├── advanced/ # Advanced GRPC features (TODO)
│ ├── load-balancing/ # Load balancing strategies
│ ├── monitoring/ # Monitoring and metrics
│ └── streaming/ # Streaming communication
├── client-server/ # Full client-server examples (TODO)
│ ├── client/ # Advanced client implementations
│ ├── server/ # Server-side examples
│ └── proto/ # Protocol definitions
└── docs/ # Documentation and guides
```

## Current Status

### ✅ Implemented
- **Basic GRPC Client**: Demonstrates fundamental GRPC communication setup
- **Configuration Templates**: Shows how to configure GRPC protocol
- **Project Structure**: Complete directory layout for future expansion

### 🚧 Planned (TODO - Waiting for GRPC feature merge)
- **Advanced Load Balancing**: Multiple load balancing strategies
- **Streaming Communication**: Bidirectional streaming examples
- **Connection Pooling**: Efficient connection management
- **Monitoring & Metrics**: Performance monitoring examples
- **Full Client-Server**: Complete end-to-end examples

## Quick Start

### Basic GRPC Communication

1. **Start Seata Server** with GRPC support:
```bash
# Ensure Seata server is configured for GRPC protocol
# Default GRPC port: 8091
```

2. **Run the basic client**:
```bash
cd basic/client
go run main.go -config=../config/seata-grpc.yml
```

### Configuration

The sample uses GRPC protocol configuration in `seata-grpc.yml`:

```yaml
seata:
transport:
protocol: "grpc" # Enable GRPC communication
service:
grouplist:
default: "127.0.0.1:8091" # Seata server GRPC endpoint
```

## Development Notes

### For Contributors

When the new GRPC features are merged into seata-go, update these areas:

1. **Enable local development**:
```bash
# Uncomment in go.mod for local testing:
replace seata.apache.org/seata-go => ../seata-go
```

2. **Implement advanced features**:
- Remove TODO comments
- Add actual implementations in advanced/ directories
- Update configuration examples

3. **Add new samples**:
- Follow the established pattern
- Include configuration files
- Add documentation

### Current Limitations

- Advanced GRPC features are not yet available in the current seata-go version
- Some configuration options are placeholders for future implementation
- Full streaming and monitoring examples are pending

## Related PRs

- [Link to GRPC implementation PR] (TODO: Add when available)
- [Link to load balancing PR] (TODO: Add when available)
- [Link to monitoring PR] (TODO: Add when available)

## Contributing

When adding new GRPC communication samples:

1. Follow the existing directory structure
2. Include comprehensive configuration examples
3. Add clear documentation and comments
4. Ensure compatibility with the current seata-go version
5. Mark future features clearly with TODO comments

## Requirements

- Go 1.20+
- Seata Server with GRPC support
- seata-go library (see go.mod for version)

## License

Licensed under the Apache License, Version 2.0.
55 changes: 55 additions & 0 deletions grpc-communication/advanced/load-balancing/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Package main demonstrates GRPC load balancing strategies with Seata server
package main

import (
"context"
"fmt"

"seata.apache.org/seata-go/pkg/client"
"seata.apache.org/seata-go/pkg/tm"
)

// TODO: Implement when GRPC load balancing features are merged
// This sample will demonstrate:
// 1. Round-robin load balancing
// 2. Weighted load balancing
// 3. Consistent hash load balancing
// 4. Health-check based load balancing

func main() {
fmt.Println("=== GRPC Load Balancing Sample ===")
fmt.Println("TODO: Implement advanced load balancing strategies")

// Placeholder initialization
client.Init()

// TODO: Demonstrate different load balancing strategies
err := tm.WithGlobalTx(context.Background(), &tm.GtxConfig{
Name: "LoadBalancingSample",
}, func(ctx context.Context) error {
// TODO: Show load balancing in action
fmt.Println("Demonstrating load balancing across multiple Seata servers")
return nil
})

if err != nil {
fmt.Printf("Load balancing sample failed: %v\n", err)
}
}
82 changes: 82 additions & 0 deletions grpc-communication/basic/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Package main demonstrates basic GRPC communication with Seata server
package main

import (
"context"
"flag"
"fmt"
"log"
"time"

"seata.apache.org/seata-go/pkg/client"
"seata.apache.org/seata-go/pkg/tm"
"seata.apache.org/seata-go/pkg/util/log"
)

var (
configPath = flag.String("config", "../config/seata-grpc.yml", "config file path")
)

func main() {
flag.Parse()

fmt.Println("=== Seata GRPC Communication Basic Client Sample ===")
fmt.Println("This sample demonstrates basic GRPC communication between client and Seata server")

// Initialize seata client with GRPC protocol
// The configuration will specify GRPC as the communication protocol
client.InitPath(*configPath)
log.Info("Seata client initialized with GRPC protocol")

// TODO: Once new GRPC features are merged, this will demonstrate:
// 1. Direct GRPC connection to Seata server
// 2. Enhanced GRPC communication features
// 3. Improved performance and reliability

// For now, demonstrate basic global transaction that works with current version
err := tm.WithGlobalTx(context.Background(), &tm.GtxConfig{
Name: "BasicGrpcCommunicationSample",
Timeout: 30 * time.Second,
}, func(ctx context.Context) error {
// This transaction will use the configured GRPC protocol to communicate with Seata server
fmt.Printf("Global transaction started with XID: %s\n", tm.GetXID(ctx))
fmt.Println("Communication protocol: GRPC")

// TODO: Add more sophisticated GRPC communication examples once new features are available:
// - Streaming communication
// - Load balancing
// - Connection pooling
// - Monitoring and metrics

// Simulate business logic
fmt.Println("Executing business logic...")
time.Sleep(1 * time.Second)
fmt.Println("Business logic completed successfully")

return nil
})

if err != nil {
log.Fatalf("Global transaction failed: %v", err)
}

fmt.Println("✓ Basic GRPC communication sample completed successfully")
fmt.Println("✓ This demonstrates the foundation for advanced GRPC features")
}
83 changes: 83 additions & 0 deletions grpc-communication/basic/config/seata-grpc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Seata GRPC Communication Configuration
# This configuration demonstrates how to enable GRPC protocol for Seata communication

seata:
enabled: true
application-id: "grpc-communication-sample"
tx-service-group: "grpc_tx_group"
enable-auto-data-source-proxy: false

# Transport configuration - GRPC protocol
transport:
# Set protocol to grpc to enable GRPC communication
protocol: "grpc"
server: "NIO"
heartbeat: true
enableClientBatchSendRequest: false
threadFactory:
bossThreadPrefix: "NettyBoss"
workerThreadPrefix: "NettyServerNIOWorker"
serverExecutorThreadPrefix: "NettyServerBizHandler"
shareBossWorker: false
clientSelectorThreadPrefix: "NettyClientSelector"
clientSelectorThreadSize: 1
clientWorkerThreadPrefix: "NettyClientWorkerThread"
bossThreadSize: 1
workerThreadSize: "default"
shutdown:
wait: 3
serialization: "seata"
compressor: "none"

# Service discovery configuration
service:
vgroup-mapping:
grpc_tx_group: "default"
grouplist:
default: "127.0.0.1:8091"
enable-degrade: false
disable-global-transaction: false

# Registry configuration
registry:
type: "file"
file:
name: "file.conf"

# Client configuration
client:
rm:
async-commit-buffer-limit: 10000
lock:
retry-interval: 10
retry-times: 30
retry-policy-branch-rollback-on-conflict: true
tm:
commit-retry-count: 5
rollback-retry-count: 5
default-global-transaction-timeout: 60000
degrade-check: false
degrade-check-period: 2000
degrade-check-allow-times: 10
undo:
data-validation: true
log-serialization: "jackson"
log-table: "undo_log"
only-care-update-columns: true
load-balance:
type: "RandomLoadBalance"
virtual-nodes: 10

# TODO: Enhanced GRPC-specific configurations will be added here
# when new GRPC features are merged:
# grpc:
# connection-pool:
# max-connections: 10
# min-connections: 1
# load-balancing:
# strategy: "round_robin"
# streaming:
# enabled: true
# monitoring:
# enabled: true
# metrics-port: 9090