sss

获取实例:

/**
	 * 获取所有服务实例
	 * 
	 * @param appContext
	 * @return
	 */
	private List<ServiceInstance> getAllServiceInstances(
			ApplicationContext appContext) {
		List<ServiceInstance> ls = Lists.newArrayList();

		Map<String, Object> restServiceMap = appContext
				.getBeansWithAnnotation(RegisterRestResource.class);
		for (Object o : restServiceMap.values()) {
			ls.addAll(getServiceInstance(o.getClass()));
		}

		return ls;
	}

  listener:

public class SpringRestServiceRegister implements
		ApplicationListener<ContextRefreshedEvent> {

	public static Logger logger = LoggerFactory
			.getLogger(SpringRestServiceRegister.class);

	private List<ServiceInstance> serviceInstances;

	private RegisterServiceCenter dispatcher;

	private final static String REST_SERVICE_NAME_PREFIX = "REST:";
	private final static String RAW_SPEC = "{scheme}://{address}:{port}{app-context}{service-path}";

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		getDispatcher();
		synchronized (dispatcher) {
			if (!dispatcher.isServiceRegistered.get()) {
				dispatcher.isServiceRegistered.set(true);
				serviceInstances = getAllServiceInstances(event
						.getApplicationContext());
				registerServiceInstances(serviceInstances);
			}
		}
	}

	private void registerServiceInstances(List<ServiceInstance> ls) {
		for (ServiceInstance serviceInstance : ls) {
			try {
				getDispatcher().getServiceDiscovery().registerService(
						serviceInstance);
				logger.info("register serviceInstance:[{},{}]", new Object[]{serviceInstance.getName(),serviceInstance.getServiceUrl()});
			} catch (Exception e) {
				logger.error("serviceInstance注册失败!" + serviceInstance, e);
			}
		}
	}

	/**
	 * 获取所有服务实例
	 * 
	 * @param appContext
	 * @return
	 */
	private List<ServiceInstance> getAllServiceInstances(
			ApplicationContext appContext) {
		List<ServiceInstance> ls = Lists.newArrayList();

		Map<String, Object> restServiceMap = appContext
				.getBeansWithAnnotation(RegisterRestResource.class);
		for (Object o : restServiceMap.values()) {
			ls.addAll(getServiceInstance(o.getClass()));
		}

		return ls;
	}

  

posted @ 2021-11-10 12:50  mimimikasa  阅读(545)  评论(0)    收藏  举报