Skip to content

Commit

Permalink
add spring cloud & java chassis interoperability (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Jan 19, 2024
1 parent 4a78672 commit e215bec
Show file tree
Hide file tree
Showing 23 changed files with 1,367 additions and 0 deletions.
35 changes: 35 additions & 0 deletions java-chassis-interoprability/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# About Spring Cloud and Java Chassis interoperability

In this demo, we build a gateway using spring-cloud-gateway, a microservice `provider-java-chassis` using Java Chassis, a microservice `provider-spring-cloud` using Spring Cloud.

Scenario 1: User -> gateway -> provider-spring-cloud -> provider-java-chassis

Request:

`http://localhost:9090/spring-cloud/sayHello?name=World`

Result:

`"Hello from Java Chassis, World"`


Scenario 2: User -> gateway -> provider-java-chassis -> provider-spring-cloud

Request:

`http://localhost:9090/java-chassis/sayHello?name=World`

Result:

`"Hello from Spring Cloud, World"`

## Using Service Center & Kie

Maven profile and Spring Profile using `servicecomb`.

## Using Nacos

Maven profile and Spring Profile using `nacos`.

>>> Notice: Nacos 2.3.0 and above is required. Because older versions of nacos will not generate instance id for Spring Cloud applications and Java Chassis interoperability needs instance id.
115 changes: 115 additions & 0 deletions java-chassis-interoprability/java-chassis-3.0.x/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?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.
-->

<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.interoprability</groupId>
<artifactId>interoprability-parent</artifactId>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>java-chassis-3.0.x</artifactId>
<packaging>pom</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<servicecomb.version>3.0.1</servicecomb.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-dependencies</artifactId>
<version>${servicecomb.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>solution-basic</artifactId>
</dependency>
<!-- using log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>

<profiles>
<profile>
<id>servicecomb</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<!-- using service-center & kie -->
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>registry-service-center</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>config-kie</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>nacos</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<dependencies>
<!-- using nacos -->
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>config-nacos</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>

<modules>
<module>provider</module>
</modules>

</project>
50 changes: 50 additions & 0 deletions java-chassis-interoprability/java-chassis-3.0.x/provider/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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.
-->

<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.interoprability</groupId>
<artifactId>java-chassis-3.0.x</artifactId>
<version>3.0-SNAPSHOT</version>
</parent>

<artifactId>java-chassis-provider-3.0.x</artifactId>
<packaging>jar</packaging>

<properties>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.servicecomb</groupId>
<artifactId>java-chassis-spring-boot-starter-standalone</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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;

@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) throws Exception {
try {
new SpringApplicationBuilder()
.web(WebApplicationType.NONE)
.sources(ProviderApplication.class)
.run(args);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 = "ProviderController")
@RequestMapping(path = "/java-chassis")
public class ProviderController {
@RpcReference(schemaId = "ProviderController", microserviceName = "provider-spring-cloud")
private SpringCloudService springCloudService;

@GetMapping("/sayHello")
public String sayHello(@RequestParam("name") String name) throws Exception {
return springCloudService.sayHelloInternal(name);
}

@GetMapping("/sayHelloInternal")
public String sayHelloInternal(@RequestParam("name") String name) throws Exception {
return "Hello from Java Chassis, " + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RequestMapping("/spring-cloud")
public interface SpringCloudService {
@GetMapping("/sayHelloInternal")
String sayHelloInternal(@RequestParam("name") String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#
## ---------------------------------------------------------------------------
## 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: provider-java-chassis
environment: production
version: 0.0.1
rest:
address: 0.0.0.0:9093

accesslog:
enabled: true
metrics:
window_time: 60000
invocation:
latencyDistribution: 0,10,50,100,1000
Consumer.invocation.slow:
enabled: true
msTime: 50
Provider.invocation.slow:
enabled: true
msTime: 50
publisher.defaultLog:
enabled: true
endpoints.client.detail.enabled: true

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

Loading

0 comments on commit e215bec

Please sign in to comment.