You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ma...@apache.org on 2019/08/07 10:41:52 UTC

[servicecomb-samples] 11/37: fix review comments and add internal access demo

This is an automated email from the ASF dual-hosted git repository.

mabin pushed a commit to branch houserush-sample
in repository https://gitbox.apache.org/repos/asf/servicecomb-samples.git

commit f46f134e4e2d353fc2405eb7a34e757fd97c66e1
Author: liubao <ba...@huawei.com>
AuthorDate: Tue Apr 16 14:12:37 2019 +0800

    fix review comments and add internal access demo
---
 porter_lightweight/README.md                       |  4 +--
 .../porter/file/api/InternalAccessEndpoint.java    | 38 ++++++++++++++++++++++
 .../porter/file/api/InternalAccessService.java     |  5 +++
 .../file/service/InternalAccessServiceImpl.java    | 28 ++++++++++++++++
 .../porter/gateway/InternalAccessHandler.java      | 37 +++++++++++++++++++++
 .../src/main/resources/config/cse.handler.xml      |  6 ++--
 .../src/main/resources/microservice.yaml           |  2 +-
 7 files changed, 115 insertions(+), 5 deletions(-)

diff --git a/porter_lightweight/README.md b/porter_lightweight/README.md
index 93d7bd5..297dbb6 100644
--- a/porter_lightweight/README.md
+++ b/porter_lightweight/README.md
@@ -80,7 +80,7 @@ java $JAVA_OPT -Dgateway.webroot=webapp -jar porter-gateway-service-0.0.1-SNAPSH
 3. 删除文件:输入上一步的文件ID,点击删除。 如果是admin用户,上传成功;如果是guest用户,上传失败。
 
 # 接口使用说明
-ServiceComb推荐先定义接口,再定义实现的开发模式。将接口定义为一个独立项目,可以由设计者统一管控,对于接口的修改,需要设计者进行审核。先定义接口还可以让开发者培养良好的开发习惯,避免将对外接口采用内部实现数据结构(比如JsonObject)、运行平台有关的数据结构(比如HttpServletResponse)来定义,使得以后将项目改在为其他技术框架变得复杂。采用这种方式组织的项目,用户很容易在不同的开发框架上进行迁移,比如gRPC、Spring Cloud等。这里的接口定义代码,对于这些运行框架都是通用的,并且具备跨平台特性。
+ServiceComb推荐先定义接口,再定义实现的开发模式。将接口定义为一个独立项目,可以由设计者统一管控,对于接口的修改,需要设计者进行审核。先定义接口还可以让开发者培养良好的开发习惯,避免将对外接口采用内部实现数据结构(比如JsonObject)、运行平台有关的数据结构(比如HttpServletResponse、HttpServletRequest)来定义,使得以后将项目改造为其他技术框架变得复杂。采用这种方式组织的项目,用户很容易在不同的开发框架上进行迁移,比如gRPC、Spring Cloud等。这里的接口定义代码,对于这些运行框架都是通用的,并且具备跨平台特性。
 
 ## 对于接口实现者(provider)
   * 依赖api对应的jar包
@@ -112,7 +112,7 @@ public class UserServiceImpl implements UserService
   </dependencies>
 ```
 
-  * 采用RCP方式调用
+  * 采用RPC方式调用
 ```
   @RpcReference(microserviceName = "user-service", schemaId = "user")
   private static UserService sserService
diff --git a/porter_lightweight/api/file-service/endpoint/src/main/java/org/apache/servicecomb/samples/porter/file/api/InternalAccessEndpoint.java b/porter_lightweight/api/file-service/endpoint/src/main/java/org/apache/servicecomb/samples/porter/file/api/InternalAccessEndpoint.java
new file mode 100644
index 0000000..1a7d94d
--- /dev/null
+++ b/porter_lightweight/api/file-service/endpoint/src/main/java/org/apache/servicecomb/samples/porter/file/api/InternalAccessEndpoint.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.samples.porter.file.api;
+
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import io.swagger.annotations.Api;
+
+@RestSchema(schemaId = "InternalAccessEndpoint")
+@RequestMapping(path = "/")
+@Api(tags = {"INTERNAL"})
+public class InternalAccessEndpoint {
+  @Autowired
+  private InternalAccessService internalAccessService;
+  
+  @GetMapping(path = "localAccess")
+  public String localAccess(String name) {
+    return internalAccessService.localAccess(name);
+  }
+}
diff --git a/porter_lightweight/api/file-service/service/src/main/java/org/apache/servicecomb/samples/porter/file/api/InternalAccessService.java b/porter_lightweight/api/file-service/service/src/main/java/org/apache/servicecomb/samples/porter/file/api/InternalAccessService.java
new file mode 100644
index 0000000..7e627fb
--- /dev/null
+++ b/porter_lightweight/api/file-service/service/src/main/java/org/apache/servicecomb/samples/porter/file/api/InternalAccessService.java
@@ -0,0 +1,5 @@
+package org.apache.servicecomb.samples.porter.file.api;
+
+public interface InternalAccessService {
+  public String localAccess(String name);
+}
diff --git a/porter_lightweight/file-service/src/main/java/org/apache/servicecomb/samples/porter/file/service/InternalAccessServiceImpl.java b/porter_lightweight/file-service/src/main/java/org/apache/servicecomb/samples/porter/file/service/InternalAccessServiceImpl.java
new file mode 100644
index 0000000..d723945
--- /dev/null
+++ b/porter_lightweight/file-service/src/main/java/org/apache/servicecomb/samples/porter/file/service/InternalAccessServiceImpl.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.samples.porter.file.service;
+
+import org.apache.servicecomb.samples.porter.file.api.InternalAccessService;
+import org.springframework.stereotype.Service;
+
+@Service
+public class InternalAccessServiceImpl implements InternalAccessService {
+  public String localAccess(String name) {
+    return "Hello, " + name;
+  }
+}
diff --git a/porter_lightweight/gateway-service/src/main/java/org/apache/servicecomb/samples/porter/gateway/InternalAccessHandler.java b/porter_lightweight/gateway-service/src/main/java/org/apache/servicecomb/samples/porter/gateway/InternalAccessHandler.java
new file mode 100644
index 0000000..0b181d8
--- /dev/null
+++ b/porter_lightweight/gateway-service/src/main/java/org/apache/servicecomb/samples/porter/gateway/InternalAccessHandler.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.servicecomb.samples.porter.gateway;
+
+import org.apache.servicecomb.core.Handler;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.swagger.invocation.AsyncResponse;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+
+public class InternalAccessHandler implements Handler {
+
+  @Override
+  public void handle(Invocation invocation, AsyncResponse asyncReponse) throws Exception {
+    if (invocation.getOperationMeta().getSwaggerOperation().getTags() != null
+        && invocation.getOperationMeta().getSwaggerOperation().getTags().contains("INTERNAL")) {
+      asyncReponse.consumerFail(new InvocationException(403, "", "not allowed"));
+      return;
+    }
+    invocation.next(asyncReponse);
+  }
+
+}
diff --git a/porter_lightweight/gateway-service/src/main/resources/config/cse.handler.xml b/porter_lightweight/gateway-service/src/main/resources/config/cse.handler.xml
index 00295f8..e9dd306 100644
--- a/porter_lightweight/gateway-service/src/main/resources/config/cse.handler.xml
+++ b/porter_lightweight/gateway-service/src/main/resources/config/cse.handler.xml
@@ -16,6 +16,8 @@
   -->
 
 <config>
-	<handler id="auth"
-			 class="org.apache.servicecomb.samples.porter.gateway.AuthHandler" />
+  <handler id="auth"
+    class="org.apache.servicecomb.samples.porter.gateway.AuthHandler" />
+  <handler id="internalAccess"
+    class="org.apache.servicecomb.samples.porter.gateway.InternalAccessHandler" />
 </config>
diff --git a/porter_lightweight/gateway-service/src/main/resources/microservice.yaml b/porter_lightweight/gateway-service/src/main/resources/microservice.yaml
index 3bb50fa..2462c7f 100644
--- a/porter_lightweight/gateway-service/src/main/resources/microservice.yaml
+++ b/porter_lightweight/gateway-service/src/main/resources/microservice.yaml
@@ -37,7 +37,7 @@ servicecomb:
   handler:
     chain:
       Consumer:
-        default: auth,qps-flowcontrol-consumer,loadbalance
+        default: internalAccess,auth,qps-flowcontrol-consumer,loadbalance
 
   uploads:
     directory: tmp_for_upload_gateway