@Overrideprotected LinkedHashMap<String, ZuulRoute>locateRoutes(){
LinkedHashMap<String, ZuulRoute> routesMap =newLinkedHashMap<>();
routesMap.putAll(super.locateRoutes());if(this.discovery != null){
Map<String, ZuulRoute> staticServices =newLinkedHashMap<>();for(ZuulRoute route : routesMap.values()){
String serviceId = route.getServiceId();if(serviceId == null){
serviceId = route.getId();}if(serviceId != null){
staticServices.put(serviceId, route);}}// Add routes for discovery services by default
List<String> services =this.discovery.getServices();
String[] ignored =this.properties.getIgnoredServices().toArray(newString[0]);for(String serviceId : services){// Ignore specifically ignored services and those that were manually// configured
String key ="/"+mapRouteToService(serviceId)+"/**";if(staticServices.containsKey(serviceId)&& staticServices.get(serviceId).getUrl()== null){// Explicitly configured with no URL, cannot be ignored// all static routes are already in routesMap
// Update location using serviceId if location is null
ZuulRoute staticRoute = staticServices.get(serviceId);if(!StringUtils.hasText(staticRoute.getLocation())){
staticRoute.setLocation(serviceId);}}if(!PatternMatchUtils.simpleMatch(ignored, serviceId)&&!routesMap.containsKey(key)){// Not ignored
routesMap.put(key,newZuulRoute(key, serviceId));}}}if(routesMap.get(DEFAULT_ROUTE)!= null){
ZuulRoute defaultRoute = routesMap.get(DEFAULT_ROUTE);// Move the defaultServiceId to the end
routesMap.remove(DEFAULT_ROUTE);
routesMap.put(DEFAULT_ROUTE, defaultRoute);}
LinkedHashMap<String, ZuulRoute> values =newLinkedHashMap<>();for(Entry<String, ZuulRoute> entry : routesMap.entrySet()){
String path = entry.getKey();// Prepend with slash if not already present.if(!path.startsWith("/")){
path ="/"+ path;}if(StringUtils.hasText(this.properties.getPrefix())){
path =this.properties.getPrefix()+ path;if(!path.startsWith("/")){
path ="/"+ path;}}
values.put(path, entry.getValue());}return values;}
觸發路由更新
// org.springframework.cloud.netflix.eureka.CloudEurekaClient#onCacheRefreshed@OverrideprotectedvoidonCacheRefreshed(){super.onCacheRefreshed();if(this.cacheRefreshedCount != null){// might be called during construction and// will be nulllong newCount =this.cacheRefreshedCount.incrementAndGet();
log.trace("onCacheRefreshed called with count: "+ newCount);this.publisher.publishEvent(newHeartbeatEvent(this, newCount));}}
@Override// org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration#startpublicvoidstart(){// only set the port if the nonSecurePort or securePort is 0 and this.port != 0if(this.port.get()!=0){if(this.registration.getNonSecurePort()==0){this.registration.setNonSecurePort(this.port.get());}if(this.registration.getSecurePort()==0&&this.registration.isSecure()){this.registration.setSecurePort(this.port.get());}}// only initialize if nonSecurePort is greater than 0 and it isn't already running// because of containerPortInitializer belowif(!this.running.get()&&this.registration.getNonSecurePort()>0){this.serviceRegistry.register(this.registration);this.context.publishEvent(newInstanceRegisteredEvent<>(this,this.registration.getInstanceConfig()));this.running.set(true);}}