-
Notifications
You must be signed in to change notification settings - Fork 827
add samples of java chassis #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# samples | ||
|
||
It's the samples of Java chassis | ||
1. pojo sample | ||
2. jaxrs sample | ||
3. spring mvc sample | ||
4. code first sample | ||
5. customer handler sample | ||
|
||
## 1. Start the ServiceComb/Service Center | ||
|
||
[how to start the service center](http://servicecomb.io/docs/start-sc/) | ||
|
||
## 2.Start the Microservice server | ||
|
||
```bash | ||
mvn test -Pserver | ||
``` | ||
|
||
## 3.Start the Microservice client | ||
|
||
```bash | ||
mvn test -Pclient | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>samples</artifactId> | ||
<groupId>io.servicecomb.samples</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>io.servicecomb.samples</groupId> | ||
<artifactId>codefirst-sample</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.servicecomb</groupId> | ||
<artifactId>handler-bizkeeper</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.servicecomb</groupId> | ||
<artifactId>handler-loadbalance</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.servicecomb</groupId> | ||
<artifactId>transport-highway</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.servicecomb</groupId> | ||
<artifactId>transport-grpc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.servicecomb</groupId> | ||
<artifactId>transport-rest-vertx</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.servicecomb</groupId> | ||
<artifactId>handler-flowcontrol-qps</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.servicecomb</groupId> | ||
<artifactId>swagger-generator-springmvc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.servicecomb</groupId> | ||
<artifactId>provider-springmvc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.servicecomb</groupId> | ||
<artifactId>provider-pojo</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
<profiles> | ||
<profile> | ||
<id>server</id> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<phase>test</phase> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
<configuration> | ||
<mainClass>io.servicecomb.samples.codefirst.server.CodeFirstServer</mainClass> | ||
<arguments> | ||
<argument>server.microservice.yaml</argument> | ||
</arguments> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
<profile> | ||
<id>client</id> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<phase>test</phase> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
<configuration> | ||
<mainClass>io.servicecomb.samples.codefirst.client.CodeFirstClient</mainClass> | ||
<arguments> | ||
<argument>client.microservice.yaml</argument> | ||
</arguments> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
|
||
</project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Code First | ||
|
||
It's the sample of code-first | ||
|
||
You can write microservice without swagger contract. | ||
|
||
|
||
## how to write server code | ||
|
||
Just write codes without swagger contract. When the server is starting, it will build the contract and register the service to the service center. | ||
|
||
```java | ||
@RpcSchema(schemaId = "codeFirst") | ||
public class CodeFirstPojo { | ||
public int reduce(int a, int b) { | ||
return a - b; | ||
} | ||
|
||
public Person sayHello(Person user) { | ||
user.setName("hello " + user.getName()); | ||
return user; | ||
} | ||
|
||
public String saySomething(String prefix, Person user) { | ||
return prefix + " " + user.getName(); | ||
} | ||
|
||
} | ||
``` | ||
|
||
## How to run sample | ||
|
||
### 1. Start the ServiceComb/Service Center | ||
|
||
[how to start the service center](http://servicecomb.io/docs/start-sc/) | ||
|
||
### 2.Start the Microservice server | ||
|
||
```bash | ||
mvn test -Pserver | ||
``` | ||
|
||
### 3.Start the Microservice client | ||
|
||
```bash | ||
mvn test -Pclient | ||
``` |
26 changes: 26 additions & 0 deletions
26
samples/codefirst-sample/src/main/java/io/servicecomb/samples/codefirst/Hello.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* Licensed 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 io.servicecomb.samples.codefirst; | ||
|
||
|
||
import io.servicecomb.samples.codefirst.models.Person; | ||
|
||
public interface Hello { | ||
|
||
String sayHi(String name); | ||
|
||
String sayHello(Person person); | ||
} |
60 changes: 60 additions & 0 deletions
60
...defirst-sample/src/main/java/io/servicecomb/samples/codefirst/client/CodeFirstClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* Licensed 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 io.servicecomb.samples.codefirst.client; | ||
|
||
import io.servicecomb.foundation.common.utils.BeanUtils; | ||
import io.servicecomb.foundation.common.utils.Log4jUtils; | ||
import io.servicecomb.provider.pojo.RpcReference; | ||
import io.servicecomb.samples.codefirst.Hello; | ||
import io.servicecomb.samples.codefirst.models.Person; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CodeFirstClient { | ||
|
||
@RpcReference(microserviceName = "codefirst", schemaId = "codeFirstJaxrsHello") | ||
private static Hello jaxrsHello; | ||
|
||
@RpcReference(microserviceName = "codefirst", schemaId = "codeFirstSpringmvcHello") | ||
private static Hello springmvcHello; | ||
|
||
@RpcReference(microserviceName = "codefirst", schemaId = "codeFirstHello") | ||
private static Hello hello; | ||
|
||
public static void main(String[] args) throws Exception { | ||
init(args); | ||
System.out.println(hello.sayHi("Java Chassis")); | ||
System.out.println(jaxrsHello.sayHi("Java Chassis")); | ||
System.out.println(springmvcHello.sayHi("Java Chassis")); | ||
Person person = new Person(); | ||
person.setName("ServiceComb/Java Chassis"); | ||
System.out.println(hello.sayHello(person)); | ||
System.out.println(jaxrsHello.sayHello(person)); | ||
System.out.println(springmvcHello.sayHello(person)); | ||
} | ||
|
||
public static void init(String[] args) throws Exception { | ||
if (args.length != 0){ | ||
String fileName = args[0]; | ||
if (!fileName.isEmpty()) { | ||
System.setProperty("cse.configurationSource.defaultFileName", fileName); | ||
} | ||
} | ||
Log4jUtils.init(); | ||
BeanUtils.init(); | ||
System.clearProperty("cse.configurationSource.defaultFileName"); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
samples/codefirst-sample/src/main/java/io/servicecomb/samples/codefirst/models/Person.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* Licensed 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 io.servicecomb.samples.codefirst.models; | ||
|
||
public class Person { | ||
|
||
private String name; | ||
|
||
public String getName(){ | ||
return name; | ||
} | ||
|
||
public void setName(String name){ | ||
this.name = name; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...sample/src/main/java/io/servicecomb/samples/codefirst/server/CodeFIrstJaxrsHelloImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* Licensed 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 io.servicecomb.samples.codefirst.server; | ||
|
||
|
||
import io.servicecomb.provider.pojo.RpcSchema; | ||
import io.servicecomb.samples.codefirst.Hello; | ||
import io.servicecomb.samples.codefirst.models.Person; | ||
|
||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
@RpcSchema(schemaId = "codeFirstJaxrsHello") | ||
@Path("/codefirstjaxrshello") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public class CodeFIrstJaxrsHelloImpl implements Hello { | ||
|
||
@Path("/sayhi") | ||
@POST | ||
@Override | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not an valid example I think. This example is the same as pojo example bellow. Any tags in this class can be deleted. For springmvc example, we need register it with @RestSchema and call it with rest template |
||
public String sayHi(String name) { | ||
return "Jaxrs Hello " + name; | ||
} | ||
|
||
@Path("/sayhello") | ||
@POST | ||
@Override | ||
public String sayHello(Person person) { | ||
return "Jaxrs Hello person " + person.getName(); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...-sample/src/main/java/io/servicecomb/samples/codefirst/server/CodeFirstPojoHelloImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2017 Huawei Technologies Co., Ltd | ||
* | ||
* Licensed 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 io.servicecomb.samples.codefirst.server; | ||
|
||
import io.servicecomb.provider.pojo.RpcSchema; | ||
import io.servicecomb.samples.codefirst.Hello; | ||
import io.servicecomb.samples.codefirst.models.Person; | ||
|
||
@RpcSchema(schemaId = "codeFirstHello") | ||
public class CodeFirstPojoHelloImpl implements Hello{ | ||
|
||
@Override | ||
public String sayHi(String name) { | ||
return "Pojo Hello " + name; | ||
} | ||
|
||
@Override | ||
public String sayHello(Person person) { | ||
return "Pojo Hello person " + person.getName(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
notes about the examples: