From 16b41540cab1f347f990f579925a80ea26c3a90d Mon Sep 17 00:00:00 2001 From: Ahoo Wang Date: Wed, 9 Jun 2021 23:38:40 +0800 Subject: [PATCH] add examples (Service-Consumer--- RPC ->> Service-Provider) --- .../cosky-service-consumer/build.gradle.kts | 30 +++++++++++++++ .../service/consumer/ConsumerServer.java | 37 +++++++++++++++++++ .../src/main/resources/bootstrap.yaml | 16 ++++++++ .../build.gradle.kts | 7 ++++ .../examples/service/provider/Constants.java | 9 +++++ .../service/provider/api/HelloApi.java | 15 ++++++++ .../service/provider/client/HelloClient.java | 12 ++++++ .../cosky-service-provider/build.gradle.kts | 30 +++++++++++++++ .../service/provider/ProviderServer.java | 14 +++++++ .../provider/controller/HelloController.java | 18 +++++++++ .../src/main/resources/bootstrap.yaml | 18 +++++++++ settings.gradle.kts | 8 ++++ 12 files changed, 214 insertions(+) create mode 100644 examples/cosky-service-consumer/build.gradle.kts create mode 100644 examples/cosky-service-consumer/src/main/java/me/ahoo/cosky/examples/service/consumer/ConsumerServer.java create mode 100644 examples/cosky-service-consumer/src/main/resources/bootstrap.yaml create mode 100644 examples/cosky-service-provider-api/build.gradle.kts create mode 100644 examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/Constants.java create mode 100644 examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/api/HelloApi.java create mode 100644 examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/client/HelloClient.java create mode 100644 examples/cosky-service-provider/build.gradle.kts create mode 100644 examples/cosky-service-provider/src/main/java/me/ahoo/cosky/examples/service/provider/ProviderServer.java create mode 100644 examples/cosky-service-provider/src/main/java/me/ahoo/cosky/examples/service/provider/controller/HelloController.java create mode 100644 examples/cosky-service-provider/src/main/resources/bootstrap.yaml diff --git a/examples/cosky-service-consumer/build.gradle.kts b/examples/cosky-service-consumer/build.gradle.kts new file mode 100644 index 00000000..9a31108b --- /dev/null +++ b/examples/cosky-service-consumer/build.gradle.kts @@ -0,0 +1,30 @@ +plugins { + application +} + +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(8)) + } +} + +dependencies { + implementation(project(":examples:cosky-service-provider-api")) + + implementation(platform(project(":cosky-dependencies"))) + implementation("io.springfox:springfox-boot-starter") + implementation("org.springframework.boot:spring-boot-starter-actuator") + implementation(project(":spring-cloud-starter-cosky-config")) + implementation(project(":spring-cloud-starter-cosky-discovery")) + implementation("com.google.guava:guava") + implementation("org.springframework.cloud:spring-cloud-starter-loadbalancer") + implementation("org.springframework.cloud:spring-cloud-starter-openfeign") + compileOnly("org.projectlombok:lombok:${rootProject.ext.get("lombokVersion")}") + annotationProcessor("org.springframework.boot:spring-boot-configuration-processor:${rootProject.ext.get("springBootVersion")}") + annotationProcessor("org.projectlombok:lombok:${rootProject.ext.get("lombokVersion")}") + testImplementation("org.springframework.boot:spring-boot-starter-test") +} + +tasks.withType { + useJUnitPlatform() +} diff --git a/examples/cosky-service-consumer/src/main/java/me/ahoo/cosky/examples/service/consumer/ConsumerServer.java b/examples/cosky-service-consumer/src/main/java/me/ahoo/cosky/examples/service/consumer/ConsumerServer.java new file mode 100644 index 00000000..3b858cd2 --- /dev/null +++ b/examples/cosky-service-consumer/src/main/java/me/ahoo/cosky/examples/service/consumer/ConsumerServer.java @@ -0,0 +1,37 @@ +package me.ahoo.cosky.examples.service.consumer; + +import lombok.extern.slf4j.Slf4j; +import lombok.var; +import me.ahoo.cosky.examples.service.provider.client.HelloClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; + +/** + * @author ahoo wang + */ +@Slf4j +@SpringBootApplication +@EnableFeignClients(basePackages = {"me.ahoo.cosky.examples.service.provider.client"}) +public class ConsumerServer implements CommandLineRunner { + public static void main(String[] args) { + SpringApplication.run(ConsumerServer.class, args); + } + + @Autowired + private HelloClient helloClient; + + /** + * Callback used to run the bean. + * + * @param args incoming main method arguments + * @throws Exception on error + */ + @Override + public void run(String... args) throws Exception { + var rpcResponse = helloClient.hi("consumer"); + log.warn(rpcResponse); + } +} diff --git a/examples/cosky-service-consumer/src/main/resources/bootstrap.yaml b/examples/cosky-service-consumer/src/main/resources/bootstrap.yaml new file mode 100644 index 00000000..74ff0b99 --- /dev/null +++ b/examples/cosky-service-consumer/src/main/resources/bootstrap.yaml @@ -0,0 +1,16 @@ +spring: + application: + name: ${service.name:example-consumer} + cloud: + cosky: + namespace: ${cosky.namespace:{dev}} + config: + config-id: ${spring.application.name}.yaml + redis: + mode: ${cosky.redis.mode:standalone} + url: ${cosky.redis.uri:redis://localhost:6379} +logging: + file: + name: logs/${spring.application.name}.log + + diff --git a/examples/cosky-service-provider-api/build.gradle.kts b/examples/cosky-service-provider-api/build.gradle.kts new file mode 100644 index 00000000..8bc6d390 --- /dev/null +++ b/examples/cosky-service-provider-api/build.gradle.kts @@ -0,0 +1,7 @@ +plugins { + `java-library` +} + +dependencies { + api("org.springframework.cloud:spring-cloud-openfeign-core") +} diff --git a/examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/Constants.java b/examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/Constants.java new file mode 100644 index 00000000..6bb7c9bc --- /dev/null +++ b/examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/Constants.java @@ -0,0 +1,9 @@ +package me.ahoo.cosky.examples.service.provider; + +/** + * @author ahoo wang + */ +public final class Constants { + public static final String SERVICE_NAME = "example-provider"; + public static final String SERVICE_NAME_PREFIX = SERVICE_NAME + "."; +} diff --git a/examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/api/HelloApi.java b/examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/api/HelloApi.java new file mode 100644 index 00000000..07668f9a --- /dev/null +++ b/examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/api/HelloApi.java @@ -0,0 +1,15 @@ +package me.ahoo.cosky.examples.service.provider.api; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +/** + * @author ahoo wang + */ +public interface HelloApi { + String PATH = "hello"; + + @GetMapping("hi/{name}") + String hi(@PathVariable("name") String name); + +} diff --git a/examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/client/HelloClient.java b/examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/client/HelloClient.java new file mode 100644 index 00000000..adabb146 --- /dev/null +++ b/examples/cosky-service-provider-api/src/main/java/me/ahoo/cosky/examples/service/provider/client/HelloClient.java @@ -0,0 +1,12 @@ +package me.ahoo.cosky.examples.service.provider.client; + +import me.ahoo.cosky.examples.service.provider.Constants; +import me.ahoo.cosky.examples.service.provider.api.HelloApi; +import org.springframework.cloud.openfeign.FeignClient; + +/** + * @author ahoo wang + */ +@FeignClient(name = Constants.SERVICE_NAME, contextId = Constants.SERVICE_NAME_PREFIX + "HelloClient", path = HelloApi.PATH) +public interface HelloClient extends HelloApi { +} diff --git a/examples/cosky-service-provider/build.gradle.kts b/examples/cosky-service-provider/build.gradle.kts new file mode 100644 index 00000000..4300b078 --- /dev/null +++ b/examples/cosky-service-provider/build.gradle.kts @@ -0,0 +1,30 @@ +plugins { + application +} + +java { + toolchain { + languageVersion.set(JavaLanguageVersion.of(8)) + } +} + +dependencies { + implementation(project(":examples:cosky-service-provider-api")) + + implementation(platform(project(":cosky-dependencies"))) + implementation("io.springfox:springfox-boot-starter") + implementation("org.springframework.boot:spring-boot-starter-actuator") + implementation(project(":spring-cloud-starter-cosky-config")) + implementation(project(":spring-cloud-starter-cosky-discovery")) + implementation("com.google.guava:guava") + implementation("org.springframework.boot:spring-boot-starter-web") + implementation("org.springframework.cloud:spring-cloud-starter-openfeign") + compileOnly("org.projectlombok:lombok:${rootProject.ext.get("lombokVersion")}") + annotationProcessor("org.springframework.boot:spring-boot-configuration-processor:${rootProject.ext.get("springBootVersion")}") + annotationProcessor("org.projectlombok:lombok:${rootProject.ext.get("lombokVersion")}") + testImplementation("org.springframework.boot:spring-boot-starter-test") +} + +tasks.withType { + useJUnitPlatform() +} diff --git a/examples/cosky-service-provider/src/main/java/me/ahoo/cosky/examples/service/provider/ProviderServer.java b/examples/cosky-service-provider/src/main/java/me/ahoo/cosky/examples/service/provider/ProviderServer.java new file mode 100644 index 00000000..8c4196c7 --- /dev/null +++ b/examples/cosky-service-provider/src/main/java/me/ahoo/cosky/examples/service/provider/ProviderServer.java @@ -0,0 +1,14 @@ +package me.ahoo.cosky.examples.service.provider; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author ahoo wang + */ +@SpringBootApplication +public class ProviderServer { + public static void main(String[] args) { + SpringApplication.run(ProviderServer.class, args); + } +} diff --git a/examples/cosky-service-provider/src/main/java/me/ahoo/cosky/examples/service/provider/controller/HelloController.java b/examples/cosky-service-provider/src/main/java/me/ahoo/cosky/examples/service/provider/controller/HelloController.java new file mode 100644 index 00000000..d17ebe64 --- /dev/null +++ b/examples/cosky-service-provider/src/main/java/me/ahoo/cosky/examples/service/provider/controller/HelloController.java @@ -0,0 +1,18 @@ +package me.ahoo.cosky.examples.service.provider.controller; + +import me.ahoo.cosky.examples.service.provider.api.HelloApi; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author ahoo wang + */ +@RestController +@RequestMapping(HelloApi.PATH) +public class HelloController implements HelloApi { + + @Override + public String hi(String name) { + return "hello " + name; + } +} diff --git a/examples/cosky-service-provider/src/main/resources/bootstrap.yaml b/examples/cosky-service-provider/src/main/resources/bootstrap.yaml new file mode 100644 index 00000000..a94bf42b --- /dev/null +++ b/examples/cosky-service-provider/src/main/resources/bootstrap.yaml @@ -0,0 +1,18 @@ +server: + port: 8099 +spring: + application: + name: ${service.name:example-provider} + cloud: + cosky: + namespace: ${cosky.namespace:{dev}} + config: + config-id: ${spring.application.name}.yaml + redis: + mode: ${cosky.redis.mode:standalone} + url: ${cosky.redis.uri:redis://localhost:6379} +logging: + file: + name: logs/${spring.application.name}.log + + diff --git a/settings.gradle.kts b/settings.gradle.kts index a82b8d5c..f5c9aba3 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -29,3 +29,11 @@ buildscript { } } +include("examples:cosky-service-provider") +findProject(":examples:cosky-service-provider")?.name = "cosky-service-provider" + +include("examples:cosky-service-provider-api") +findProject(":examples:cosky-service-provider-api")?.name = "cosky-service-provider-api" + +include("examples:cosky-service-consumer") +findProject(":examples:cosky-service-consumer")?.name = "cosky-service-consumer"