예제 #1
0
        public static string Of(Instance instance)
        {
            Preconditions.NotNull(instance, "instance");
            String groupId = instance.GroupId;

            groupId = ServiceGroups.IsDefaultGroupId(groupId) ? ServiceGroups.DEFAULT_GROUP_ID : groupId;

            return(Of(instance.ServiceId, instance.RegionId, instance.ZoneId, groupId, instance.InstanceId));
        }
예제 #2
0
 public static RouteRule newDefaultRouteRule(List <Instance> instances)
 {
     return(new RouteRule()
     {
         RouteId = DEFAULT_ROUTE_RULE,
         Groups = new List <ServiceGroup>()
         {
             new ServiceGroup()
             {
                 GroupKey = DEFAULT_GROUP_KEY,
                 Weight = ServiceGroups.FixWeight(null),
                 Instances = instances
             }
         },
         Strategy = DEFAULT_ROUTE_STRATEGY
     });
 }
예제 #3
0
        public static List <RouteRule> NewRouteRules(this Service service)
        {
            List <RouteRule> routeRules = FilterRouteRules(service.RouteRules);
            Dictionary <string, Instance> groupKey2Instance   = GenerateGroupKey2Instances(service.Instances);
            Dictionary <string, Instance> instanceId2Instance = GenerateInstanceId2Instance(service.Instances, service.LogicInstances);

            List <RouteRule> newRouteRules = new List <RouteRule>();
            HashSet <string> routeRuleIds  = new HashSet <string>();

            foreach (RouteRule routeRule in routeRules)
            {
                List <ServiceGroup> nonemptyServiceGroups = new List <ServiceGroup>();
                foreach (ServiceGroup serviceGroup in routeRule.Groups)
                {
                    if (serviceGroup == null || string.IsNullOrWhiteSpace(serviceGroup.GroupKey))
                    {
                        continue;
                    }
                    string          groupKey  = serviceGroup.GroupKey.ToLower();
                    List <Instance> instances = GenerateGroupInstances(serviceGroup, instanceId2Instance, groupKey2Instance);
                    if (IsCanaryRouteRule(routeRule))
                    {
                        Dictionary <String, Instance> maps = new Dictionary <string, Instance>();
                        foreach (Instance instance in instances)
                        {
                            maps[instance.InstanceId] = instance;
                        }
                        foreach (Instance instance in FilterInstances(serviceGroup.Instances))
                        {
                            if (!maps.ContainsKey(instance.InstanceId))
                            {
                                instances.Add(instance);
                            }
                        }
                    }

                    if (Conditions.IsNullOrEmpty(instances))
                    {
                        continue;
                    }

                    ServiceGroup nonemptyServiceGroup = new ServiceGroup()
                    {
                        GroupKey  = serviceGroup.GroupKey,
                        Weight    = ServiceGroups.FixWeight(serviceGroup.Weight),
                        Instances = instances,
                        Metadata  = serviceGroup.Metadata == null ? null : new Dictionary <string, string>(serviceGroup.Metadata)
                    };

                    nonemptyServiceGroups.Add(nonemptyServiceGroup);
                }

                routeRuleIds.Add(routeRule.RouteId.ToLower());
                newRouteRules.Add(new RouteRule()
                {
                    RouteId  = routeRule.RouteId,
                    Groups   = nonemptyServiceGroups,
                    Strategy = routeRule.Strategy
                });
            }

            if (!routeRuleIds.Contains(DEFAULT_ROUTE_RULE))
            {
                newRouteRules.Add(newDefaultRouteRule(groupKey2Instance.Values.ToList()));
            }

            return(newRouteRules);
        }