Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
edge service
Browse files Browse the repository at this point in the history
  • Loading branch information
yi5637 committed May 26, 2018
1 parent 89acbb2 commit c6d416f
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 0 deletions.
8 changes: 8 additions & 0 deletions edge/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM 100.125.0.198:20202/hwcse/dockerhub-java:8-jre-alpine

WORKDIR /home/apps/
ADD target/edge-1.0.0.jar .
ADD target/lib ./lib
ADD start.sh .

ENTRYPOINT ["sh", "/home/apps/start.sh"]
27 changes: 27 additions & 0 deletions edge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Swagger generated server

CSE Spring MVC Server


## Overview
这里的代码是根据用户提供的swagger.yaml文件自动生成。生成的代码总的来说分成以下几类:

1,入口main函数在com.service.edge.EdgeApplication这个类中。

2,com.service.edge.controller.EdgeImpl这个类插入通过定义springmvc的annotation,拦截用户请求、解析参数,并将处理的handler代理给edgeDelegate类。

3,com.service.edge.controller.EdgeDelegate类是用户实现具体handler实现的类,理论上只需要用户在这个类里面实现业务逻辑。


4,resource目录下定义了一些配置文件,包括log4j的配置文件,微服务的配置信息,以及swagger协议文件等等。

5,特别注意两点:microservice.yaml里面配置信息要填写正确,生成的有可能有些偏差;swagger.yaml里面的x-java-interface配置项一定要指定为com.service.edge.controller.Edge。


## 访问边缘服务

http://localhost:13080/api/forecast/forecast/show
http://localhost:13080/api/weather/weather/show

其中:/api/{microservice-name}/{rest-path}
请求包含Header, "auth": "weather"
123 changes: 123 additions & 0 deletions edge/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent>
<groupId>dev</groupId>
<artifactId>weathermap</artifactId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.service</groupId>
<artifactId>edge</artifactId>

<packaging>jar</packaging>

<name>edge</name>
<url>http://maven.apache.org</url>

<properties>
<demo.main>com.service.edge.EdgeApplication</demo.main>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.huawei.paas.cse</groupId>
<artifactId>cse-dependency</artifactId>
<version>${cse.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>com.huawei.paas.cse</groupId>
<artifactId>cse-solution-service-engine</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>edge-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>handler-loadbalance</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>make-vm-zip</id>
<phase>package</phase>
<configuration>
<target>
<zip destfile="target/${build.finalName}.zip">
<zipfileset dir="target/lib" prefix="lib"/>
<zipfileset dir="target" includes="*.jar"/>
<zipfileset dir="." prefix=""
includes="startup.sh"/>
<zipfileset dir="." prefix=""
includes="shutdown.sh"/>
<zipfileset dir="." prefix=""
includes="startup.bat"/>
</zip>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>./lib/</classpathPrefix>
<mainClass>${demo.main}</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
40 changes: 40 additions & 0 deletions edge/src/main/java/com/service/edge/AuthFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.service.edge;

import org.apache.commons.lang.StringUtils;
import org.apache.http.auth.AuthenticationException;
import org.apache.servicecomb.common.rest.filter.HttpServerFilter;
import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.foundation.common.http.HttpStatus;
import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
import org.apache.servicecomb.swagger.invocation.Response;
import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
import org.apache.servicecomb.swagger.invocation.exception.ExceptionFactory;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AuthFilter implements HttpServerFilter {

private static final Logger LOGGER = LoggerFactory.getLogger(AuthFilter.class);

@Override
public int getOrder() {
return 1000;
}

@Override
public Response afterReceiveRequest(Invocation invocation, HttpServletRequestEx requestEx) {
LOGGER.info("Edge service has received a request.");
if (StringUtils.isBlank(requestEx.getHeader("auth"))) {
LOGGER.error("Auth header is needed.");
return Response.failResp(ExceptionFactory.create(new HttpStatus(401, "auth: weather."),
new CommonExceptionData("Auth header is needed (auth: weather).")));
}
return null;
}

@Override
public void beforeSendResponse(Invocation invocation, HttpServletResponseEx responseEx) {
}
}
11 changes: 11 additions & 0 deletions edge/src/main/java/com/service/edge/EdgeApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.service.edge;

import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.apache.servicecomb.foundation.common.utils.Log4jUtils;

public class EdgeApplication {
public static void main(String[] args) throws Exception {
Log4jUtils.init();
BeanUtils.init();
}
}
50 changes: 50 additions & 0 deletions edge/src/main/java/com/service/edge/EdgeDispatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.service.edge;

import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.CookieHandler;
import org.apache.servicecomb.edge.core.AbstractEdgeDispatcher;
import org.apache.servicecomb.edge.core.CompatiblePathVersionMapper;
import org.apache.servicecomb.edge.core.EdgeInvocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.core.Response;
import java.util.Map;

public class EdgeDispatcher extends AbstractEdgeDispatcher {

private static final Logger LOGGER = LoggerFactory.getLogger(AuthFilter.class);

@Override
public int getOrder() {
return 1000;
}

@Override
public void init(Router router) {
String regex = "/api/([^\\\\/]+)/(.*)";
router.routeWithRegex(regex).handler(CookieHandler.create());
router.routeWithRegex(regex).handler(createBodyHandler());
router.routeWithRegex(regex).failureHandler(this::onFailure).handler(this::onRequest);
}

protected void onRequest(RoutingContext context) {
Map<String, String> pathParams = context.pathParams();
String microserviceName = pathParams.get("param0");
String path = "/" + pathParams.get("param1");

EdgeInvocation edgeInvocation = new EdgeInvocation();
edgeInvocation.init(microserviceName, context, path, httpServerFilters);
edgeInvocation.edgeInvoke();
}

protected void onFailure(RoutingContext context) {
LOGGER.error("Edge service failed.", context.failure());
HttpServerResponse response = context.response();
response.setStatusCode(Response.Status.BAD_REQUEST.getStatusCode());
response.setStatusMessage(Response.Status.BAD_REQUEST.getReasonPhrase());
response.end(context.failure().getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

com.service.edge.AuthFilter
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.service.edge.EdgeDispatcher
12 changes: 12 additions & 0 deletions edge/src/main/resources/META-INF/spring/cse.edge.bean.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:cse="http://www.huawei.com/schema/paas/cse/pojo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans classpath:org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.service.edge" />

</beans>
4 changes: 4 additions & 0 deletions edge/src/main/resources/config/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
paas.logs.dir=../logs/
paas.logs.file=edge.log

log4j.rootLogger=INFO,paas,stdout
32 changes: 32 additions & 0 deletions edge/src/main/resources/microservice.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
APPLICATION_ID: weathermap
service_description:
name: edge
version: 0.0.1
properties:
allowCrossApp: false
cse:
service:
registry:
address: https://cse.cn-north-1.myhwclouds.com:443
instance:
watch: false
config:
client:
serverUri: https://cse.cn-north-1.myhuaweicloud.com:443
refreshMode: 1
refresh_interval: 5000
#monitor:
#client:
#serverUri: https://cse.cn-north-1.myhuaweicloud.com:443
rest:
address: 0.0.0.0:13080
handler:
chain:
Consumer:
default: loadbalance
#When a local project is deployed as a container outside a cluster, you need to delete all monitor and credentials comments and configure the AK/SK.
#credentials:
#accessKey: ak
#secretKey: sk
#akskCustomCipher: default
#project: cn-north-1
4 changes: 4 additions & 0 deletions edge/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# CMDVAR="-Djava.security.egd=file:/dev/./urandom","java -agentlib:jdwp=transport=dt_socket,address=0:8000,server=y,suspend=n -jar"
java $CMDVAR -jar ./edge-0.0.1-SNAPSHOT.jar
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<module>forecast</module>
<module>fusionweather</module>
<module>weather-beta</module>
<module>edge</module>
</modules>

<build>
Expand Down

0 comments on commit c6d416f

Please sign in to comment.