diff --git a/sop-example/sop-easyopen/pom.xml b/sop-example/sop-easyopen/pom.xml index 1e2d9bfc..225a61ca 100644 --- a/sop-example/sop-easyopen/pom.xml +++ b/sop-example/sop-easyopen/pom.xml @@ -7,7 +7,7 @@ 1.0.0-SNAPSHOT jar - easyopen-server-empty + easyopen-server easyopen Demo project for Spring Boot @@ -31,10 +31,28 @@ sop-service-common 1.15.2-SNAPSHOT + + org.springframework.cloud - spring-cloud-starter-netflix-eureka-client + spring-cloud-starter-alibaba-nacos-discovery + 0.2.2.RELEASE + + + com.alibaba.nacos + nacos-client + + + + + com.alibaba.nacos + nacos-client + 1.1.0 + net.oschina.durcframework easyopen diff --git a/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/api/GoodsApi.java b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/api/GoodsApi.java index f26168ed..bd848d3f 100644 --- a/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/api/GoodsApi.java +++ b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/api/GoodsApi.java @@ -18,7 +18,7 @@ import java.math.BigDecimal; @ApiDoc("商品接口") public class GoodsApi { - @Api(name = "goods.get") + @Api(name = "easyopen.goods.get") @ApiDocMethod(description = "获取商品") Goods getGoods(GoodsParam param) { Goods goods = new Goods(); diff --git a/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/config/SopConfig.java b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/config/SopConfig.java index f6036b78..4228df21 100644 --- a/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/config/SopConfig.java +++ b/sop-example/sop-easyopen/src/main/java/com/gitee/easyopen/server/config/SopConfig.java @@ -1,7 +1,7 @@ package com.gitee.easyopen.server.config; import com.gitee.sop.servercommon.configuration.EasyopenDocSupportController; -import com.gitee.sop.servercommon.configuration.EasyopenServiceConfiguration; +import com.gitee.sop.servercommon.easyopen.EasyopenServiceConfiguration; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Controller; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; diff --git a/sop-example/sop-easyopen/src/main/resources/application-dev.properties b/sop-example/sop-easyopen/src/main/resources/application-dev.properties new file mode 100644 index 00000000..c65f550f --- /dev/null +++ b/sop-example/sop-easyopen/src/main/resources/application-dev.properties @@ -0,0 +1,7 @@ +server.port=2121 +spring.application.name=easyopen-service + +# nacos注册中心 +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 + +easyopen.prefix-path=/api diff --git a/sop-example/sop-easyopen/src/main/resources/application-dev.yml b/sop-example/sop-easyopen/src/main/resources/application-dev.yml deleted file mode 100644 index b280f484..00000000 --- a/sop-example/sop-easyopen/src/main/resources/application-dev.yml +++ /dev/null @@ -1,23 +0,0 @@ -server: - port: 2121 - -# 指定注册中心 -eureka: - port: 1111 - host: localhost - client: - serviceUrl: - defaultZone: http://${eureka.host}:${eureka.port}/eureka/ - -spring: - application: - # 指定服务名 - name: easyopen-service - - cloud: - zookeeper: - connect-string: localhost:2181 - -# 指定接口路径 -easyopen: - prefix-path: /api diff --git a/sop-example/sop-easyopen/src/main/resources/application.properties b/sop-example/sop-easyopen/src/main/resources/application.properties new file mode 100644 index 00000000..257b3064 --- /dev/null +++ b/sop-example/sop-easyopen/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.profiles.active=dev \ No newline at end of file diff --git a/sop-example/sop-easyopen/src/main/resources/application.yml b/sop-example/sop-easyopen/src/main/resources/application.yml deleted file mode 100644 index caf4dfcd..00000000 --- a/sop-example/sop-easyopen/src/main/resources/application.yml +++ /dev/null @@ -1,3 +0,0 @@ -spring: - profiles: - active: dev \ No newline at end of file diff --git a/sop-gateway/src/main/java/com/gitee/sop/gateway/config/ZuulConfig.java b/sop-gateway/src/main/java/com/gitee/sop/gateway/config/ZuulConfig.java index d5f66cd6..e416891f 100644 --- a/sop-gateway/src/main/java/com/gitee/sop/gateway/config/ZuulConfig.java +++ b/sop-gateway/src/main/java/com/gitee/sop/gateway/config/ZuulConfig.java @@ -43,7 +43,6 @@ public class ZuulConfig extends AlipayZuulConfiguration { //@Configuration //public class ZuulConfig extends EasyopenZuulConfiguration { // static { -// new ManagerInitializer(); // Map appSecretMap = new HashMap<>(); // appSecretMap.put("easyopen_test", "G9w0BAQEFAAOCAQ8AMIIBCgKCA"); // ApiConfig.getInstance().addAppSecret(appSecretMap); diff --git a/sop-test/src/test/java/com/gitee/sop/test/EasyopenClientPostTest.java b/sop-test/src/test/java/com/gitee/sop/test/EasyopenClientPostTest.java index d09b4d06..3328e5f4 100644 --- a/sop-test/src/test/java/com/gitee/sop/test/EasyopenClientPostTest.java +++ b/sop-test/src/test/java/com/gitee/sop/test/EasyopenClientPostTest.java @@ -35,7 +35,7 @@ public class EasyopenClientPostTest extends TestBase { // 系统参数 Map params = new HashMap<>(); - params.put("name", "goods.get"); + params.put("name", "easyopen.goods.get"); params.put("app_key", appKey); params.put("data", json); params.put("timestamp", getTime()); diff --git a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocDiscovery.java b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocDiscovery.java index 96f9f55a..63f5658b 100644 --- a/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocDiscovery.java +++ b/sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/manager/DocDiscovery.java @@ -14,7 +14,9 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.DigestUtils; import org.springframework.web.client.RestTemplate; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; /** @@ -33,15 +35,9 @@ public class DocDiscovery { private RestTemplate restTemplate = new RestTemplate(); - private volatile long lastUpdateTime; + private Map updateTimeMap = new HashMap<>(16); public synchronized void refresh(DocManager docManager) { - long now = System.currentTimeMillis(); - // 5秒内只能执行一次,解决重启应用连续加载4次问题 - if (now - lastUpdateTime < FIVE_SECONDS) { - return; - } - lastUpdateTime = now; NamingService namingService = nacosDiscoveryProperties.namingServiceInstance(); List subscribes = null; try { @@ -60,6 +56,14 @@ public class DocDiscovery { if (Objects.equals(thisServiceId, serviceName) || "api-gateway".equalsIgnoreCase(serviceName)) { continue; } + // nacos会不停的触发事件,这里做了一层拦截 + // 同一个serviceId5秒内允许访问一次 + Long lastUpdateTime = updateTimeMap.getOrDefault(serviceName, 0L); + long now = System.currentTimeMillis(); + if (now - lastUpdateTime < FIVE_SECONDS) { + continue; + } + updateTimeMap.put(serviceName, now); try { List allInstances = namingService.getAllInstances(serviceName); if (CollectionUtils.isEmpty(allInstances)) {