diff --git a/java-chassis-interoprability/README.md b/java-chassis-interoprability/README.md new file mode 100644 index 0000000..1b963d7 --- /dev/null +++ b/java-chassis-interoprability/README.md @@ -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. + diff --git a/java-chassis-interoprability/java-chassis-3.0.x/pom.xml b/java-chassis-interoprability/java-chassis-3.0.x/pom.xml new file mode 100644 index 0000000..fc1bb12 --- /dev/null +++ b/java-chassis-interoprability/java-chassis-3.0.x/pom.xml @@ -0,0 +1,115 @@ + + + + + 4.0.0 + + + org.apache.servicecomb.interoprability + interoprability-parent + 3.0-SNAPSHOT + + + java-chassis-3.0.x + pom + + + UTF-8 + 3.0.1 + + + + + + org.apache.servicecomb + java-chassis-dependencies + ${servicecomb.version} + pom + import + + + + + + + org.apache.servicecomb + solution-basic + + + + org.apache.logging.log4j + log4j-slf4j-impl + + + org.apache.logging.log4j + log4j-api + + + org.apache.logging.log4j + log4j-core + + + com.lmax + disruptor + 3.4.4 + + + + + + servicecomb + + true + + + + + org.apache.servicecomb + registry-service-center + + + org.apache.servicecomb + config-kie + + + + + nacos + + false + + + + + org.apache.servicecomb + registry-nacos + + + org.apache.servicecomb + config-nacos + + + + + + + provider + + + diff --git a/java-chassis-interoprability/java-chassis-3.0.x/provider/pom.xml b/java-chassis-interoprability/java-chassis-3.0.x/provider/pom.xml new file mode 100644 index 0000000..4d8aa8c --- /dev/null +++ b/java-chassis-interoprability/java-chassis-3.0.x/provider/pom.xml @@ -0,0 +1,50 @@ + + + + + 4.0.0 + + + org.apache.servicecomb.interoprability + java-chassis-3.0.x + 3.0-SNAPSHOT + + + java-chassis-provider-3.0.x + jar + + + + + + + org.apache.servicecomb + java-chassis-spring-boot-starter-standalone + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderApplication.java b/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderApplication.java new file mode 100644 index 0000000..f429b34 --- /dev/null +++ b/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderApplication.java @@ -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(); + } + } +} diff --git a/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderController.java b/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderController.java new file mode 100644 index 0000000..92230e1 --- /dev/null +++ b/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderController.java @@ -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; + } +} diff --git a/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/java/org/apache/servicecomb/samples/SpringCloudService.java b/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/java/org/apache/servicecomb/samples/SpringCloudService.java new file mode 100644 index 0000000..2f92ba6 --- /dev/null +++ b/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/java/org/apache/servicecomb/samples/SpringCloudService.java @@ -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); +} diff --git a/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/resources/application.yml b/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/resources/application.yml new file mode 100644 index 0000000..126a73f --- /dev/null +++ b/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/resources/application.yml @@ -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 + diff --git a/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/resources/log4j2.xml b/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/resources/log4j2.xml new file mode 100644 index 0000000..53ed8e9 --- /dev/null +++ b/java-chassis-interoprability/java-chassis-3.0.x/provider/src/main/resources/log4j2.xml @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/java-chassis-interoprability/pom.xml b/java-chassis-interoprability/pom.xml new file mode 100644 index 0000000..efc69ef --- /dev/null +++ b/java-chassis-interoprability/pom.xml @@ -0,0 +1,67 @@ + + + + + 4.0.0 + + org.apache.servicecomb.interoprability + interoprability-parent + 3.0-SNAPSHOT + pom + + + UTF-8 + 3.11.0 + 3.1.3 + + + + java-chassis-3.0.x + spring-cloud-2022.0.x + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + -parameters + 17 + 17 + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot-maven-plugin.version} + + + + repackage + + + + + + + + diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/pom.xml b/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/pom.xml new file mode 100644 index 0000000..f89233d --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/pom.xml @@ -0,0 +1,71 @@ + + + + 4.0.0 + + + org.apache.servicecomb.interoprability + spring-cloud-application-2022.0.x + 3.0-SNAPSHOT + + + spring-cloud-gateway-2022.0.x + jar + + + + + + + servicecomb + + true + + + + + com.huaweicloud + spring-cloud-starter-huawei-service-engine-gateway + + + + + nacos + + false + + + + + com.huaweicloud + spring-cloud-starter-huawei-nacos-gateway + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/java/org/apache/servicecomb/samples/GatewayApplication.java b/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/java/org/apache/servicecomb/samples/GatewayApplication.java new file mode 100644 index 0000000..4177bf0 --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/java/org/apache/servicecomb/samples/GatewayApplication.java @@ -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 GatewayApplication { + public static void main(String[] args) throws Exception { + try { + new SpringApplicationBuilder() + .web(WebApplicationType.REACTIVE) + .sources(GatewayApplication.class) + .run(args); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/resources/application.yaml b/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/resources/application.yaml new file mode 100644 index 0000000..cf7daa4 --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/resources/application.yaml @@ -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. +## --------------------------------------------------------------------------- +spring: + cloud: + gateway: + routes: + - id: provider-java-chassis + uri: lb://provider-java-chassis + predicates: + - Path=/java-chassis/** + - id: provider-spring-cloud + uri: lb://provider-spring-cloud + predicates: + - Path=/spring-cloud/** + + httpclient: + pool: + maxConnections: 50 + maxIdleTime: PT300S + maxLifeTime: PT600S + evictionInterval: PT600S + type: FIXED diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/resources/bootstrap.yml b/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..1f631ec --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/resources/bootstrap.yml @@ -0,0 +1,101 @@ +# +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +# +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +server: + port: 9090 + +# Profile:注册中心类型:servicecomb/nacos +spring: + profiles: + active: servicecomb + + application: + # 微服务名称。 微服务名称定义好以后,后续不能变更。 + name: gateway + +# CSE 注册配置中心 +--- +spring: + activate: + on-profile: servicecomb + cloud: + servicecomb: + # 微服务的基本信息 + service: + # 微服务名称,和spring.application.name保持一致。 + name: ${spring.application.name} + # 微服务版本号,本示例使用ServiceStage环境变量。建议保留这种配置方式, + # 部署的时候,不用手工修改版本号,防止契约注册失败。 + version: ${CAS_INSTANCE_VERSION:0.0.2} + # 应用名称。默认情况下只有应用名称相同的微服务之间才可以相互发现。 + application: ${CAS_APPLICATION_NAME:basic-application} + # 环境名称。只有环境名称相同的微服务之间才可以相互发现。 + # 可以取值 development, testing, acceptance, production + environment: production + # 注册发现相关配置 + discovery: + # 注册中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式, + # 部署的时候,不用手工修改地址。 + address: ${PAAS_CSE_SC_ENDPOINT:http://127.0.0.1:30100} + # 微服务向CSE发送心跳间隔时间,单位秒 + healthCheckInterval: 10 + # 拉取实例的轮询时间,单位毫秒 + pollInterval: 15000 + # 优雅停机设置。优雅停机后,先从注册中心注销自己。这个时间表示注销自己后等待的时间,这个时间后才退出。 + waitTimeForShutDownInMillis: 15000 + config: + # 配置中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式, + # 部署的时候,不用手工修改地址。 + serverAddr: ${PAAS_CSE_CC_ENDPOINT:http://127.0.0.1:30110} + serverType: kie + +# Nacos 注册配置中心 +--- +spring: + activate: + on-profile: nacos + cloud: + nacos: + discovery: + enabled: true + server-addr: http://127.0.0.1:8848 + service: ${spring.application.name} + group: ${CAS_APPLICATION_NAME:basic-application} + namespace: production + config: + enabled: true + server-addr: http://127.0.0.1:8848 + group: ${CAS_APPLICATION_NAME:basic-application} + namespace: production + file-extension: yaml diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/resources/log4j2.xml b/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/resources/log4j2.xml new file mode 100644 index 0000000..5680024 --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/gateway/src/main/resources/log4j2.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/pom.xml b/java-chassis-interoprability/spring-cloud-2022.0.x/pom.xml new file mode 100644 index 0000000..fa054d6 --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/pom.xml @@ -0,0 +1,83 @@ + + + + + 4.0.0 + + + org.apache.servicecomb.interoprability + interoprability-parent + 3.0-SNAPSHOT + + + spring-cloud-application-2022.0.x + pom + + + UTF-8 + 2022.0.2 + 3.0.7 + 1.11.0-2022.0.x + + + + + + com.huaweicloud + spring-cloud-huawei-dependencies + ${spring-cloud-huawei.version} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-logging + + + + + io.micrometer + micrometer-registry-prometheus + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + com.lmax + disruptor + 3.4.4 + + + + + provider + gateway + + + diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/provider/pom.xml b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/pom.xml new file mode 100644 index 0000000..4d7df6a --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/pom.xml @@ -0,0 +1,85 @@ + + + + + 4.0.0 + + + org.apache.servicecomb.interoprability + spring-cloud-application-2022.0.x + 3.0-SNAPSHOT + + + spring-cloud-provider-2022.0.x + jar + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-logging + + + + + + + + servicecomb + + true + + + + + com.huaweicloud + spring-cloud-starter-huawei-service-engine + + + + + nacos + + false + + + + + com.huaweicloud + spring-cloud-starter-huawei-nacos + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/JavaChassisManagementEndpoint.java b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/JavaChassisManagementEndpoint.java new file mode 100644 index 0000000..b327ae4 --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/JavaChassisManagementEndpoint.java @@ -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. + */ +package org.apache.servicecomb.samples; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.apache.servicecomb.foundation.common.utils.ResourceUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(("/scb/management")) +public class JavaChassisManagementEndpoint { + private static final Logger LOGGER = LoggerFactory.getLogger(JavaChassisManagementEndpoint.class); + + @Autowired + private Registration registration; + + @GetMapping(path = "/health") + public boolean health(@RequestParam("instanceId") String instanceId, + @RequestParam("registryName") String registryName) { + return "sc-registry".equals(registryName) && registration.getInstanceId().equals(instanceId); + } + + @PostMapping(path = "/schema/contents") + public Map schemaContents() { + try { + List resourceUris = ResourceUtil.findResourcesBySuffix("export", ".yaml"); + Map result = new HashMap<>(resourceUris.size()); + for (URI uri : resourceUris) { + String path = uri.toURL().getPath(); + String[] segments = path.split("/"); + if (segments.length < 2 || !"export".equals(segments[segments.length - 2])) { + continue; + } + result.put(segments[segments.length - 1].substring(0, segments[segments.length - 1].indexOf(".yaml")), + IOUtils.toString(uri, StandardCharsets.UTF_8)); + } + return result; + } catch (IOException | URISyntaxException e) { + LOGGER.error("Load schema ids failed from microservices. {}.", e.getMessage()); + return Collections.emptyMap(); + } + } +} diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/JavaChassisService.java b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/JavaChassisService.java new file mode 100644 index 0000000..bc0c8fd --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/JavaChassisService.java @@ -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.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@FeignClient(name = "provider-java-chassis", path = "/java-chassis") +public interface JavaChassisService { + @GetMapping("/sayHelloInternal") + String sayHelloInternal(@RequestParam("name") String name); +} diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderApplication.java b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderApplication.java new file mode 100644 index 0000000..65e070b --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderApplication.java @@ -0,0 +1,38 @@ +/* + * 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.cloud.openfeign.EnableFeignClients; + +@SpringBootApplication +@EnableFeignClients +public class ProviderApplication { + public static void main(String[] args) throws Exception { + try { + new SpringApplicationBuilder() + .web(WebApplicationType.SERVLET) + .sources(ProviderApplication.class) + .run(args); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderController.java b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderController.java new file mode 100644 index 0000000..d3c1aae --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/java/org/apache/servicecomb/samples/ProviderController.java @@ -0,0 +1,45 @@ +/* + * 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.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(path = "/spring-cloud") +public class ProviderController { + private JavaChassisService javaChassisService; + + @Autowired + public void setJavaChassisService(JavaChassisService javaChassisService) { + this.javaChassisService = javaChassisService; + } + + @GetMapping("/sayHello") + public String sayHello(@RequestParam("name") String name) throws Exception { + return javaChassisService.sayHelloInternal(name); + } + + @GetMapping("/sayHelloInternal") + public String sayHelloInternal(@RequestParam("name") String name) throws Exception { + return "Hello from Spring Cloud, " + name; + } +} diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/resources/bootstrap.yml b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..e0e9398 --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/resources/bootstrap.yml @@ -0,0 +1,84 @@ +# +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +server: + port: 9092 + +# Profile:注册中心类型:servicecomb/nacos +spring: + profiles: + active: servicecomb # 注册中心类型:servicecomb 或者 nacos + + application: + # 微服务名称。 微服务名称定义好以后,后续不能变更。 + name: provider-spring-cloud + +# CSE 注册配置中心 +--- +spring: + activate: + on-profile: servicecomb + cloud: + servicecomb: + # 微服务的基本信息 + service: + # 微服务名称,和spring.application.name保持一致。 + name: ${spring.application.name} + # 微服务版本号,本示例使用ServiceStage环境变量。建议保留这种配置方式, + # 部署的时候,不用手工修改版本号,防止契约注册失败。 + version: ${CAS_INSTANCE_VERSION:0.0.2} + # 应用名称。默认情况下只有应用名称相同的微服务之间才可以相互发现。 + application: ${CAS_APPLICATION_NAME:basic-application} + # 环境名称。只有环境名称相同的微服务之间才可以相互发现。 + # 可以取值 development, testing, acceptance, production + environment: production + # 注册发现相关配置 + discovery: + # 注册中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式, + # 部署的时候,不用手工修改地址。 + address: ${PAAS_CSE_SC_ENDPOINT:http://127.0.0.1:30100} + # 微服务向CSE发送心跳间隔时间,单位秒 + healthCheckInterval: 10 + # 拉取实例的轮询时间,单位毫秒 + pollInterval: 15000 + # 优雅停机设置。优雅停机后,先从注册中心注销自己。这个时间表示注销自己后等待的时间,这个时间后才退出。 + waitTimeForShutDownInMillis: 15000 + config: + # 配置中心地址,本示例使用ServiceStage环境变量。建议保留这种配置方式, + # 部署的时候,不用手工修改地址。 + serverAddr: ${PAAS_CSE_CC_ENDPOINT:http://127.0.0.1:30110} + serverType: kie + +# Nacos 注册配置中心 +--- +spring: + activate: + on-profile: nacos + cloud: + nacos: + discovery: + enabled: true + server-addr: http://127.0.0.1:8848 + service: ${spring.application.name} + group: ${CAS_APPLICATION_NAME:basic-application} + namespace: production + config: + enabled: true + server-addr: http://127.0.0.1:8848 + group: ${CAS_APPLICATION_NAME:basic-application} + namespace: production + file-extension: yaml diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/resources/export/ProviderController.yaml b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/resources/export/ProviderController.yaml new file mode 100644 index 0000000..462bfe4 --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/resources/export/ProviderController.yaml @@ -0,0 +1,52 @@ +openapi: 3.0.1 +info: + title: swagger definition for org.apache.servicecomb.samples.ProviderController + version: 1.0.0 +servers: +- url: /spring-cloud +paths: + /sayHello: + get: + operationId: sayHello + parameters: + - name: name + in: query + required: true + schema: + type: string + responses: + "200": + description: response of 200 + content: + application/json: + schema: + type: string + application/protobuf: + schema: + type: string + text/plain: + schema: + type: string + /sayHelloInternal: + get: + operationId: sayHelloInternal + parameters: + - name: name + in: query + required: true + schema: + type: string + responses: + "200": + description: response of 200 + content: + application/json: + schema: + type: string + application/protobuf: + schema: + type: string + text/plain: + schema: + type: string +components: {} diff --git a/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/resources/log4j2.xml b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/resources/log4j2.xml new file mode 100644 index 0000000..4105c13 --- /dev/null +++ b/java-chassis-interoprability/spring-cloud-2022.0.x/provider/src/main/resources/log4j2.xml @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +