Procházet zdrojové kódy

修改注册逻辑

tianboguang před 3 roky
rodič
revize
fa15561a24

+ 52 - 0
platform-admin/src/main/java/com/platform/config/NacosConfiguration.java

@@ -0,0 +1,52 @@
+package com.platform.config;
+
+import com.alibaba.nacos.api.annotation.NacosInjected;
+import com.alibaba.nacos.api.annotation.NacosProperties;
+import com.alibaba.nacos.api.config.ConfigType;
+import com.alibaba.nacos.api.exception.NacosException;
+import com.alibaba.nacos.api.naming.NamingService;
+import com.alibaba.nacos.spring.context.annotation.EnableNacos;
+import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.context.annotation.PropertySources;
+
+import javax.annotation.PostConstruct;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+@Configuration
+@PropertySources({
+        @PropertySource({
+                "classpath:application.properties"
+        })
+})
+@EnableNacos(
+    globalProperties = @NacosProperties(serverAddr = "${nacos.config.server-addr}",namespace = "${nacos.config.namespace}")
+)
+@NacosPropertySource(dataId = "${nacos.config.data-id}", autoRefreshed = true,type = ConfigType.PROPERTIES,groupId = "${nacos.config.group}")
+public class NacosConfiguration {
+
+    @NacosInjected
+    private NamingService namingService;
+
+    @Value("${spring.application.name}")
+    private String applicationName;
+
+    @Value("${nacos.discovery.register.group-name}")
+    private String groupName;
+
+    @Value("${server.port}")
+    private Integer port;
+
+    @PostConstruct
+    public void init() {
+        try {
+            InetAddress address = InetAddress.getLocalHost();
+            namingService.registerInstance(applicationName, groupName, address.getHostAddress(), port);
+        } catch (UnknownHostException | NacosException e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 106 - 0
platform-common/src/main/java/com/platform/aop/RestTemplateAspect.java

@@ -0,0 +1,106 @@
+package com.platform.aop;
+
+import com.alibaba.nacos.api.annotation.NacosInjected;
+import com.alibaba.nacos.api.exception.NacosException;
+import com.alibaba.nacos.api.naming.NamingService;
+import com.alibaba.nacos.api.naming.pojo.Instance;
+import com.netflix.loadbalancer.ILoadBalancer;
+import com.netflix.loadbalancer.LoadBalancerBuilder;
+import com.netflix.loadbalancer.Server;
+import org.apache.http.client.utils.URIBuilder;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.stream.Collectors;
+
+/**
+ * 系统日志,切面处理类
+ *
+ * @author lipengjun
+ * @email 939961241@qq.com
+ * @gitee https://gitee.com/fuyang_lipengjun/platform
+ * @date 2017年3月8日 上午11:07:35
+ */
+@Aspect
+@Component
+public class RestTemplateAspect {
+
+    @NacosInjected
+    private NamingService namingService;
+
+    @Value("${nacos.discovery.register.group-name}")
+    private String groupName;
+
+    private static Map<String, ILoadBalancer> loadBalancerMap = new HashMap<>();
+
+
+    /**
+     * 切点
+     */
+    @Pointcut("execution (public * org.springframework.web.client.RestTemplate.getForObject(..)) " +
+            "|| execution (public * org.springframework.web.client.RestTemplate.postForEntity(..)) ")
+    public void logPointCut() {
+
+    }
+
+    /**
+     * 环绕通知
+     */
+    @Around("logPointCut()")
+    public Object saveSysLog(ProceedingJoinPoint point) throws Throwable {
+        Object[] args = point.getArgs();
+        // 获取请求链接
+        String url = args[0].toString();
+        URIBuilder uriBuilder = new URIBuilder(url);
+        if (uriBuilder.getPort() > 0) {
+            return point.proceed();
+        }
+        String serviceName = uriBuilder.getHost();
+        if (serviceName.contains(".")) {
+            return point.proceed();
+        }
+        args[0] = url.replace(serviceName, getServer(serviceName).getHostPort());
+        try {
+            return point.proceed(args);
+        } catch (Exception e) {
+            e.printStackTrace();
+            initLoadBalancerMap(serviceName);
+            throw new RuntimeException("请求失败!");
+        }
+    }
+
+    private Server getServer(String hostName) {
+        if (!loadBalancerMap.containsKey(hostName)) {
+            initLoadBalancerMap(hostName);
+        }
+        return loadBalancerMap.get(hostName).chooseServer(null);
+    }
+
+    private void initLoadBalancerMap(String hostName) {
+        // 获取服务列表
+        List<Instance> instances = null;
+        try {
+            instances = namingService.getAllInstances(hostName, groupName);
+        } catch (NacosException e) {
+            e.printStackTrace();
+            throw new RuntimeException("获取服务实例失败,服务名称:" + hostName);
+        }
+        if (CollectionUtils.isEmpty(instances)) {
+            throw new RuntimeException("未获取到服务实例,服务名称:" + hostName);
+        }
+        List<Server> servers = instances.stream().map(instance -> new Server(instance.getIp(), instance.getPort())).collect(Collectors.toList());
+        ILoadBalancer loadBalancer = LoadBalancerBuilder.newBuilder().buildFixedServerListLoadBalancer(servers);
+        loadBalancerMap.put(hostName, loadBalancer);
+    }
+
+}

+ 45 - 0
platform-common/src/main/java/com/platform/vos/ReturnDTO.java

@@ -0,0 +1,45 @@
+package com.platform.vos;
+
+public class ReturnDTO {
+    private int code;
+    private String error;
+    private Object message;
+
+    public ReturnDTO(int code, String error, Object message) {
+        this.code = code;
+        this.error = error;
+        this.message = message;
+    }
+
+    public ReturnDTO(int code, String error) {
+        this.code = code;
+        this.error = error;
+    }
+
+    public ReturnDTO() {
+    }
+
+    public int getCode() {
+        return this.code;
+    }
+
+    public void setCode(int code) {
+        this.code = code;
+    }
+
+    public String getError() {
+        return this.error;
+    }
+
+    public void setError(String error) {
+        this.error = error;
+    }
+
+    public Object getMessage() {
+        return this.message;
+    }
+
+    public void setMessage(Object message) {
+        this.message = message;
+    }
+}