Commit 9744aa1a by Nepxion

重构类结构

parent 35a6063c
package com.nepxion.discovery.plugin.example; package com.nepxion.discovery.plugin.example.service;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
...@@ -15,11 +15,11 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; ...@@ -15,11 +15,11 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import com.nepxion.discovery.plugin.example.extension.MyDiscoveryEnabledAdapter; import com.nepxion.discovery.plugin.example.service.extension.MyDiscoveryListener;
import com.nepxion.discovery.plugin.example.extension.MyDiscoveryListener; import com.nepxion.discovery.plugin.example.service.extension.MyLoadBalanceListener;
import com.nepxion.discovery.plugin.example.extension.MyLoadBalanceListener; import com.nepxion.discovery.plugin.example.service.extension.MyRegisterListener;
import com.nepxion.discovery.plugin.example.extension.MyRegisterListener; import com.nepxion.discovery.plugin.example.service.extension.MyServiceDiscoveryEnabledAdapter;
import com.nepxion.discovery.plugin.example.extension.MySubscriber; import com.nepxion.discovery.plugin.example.service.extension.MySubscriber;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
...@@ -52,7 +52,7 @@ public class DiscoveryApplicationA1 { ...@@ -52,7 +52,7 @@ public class DiscoveryApplicationA1 {
} }
@Bean @Bean
public MyDiscoveryEnabledAdapter MyDiscoveryEnabledAdapter() { public MyServiceDiscoveryEnabledAdapter myServiceDiscoveryEnabledAdapter() {
return new MyDiscoveryEnabledAdapter(); return new MyServiceDiscoveryEnabledAdapter();
} }
} }
\ No newline at end of file
package com.nepxion.discovery.plugin.example; package com.nepxion.discovery.plugin.example.service;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example; package com.nepxion.discovery.plugin.example.service;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example; package com.nepxion.discovery.plugin.example.service;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example; package com.nepxion.discovery.plugin.example.service;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example; package com.nepxion.discovery.plugin.example.service;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example; package com.nepxion.discovery.plugin.example.service;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.extension; package com.nepxion.discovery.plugin.example.service.extension;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.extension; package com.nepxion.discovery.plugin.example.service.extension;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.extension; package com.nepxion.discovery.plugin.example.service.extension;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.extension; package com.nepxion.discovery.plugin.example.service.extension;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
...@@ -16,28 +16,31 @@ import org.slf4j.LoggerFactory; ...@@ -16,28 +16,31 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter; import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.strategy.extension.constant.StrategyConstant; import com.nepxion.discovery.plugin.strategy.discovery.DiscoveryEnabledAdapter;
import com.nepxion.discovery.plugin.strategy.extension.context.StrategyContext; import com.nepxion.discovery.plugin.strategy.extension.service.constant.ServiceStrategyConstant;
import com.nepxion.discovery.plugin.strategy.extension.enable.DiscoveryEnabledAdapter; import com.nepxion.discovery.plugin.strategy.extension.service.context.ServiceStrategyContext;
import com.nepxion.discovery.plugin.strategy.extension.service.context.ServiceStrategyContextHolder;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
public class MyDiscoveryEnabledAdapter implements DiscoveryEnabledAdapter { public class MyServiceDiscoveryEnabledAdapter implements DiscoveryEnabledAdapter {
private static final Logger LOG = LoggerFactory.getLogger(MyDiscoveryEnabledAdapter.class); private static final Logger LOG = LoggerFactory.getLogger(MyServiceDiscoveryEnabledAdapter.class);
@Autowired @Autowired
protected PluginAdapter pluginAdapter; protected PluginAdapter pluginAdapter;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public boolean apply(Server server, StrategyContext context) { public boolean apply(Server server) {
ServiceStrategyContext context = ServiceStrategyContextHolder.currentContext();
Map<String, Object> attributes = context.getAttributes();
String serviceId = server.getMetaInfo().getAppName().toLowerCase(); String serviceId = server.getMetaInfo().getAppName().toLowerCase();
Map<String, String> metadata = pluginAdapter.getServerMetadata(server); Map<String, String> metadata = pluginAdapter.getServerMetadata(server);
Map<String, Object> attributes = context.getAttributes();
LOG.info("负载均衡用户定制触发:serviceId={}, host={}, metadata={}, context={}", serviceId, server.toString(), metadata, context); LOG.info("Serivice端负载均衡用户定制触发:serviceId={}, host={}, metadata={}, context={}", serviceId, server.toString(), metadata, context);
if (attributes.containsKey(StrategyConstant.PARAMETER_MAP)) { if (attributes.containsKey(ServiceStrategyConstant.PARAMETER_MAP)) {
Map<String, Object> parameterMap = (Map<String, Object>) attributes.get(StrategyConstant.PARAMETER_MAP); Map<String, Object> parameterMap = (Map<String, Object>) attributes.get(ServiceStrategyConstant.PARAMETER_MAP);
String value = parameterMap.get("value").toString(); String value = parameterMap.get("value").toString();
if (value.contains("abc")) { if (value.contains("abc")) {
LOG.info("过滤条件:当前端输入值包含'abc'的时候,不能被Ribbon负载均衡到"); LOG.info("过滤条件:当前端输入值包含'abc'的时候,不能被Ribbon负载均衡到");
......
package com.nepxion.discovery.plugin.example.extension; package com.nepxion.discovery.plugin.example.service.extension;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.feign; package com.nepxion.discovery.plugin.example.service.feign;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.feign; package com.nepxion.discovery.plugin.example.service.feign;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.feign; package com.nepxion.discovery.plugin.example.service.feign;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.feign; package com.nepxion.discovery.plugin.example.service.feign;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.feign; package com.nepxion.discovery.plugin.example.service.feign;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.feign; package com.nepxion.discovery.plugin.example.service.feign;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.feign; package com.nepxion.discovery.plugin.example.service.feign;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example.feign; package com.nepxion.discovery.plugin.example.service.feign;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
......
package com.nepxion.discovery.plugin.example; package com.nepxion.discovery.plugin.example.zuul;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
...@@ -13,6 +13,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; ...@@ -13,6 +13,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import com.nepxion.discovery.plugin.example.zuul.extension.MyZuulDiscoveryEnabledAdapter;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
...@@ -23,4 +26,9 @@ public class DiscoveryApplicationZuul { ...@@ -23,4 +26,9 @@ public class DiscoveryApplicationZuul {
new SpringApplicationBuilder(DiscoveryApplicationZuul.class).run(args); new SpringApplicationBuilder(DiscoveryApplicationZuul.class).run(args);
} }
@Bean
public MyZuulDiscoveryEnabledAdapter myZuulDiscoveryEnabledAdapter() {
return new MyZuulDiscoveryEnabledAdapter();
}
} }
\ No newline at end of file
package com.nepxion.discovery.plugin.example.zuul.extension;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.strategy.discovery.DiscoveryEnabledAdapter;
import com.netflix.loadbalancer.Server;
import com.netflix.zuul.context.RequestContext;
public class MyZuulDiscoveryEnabledAdapter implements DiscoveryEnabledAdapter {
private static final Logger LOG = LoggerFactory.getLogger(MyZuulDiscoveryEnabledAdapter.class);
@Autowired
protected PluginAdapter pluginAdapter;
@Override
public boolean apply(Server server) {
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
String token = request.getHeader("token");
String serviceId = server.getMetaInfo().getAppName().toLowerCase();
Map<String, String> metadata = pluginAdapter.getServerMetadata(server);
LOG.info("Zuul端负载均衡用户定制触发:serviceId={}, host={}, metadata={}, context={}", serviceId, server.toString(), metadata, context);
if (StringUtils.equals(token, "abc")) {
return false;
}
return true;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment