Skip to content
This repository was archived by the owner on Jun 6, 2018. It is now read-only.

Commit 5eddad3

Browse files
committed
#1 服务端基本架构
1 parent 502e722 commit 5eddad3

File tree

18 files changed

+631
-0
lines changed

18 files changed

+631
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
target/
12
*.class
23

34
# Mobile Tools for Java (J2ME)

examples/hello-service/pom.xml

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>io.springside.springtime.examples</groupId>
6+
<artifactId>hello-service</artifactId>
7+
<version>master-SNAPSHOT</version>
8+
<name>hello-service</name>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.4.2.RELEASE</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>io.springside.springtime</groupId>
19+
<artifactId>springtime</artifactId>
20+
<version>master-SNAPSHOT</version>
21+
</dependency>
22+
23+
<!-- test -->
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-test</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.assertj</groupId>
35+
<artifactId>assertj-core</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.apache.httpcomponents</groupId>
39+
<artifactId>fluent-hc</artifactId>
40+
<version>4.5.2</version>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-maven-plugin</artifactId>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.springside.springtime.examples.helloservice;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Import;
6+
7+
import io.springside.springtime.springboot.SpringTimeConfiguration;
8+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
9+
10+
@SpringBootApplication
11+
@Import(SpringTimeConfiguration.class)
12+
@EnableSwagger2
13+
public class HelloServiceApp {
14+
public static void main(String[] args) throws Exception {
15+
SpringApplication.run(HelloServiceApp.class, args);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.springside.springtime.examples.helloservice.idl;
2+
3+
public class City {
4+
5+
private String name;
6+
7+
public String getName() {
8+
return name;
9+
}
10+
public void setName(String name) {
11+
this.name = name;
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.springside.springtime.examples.helloservice.idl;
2+
3+
public interface GreetingService {
4+
5+
public static class HelloRequest {
6+
public String name;
7+
}
8+
9+
public static class HelloResponse {
10+
public String message;
11+
}
12+
13+
public static class WeatherRequest {
14+
public City city;
15+
}
16+
17+
public static class WeatherResponse {
18+
public String weather;
19+
}
20+
21+
public HelloResponse hello(HelloRequest helloRequest);
22+
23+
public WeatherResponse weather(WeatherRequest weatherRequest);
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.springside.springtime.examples.helloservice.restful;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
import org.springframework.web.bind.annotation.ResponseBody;
7+
8+
@Controller
9+
public class HelloService {
10+
11+
@RequestMapping(value = "/hello",method=RequestMethod.GET)
12+
@ResponseBody
13+
String home() {
14+
return "Hello World!";
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.springside.springtime.examples.helloservice.service;
2+
3+
import org.springframework.stereotype.Controller;
4+
5+
import io.springside.springtime.examples.helloservice.idl.GreetingService;
6+
import io.swagger.annotations.Api;
7+
import io.swagger.annotations.ApiOperation;
8+
9+
@Api("greeting")
10+
@Controller("greeting")
11+
public class GreetingServiceImpl implements GreetingService {
12+
13+
@Override
14+
@ApiOperation("hello")
15+
public HelloResponse hello(HelloRequest helloRequest) {
16+
HelloResponse response = new HelloResponse();
17+
response.message = "Hello " + helloRequest.name;
18+
return response;
19+
}
20+
21+
@Override
22+
public WeatherResponse weather(WeatherRequest weatherRequest) {
23+
WeatherResponse response = new WeatherResponse();
24+
response.weather = "Sunny at " + weatherRequest.city.getName();
25+
return response;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
server.port=8080
2+
server.context-path=/web
3+
management.context-path=/manage
4+
5+
server.jetty.maxThreads=512
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.springside.springtime.examples.helloservice;
2+
3+
import static org.assertj.core.api.Assertions.*;
4+
5+
import java.io.IOException;
6+
7+
import org.apache.http.HttpVersion;
8+
import org.apache.http.client.ClientProtocolException;
9+
import org.apache.http.client.fluent.Request;
10+
import org.apache.http.entity.ContentType;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
15+
import org.springframework.test.context.junit4.SpringRunner;
16+
17+
/**
18+
* via HTTP 1.1
19+
*/
20+
@RunWith(SpringRunner.class)
21+
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
22+
public class GreetingServerWithApacheTest {
23+
24+
@Test
25+
public void helloService() throws ClientProtocolException, IOException {
26+
String requestJson = "{\"name\":\"david\"}";
27+
String expectdResponse = "{\"message\":\"Hello david\"}";
28+
String response = Request.Post("http://localhost:8080/rpc/greeting/hello").version(HttpVersion.HTTP_1_1)
29+
.bodyString(requestJson, ContentType.APPLICATION_JSON).execute().returnContent().asString();
30+
assertThat(response).isEqualTo(expectdResponse);
31+
}
32+
33+
@Test
34+
public void weatherService() throws ClientProtocolException, IOException {
35+
String requestJson = "{\"city\":{\"name\":\"guangzhou\"}}";
36+
String expectdResponse = "{\"weather\":\"Sunny at guangzhou\"}";
37+
38+
String response = Request.Post("http://localhost:8080/rpc/greeting/weather").version(HttpVersion.HTTP_1_1)
39+
.bodyString(requestJson, ContentType.APPLICATION_JSON).execute().returnContent().asString();
40+
assertThat(response).isEqualTo(expectdResponse);
41+
}
42+
43+
@Test
44+
public void helloRest() throws ClientProtocolException, IOException {
45+
String expectdResponse = "Hello World!";
46+
String response = Request.Get("http://localhost:8080/web/hello").version(HttpVersion.HTTP_1_1).execute()
47+
.returnContent().asString();
48+
assertThat(response).isEqualTo(expectdResponse);
49+
}
50+
}

pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>io.springside</groupId>
6+
<artifactId>springtime-project</artifactId>
7+
<version>master-SNAPSHOT</version>
8+
<packaging>pom</packaging>
9+
<name>springtime-project</name>
10+
11+
<modules>
12+
<module>springtime</module>
13+
<module>examples/hello-service</module>
14+
</modules>
15+
</project>

springtime/pom.xml

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>io.springside.springtime</groupId>
6+
<artifactId>springtime</artifactId>
7+
<version>master-SNAPSHOT</version>
8+
<name>springtime</name>
9+
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>1.4.2.RELEASE</version>
14+
</parent>
15+
16+
<properties>
17+
<cglib.version>3.2.4</cglib.version>
18+
<swagger.version>1.5.10</swagger.version>
19+
<springfox.version>2.6.1</springfox.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-web</artifactId>
26+
<exclusions>
27+
<exclusion>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-tomcat</artifactId>
30+
</exclusion>
31+
</exclusions>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-jetty</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.eclipse.jetty.http2</groupId>
40+
<artifactId>http2-server</artifactId>
41+
<version>${jetty.version}</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-actuator</artifactId>
47+
</dependency>
48+
49+
<dependency>
50+
<groupId>io.swagger</groupId>
51+
<artifactId>swagger-annotations</artifactId>
52+
<version>${swagger.version}</version>
53+
</dependency>
54+
<dependency>
55+
<groupId>io.springfox</groupId>
56+
<artifactId>springfox-swagger2</artifactId>
57+
<version>${springfox.version}</version>
58+
</dependency>
59+
<dependency>
60+
<groupId>io.springfox</groupId>
61+
<artifactId>springfox-swagger-ui</artifactId>
62+
<version>${springfox.version}</version>
63+
</dependency>
64+
65+
<dependency>
66+
<groupId>cglib</groupId>
67+
<artifactId>cglib</artifactId>
68+
<version>${cglib.version}</version>
69+
</dependency>
70+
71+
<dependency>
72+
<groupId>com.google.guava</groupId>
73+
<artifactId>guava</artifactId>
74+
<version>19.0</version>
75+
</dependency>
76+
</dependencies>
77+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.springside.springtime.jetty;
2+
3+
import java.io.IOException;
4+
import java.util.Map;
5+
import java.util.Map.Entry;
6+
7+
import javax.servlet.ServletException;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
import org.eclipse.jetty.server.Request;
12+
import org.eclipse.jetty.server.handler.AbstractHandler;
13+
import org.springframework.context.ApplicationContext;
14+
15+
import io.springside.springtime.serializer.JsonSerializer;
16+
import io.springside.springtime.serializer.Serializer;
17+
import io.springside.springtime.service.ServiceDispatcher;
18+
import io.springside.springtime.service.ServiceBeanRegistry;
19+
import io.swagger.annotations.Api;
20+
21+
/**
22+
* Jetty的Handler.
23+
*
24+
* 以服务化框架的方式直接处理/rpc 路径的请求.
25+
* 其他路径的请求,仍然交给WebServletContxt Handler处理.
26+
*/
27+
public class SpringTimeHandler extends AbstractHandler {
28+
29+
public static final String RPC_PREFIX = "/rpc";
30+
31+
private ServiceDispatcher dispatcher;
32+
private Serializer serializer = new JsonSerializer();
33+
34+
public SpringTimeHandler(ApplicationContext applicationContext) {
35+
ServiceBeanRegistry serviceRegistry = new ServiceBeanRegistry();
36+
Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Api.class);
37+
for (Entry<String, Object> entry : beans.entrySet()) {
38+
serviceRegistry.add(RPC_PREFIX, entry.getKey(), entry.getValue());
39+
}
40+
41+
dispatcher = new ServiceDispatcher(serviceRegistry);
42+
}
43+
44+
@Override
45+
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
46+
throws IOException, ServletException {
47+
if (!target.startsWith(RPC_PREFIX)) {
48+
return;
49+
}
50+
String path = target.toLowerCase();
51+
Serializer serializerForRequest = serializer;
52+
53+
dispatcher.dispatch(path, serializerForRequest, baseRequest.getInputStream(), response.getOutputStream());
54+
55+
response.setContentType(Serializer.JSON_TYPE);
56+
response.setStatus(HttpServletResponse.SC_OK);
57+
response.getOutputStream().flush();
58+
baseRequest.setHandled(true);
59+
}
60+
}

0 commit comments

Comments
 (0)