Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# samples
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notes about the examples:

  1. we can call jaxrs/springmvc server implementation with RPC style client code, this is not our intention, but this seams a quite great feature. We can discuss if include this feature in the example. Before that, we must test any other HTTP methods like GET/POST
  2. should add client codes use RestTemplate to call jarxs/springmvc service, that's what we have given in other demos.


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
```
115 changes: 115 additions & 0 deletions samples/codefirst-sample/pom.xml
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>
47 changes: 47 additions & 0 deletions samples/codefirst-sample/readme.md
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
```
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);
}
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");
}
}
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;
}
}
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}

}
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();
}
}
Loading