Skip to content

Commit

Permalink
add tomcat example (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Oct 25, 2023
1 parent a41fab1 commit 0a2c049
Show file tree
Hide file tree
Showing 17 changed files with 903 additions and 0 deletions.
78 changes: 78 additions & 0 deletions basic-tomcat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Description
This project providers sample to show working with Java Chassis Microservices.

* provider
A Microserivce using Java Chassis with a REST interface. Provider is packaged as WAR and deploy in tomcat.

* consumer
A Microserivce using Java Chassis with a REST interface. Consumer calls provider with RPC. Consumer is packaged as WAR and deploy in tomcat.

* gateway
A Microserivce using Java Chassis Edge Service to forward requests to consumer. Gateway packaged as executable JAR.

## Precondition
see [Precondition](../README.md)
# Build and Run

* Build

mvn clean package

* Run provider

Deploy basic-provider-2.0-SNAPSHOT.war to tomcat.

* Run consumer

Deploy basic-consumer-2.0-SNAPSHOT.war to tomcat.

* Run gateway

In ${Project}/gateway/target/

java -jar basic-gateway-2.0-SNAPSHOT.jar

* Testing

Open in browser: http://localhost:9090/sayHello?name=World



# 项目说明

这个项目提供了 Java Chassis 的简单例子,例子包括:

* provider
使用 Java Chassis 开发一个 REST 接口。 Provider打包为WAR在tomcat部署。

* consumer
使用 Java Chassis 开发一个 REST 接口, 接口实现通过 RPC 调用 provider 的接口。 Consumer打包为WAR在tomcat部署。

* gateway
使用 Java Chassis Edge Service 开发一个网关, 网关将所有请求转发到 consumer。 Gateway打包为可执行JAR运行。

## 使用

* 编译

mvn clean package

* 启动 provider

将basic-provider-2.0-SNAPSHOT.war部署到Tomcat。

* 启动 consumer

将basic-consumer-2.0-SNAPSHOT.war部署到Tomcat。

* 启动 gateway

进入目录 ${Project}/gateway/target/

java -jar basic-gateway-2.0-SNAPSHOT.jar

* 测试

启动3个微服务后, 然后通过界面访问: http://localhost:9090/sayHello?name=World


65 changes: 65 additions & 0 deletions basic-tomcat/consumer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<!--
~ 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.
-->

<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.servicecomb.samples</groupId>
<artifactId>basic-application</artifactId>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>basic-consumer</artifactId>
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-spring-boot-starter-servlet</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class ConsumerApplication extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
try {
new SpringApplicationBuilder()
.web(WebApplicationType.SERVLET)
.sources(ConsumerApplication.class)
.run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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;

import org.apache.servicecomb.provider.pojo.RpcReference;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RestSchema(schemaId = "ConsumerController")
@RequestMapping(path = "/")
public class ConsumerController {
@RpcReference(schemaId = "ProviderController", microserviceName = "provider")
private ProviderService providerService;

// consumer service which delegate the implementation to provider service.
@GetMapping("/sayHello")
public String sayHello(@RequestParam("name") String name) {
return providerService.sayHello(name);
}

@GetMapping("/exampleConfig")
public String exampleConfig() {
return providerService.exampleConfig();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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;

public interface ProviderService {
String sayHello(String name);

String exampleConfig();
}
59 changes: 59 additions & 0 deletions basic-tomcat/consumer/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#
## ---------------------------------------------------------------------------
## 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.
## ---------------------------------------------------------------------------
servicecomb:
service:
application: basic-application
name: consumer
version: 0.0.1

# port should same as tomcat
rest:
address: 0.0.0.0:8080

spring:
profiles:
active: servicecomb # 注册中心类型:servicecomb 或者 nacos

---
spring:
config:
activate:
on-profile: servicecomb
servicecomb:
# 注册发现
registry:
sc:
address: http://localhost:30100
# 动态配置
kie:
serverUri: http://localhost:30110

---
spring:
config:
activate:
on-profile: nacos
servicecomb:
# 注册发现
registry:
nacos:
serverAddr: http://localhost:8848
# 动态配置
nacos:
serverAddr: http://localhost:8848

29 changes: 29 additions & 0 deletions basic-tomcat/consumer/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<Configuration status="INFO">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d][%t][%p]%m [%c:%L]%n" />
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
49 changes: 49 additions & 0 deletions basic-tomcat/gateway/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!--
~ 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.
-->

<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.servicecomb.samples</groupId>
<artifactId>basic-application</artifactId>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>basic-gateway</artifactId>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-spring-boot-starter-standalone</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>edge-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 0a2c049

Please sign in to comment.