forked from apache/dubbo-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
513 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package pixiu | ||
|
||
import ( | ||
"context" | ||
"dubbo.apache.org/dubbo-go/v3/common" | ||
"dubbo.apache.org/dubbo-go/v3/metadata/identifier" | ||
"dubbo.apache.org/dubbo-go/v3/metadata/report" | ||
"dubbo.apache.org/dubbo-go/v3/registry" | ||
"encoding/json" | ||
gxset "github.com/dubbogo/gost/container/set" | ||
"google.golang.org/grpc" | ||
"istio.io/api/dubbo/v1alpha1" | ||
) | ||
|
||
type pixiuMetadataReportFactory struct { | ||
} | ||
|
||
// CreateMetadataReport create a new metadata report | ||
func (mf *pixiuMetadataReportFactory) CreateMetadataReport(url *common.URL) report.MetadataReport { | ||
|
||
conn, err := grpc.Dial(url.Location) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
snpClient := v1alpha1.NewServiceNameMappingServiceClient(conn) | ||
|
||
metaClient := v1alpha1.NewServiceMetadataServiceClient(conn) | ||
|
||
return &pixiuMetadataReport{snpClient: snpClient, metaClient: metaClient} | ||
} | ||
|
||
type pixiuMetadataReport struct { | ||
snpClient v1alpha1.ServiceNameMappingServiceClient | ||
metaClient v1alpha1.ServiceMetadataServiceClient | ||
} | ||
|
||
func (p pixiuMetadataReport) StoreProviderMetadata(providerIdentifier *identifier.MetadataIdentifier, serviceDefinitions string) error { | ||
panic("implement me") | ||
} | ||
|
||
func (p pixiuMetadataReport) StoreConsumerMetadata(metadataIdentifier *identifier.MetadataIdentifier, s string) error { | ||
panic("implement me") | ||
} | ||
|
||
func (p pixiuMetadataReport) SaveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier, url *common.URL) error { | ||
panic("implement me") | ||
} | ||
|
||
func (p pixiuMetadataReport) RemoveServiceMetadata(metadataIdentifier *identifier.ServiceMetadataIdentifier) error { | ||
panic("implement me") | ||
} | ||
|
||
func (p pixiuMetadataReport) GetExportedURLs(metadataIdentifier *identifier.ServiceMetadataIdentifier) ([]string, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (p pixiuMetadataReport) SaveSubscribedData(metadataIdentifier *identifier.SubscriberMetadataIdentifier, s string) error { | ||
panic("implement me") | ||
} | ||
|
||
func (p pixiuMetadataReport) GetSubscribedURLs(metadataIdentifier *identifier.SubscriberMetadataIdentifier) ([]string, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (p pixiuMetadataReport) GetServiceDefinition(metadataIdentifier *identifier.MetadataIdentifier) (string, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (p pixiuMetadataReport) GetAppMetadata(metadataIdentifier *identifier.SubscriberMetadataIdentifier) (*common.MetadataInfo, error) { | ||
response, err := p.metaClient.Get(context.Background(), &v1alpha1.GetServiceMetadataRequest{ | ||
Namespace: metadataIdentifier.Group, | ||
ApplicationName: "", | ||
Revision: metadataIdentifier.Revision, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
data := response.GetMetadataInfo() | ||
var metadataInfo common.MetadataInfo | ||
err = json.Unmarshal([]byte(data), &metadataInfo) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &metadataInfo, nil | ||
} | ||
|
||
func (p pixiuMetadataReport) PublishAppMetadata(metadataIdentifier *identifier.SubscriberMetadataIdentifier, info *common.MetadataInfo) error { | ||
|
||
data, err := json.Marshal(info) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = p.metaClient.Publish(context.Background(), &v1alpha1.PublishServiceMetadataRequest{ | ||
Namespace: metadataIdentifier.Group, | ||
ApplicationName: metadataIdentifier.Application, | ||
Revision: metadataIdentifier.Revision, | ||
MetadataInfo: string(data), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return err | ||
} | ||
|
||
func (p pixiuMetadataReport) RegisterServiceAppMapping(serviceInterface string, group string, appName string) error { | ||
_, err := p.snpClient.RegisterServiceAppMapping(context.Background(), &v1alpha1.ServiceMappingRequest{ | ||
Namespace: group, | ||
ApplicationName: appName, | ||
InterfaceNames: []string{serviceInterface}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (p pixiuMetadataReport) GetServiceAppMapping(key string, group string, listener registry.MappingListener) (*gxset.HashSet, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (p pixiuMetadataReport) RemoveServiceAppMappingListener(key string, group string) error { | ||
panic("implement me") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package xds | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" | ||
_struct "github.com/golang/protobuf/ptypes/struct" | ||
"time" | ||
|
||
envoy_service_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
"istio.io/api/dubbo/v1alpha1" | ||
"log" | ||
"testing" | ||
) | ||
|
||
const ( | ||
xdsServerAddress = "localhost:15010" // 将此地址替换为实际的xDS服务器地址 | ||
) | ||
|
||
func sendXdsRequest(stream envoy_service_discovery_v3.AggregatedDiscoveryService_StreamAggregatedResourcesClient, resourceType, resourceName string, node *envoy_config_core_v3.Node) error { | ||
req := &envoy_service_discovery_v3.DiscoveryRequest{ | ||
TypeUrl: resourceType, | ||
ResourceNames: []string{resourceName}, | ||
ResponseNonce: time.Now().String(), | ||
Node: node, | ||
} | ||
|
||
return stream.Send(req) | ||
} | ||
func TestXDS(t *testing.T) { | ||
conn, err := grpc.Dial(xdsServerAddress, grpc.WithInsecure()) | ||
if err != nil { | ||
log.Fatalf("Failed to connect to xDS server: %v", err) | ||
} | ||
defer conn.Close() | ||
|
||
adsClient := envoy_service_discovery_v3.NewAggregatedDiscoveryServiceClient(conn) | ||
stream, err := adsClient.StreamAggregatedResources(context.Background()) | ||
if err != nil { | ||
log.Fatalf("Failed to open ADS stream: %v", err) | ||
} | ||
//sidecar~ip~{POD_NAME}~{NAMESPACE_NAME}.svc.cluster.local | ||
node := &envoy_config_core_v3.Node{ | ||
Id: "sidecar~127.0.0.1~xds_client~default.svc.cluster.local", | ||
Cluster: "default", | ||
Metadata: &_struct.Struct{ | ||
Fields: map[string]*structpb.Value{ | ||
"env": { | ||
Kind: &structpb.Value_StringValue{ | ||
StringValue: "test", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
//发送请求 | ||
err = sendXdsRequest(stream, "dubbo.networking.v1alpha1.v1.servicenamemapping", "a|default", node) | ||
if err != nil { | ||
log.Fatalf("Failed to send Listener request: %v", err) | ||
} | ||
|
||
//err = sendXdsRequest(stream, resource.ClusterType, "cluster", node) | ||
//if err != nil { | ||
// log.Fatalf("Failed to send Cluster request: %v", err) | ||
//} | ||
|
||
// 接收响应 | ||
for { | ||
resp, err := stream.Recv() | ||
if err != nil { | ||
log.Fatalf("Failed to receive xDS response: %v", err) | ||
} | ||
log.Printf("Received xDS response: %v", resp) | ||
} | ||
} | ||
|
||
func TestClient(t *testing.T) { | ||
|
||
dial, err := grpc.Dial("127.0.0.1:15010", grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
|
||
fmt.Println(dial) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
client := v1alpha1.NewServiceNameMappingServiceClient(dial) | ||
|
||
mapping, err := client.RegisterServiceAppMapping(context.Background(), &v1alpha1.ServiceMappingRequest{ | ||
Namespace: "default", | ||
ApplicationName: "application-05", | ||
InterfaceNames: []string{"a"}, | ||
}) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
fmt.Println("res:", mapping) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.