protected Transport chooseOneTransport(List<Transport> group) { group.sort((t1, t2) -> { return Integer.compare(t1.getOrder(), t2.getOrder()); }); for (Transport transport : group) { if (transport.canInit()) { LOGGER.info("choose {} for {}.", transport.getClass().getName(), transport.getName()); return transport; } } throw new ServiceCombException( String.format("all transport named %s refused to init.", group.get(0).getName())); }
public Endpoint(Transport transport, String endpoint, MicroserviceInstance instance) { this.transport = transport; this.endpoint = endpoint; this.instance = instance; this.address = transport.parseAddress(this.endpoint); }
public void init() throws Exception { buildTransportMap(); for (Transport transport : transportMap.values()) { if (transport.init()) { Endpoint endpoint = transport.getPublishEndpoint(); if (endpoint != null && endpoint.getEndpoint() != null) { LOGGER.info("endpoint to publish: {}", endpoint.getEndpoint()); Microservice microservice = RegistryUtils.getMicroservice(); microservice.getIntance().getEndpoints().add(endpoint.getEndpoint()); } continue; } } }
protected Map<String, List<Transport>> groupByName() { Map<String, List<Transport>> groups = new HashMap<>(); for (Transport transport : transports) { List<Transport> list = groups.computeIfAbsent(transport.getName(), name -> { return new ArrayList<>(); }); list.add(transport); } return groups; }
protected void checkTransportGroup(List<Transport> group) { // order value must be different, otherwise, maybe will choose a random transport Map<Integer, Transport> orderMap = new HashMap<>(); for (Transport transport : group) { Transport existTransport = orderMap.putIfAbsent(transport.getOrder(), transport); if (existTransport != null) { throw new ServiceCombException(String.format("%s and %s have the same order %d", existTransport.getClass().getName(), transport.getClass().getName(), transport.getOrder())); } } }
@Override protected void createInvocation(Object[] args) { this.invocation = InvocationFactory.forProvider(transport.getEndpoint(), restOperationMeta.getOperationMeta(), args); } }
@Override public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception { Transport transport = invocation.getTransport(); log.debug( "Sending request {} to {}", invocation.getMicroserviceQualifiedName(), invocation.getEndpoint().getEndpoint()); transport.send(invocation, asyncResp); } }
protected void buildTransportMap() { Map<String, List<Transport>> groups = groupByName(); for (Entry<String, List<Transport>> entry : groups.entrySet()) { List<Transport> group = entry.getValue(); checkTransportGroup(group); Transport transport = chooseOneTransport(group); transportMap.put(transport.getName(), transport); } }
public String getRealTransportName() { return (endpoint != null) ? endpoint.getTransport().getName() : getConfigTransportName(); }