Skip to content

Commit

Permalink
add examples (Service-Consumer--- RPC ->> Service-Provider)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Jun 9, 2021
1 parent 5c9fe6b commit 16b4154
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/cosky-service-consumer/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<Test> {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 16 additions & 0 deletions examples/cosky-service-consumer/src/main/resources/bootstrap.yaml
Original file line number Diff line number Diff line change
@@ -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


7 changes: 7 additions & 0 deletions examples/cosky-service-provider-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
`java-library`
}

dependencies {
api("org.springframework.cloud:spring-cloud-openfeign-core")
}
Original file line number Diff line number Diff line change
@@ -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 + ".";
}
Original file line number Diff line number Diff line change
@@ -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);

}
Original file line number Diff line number Diff line change
@@ -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 {
}
30 changes: 30 additions & 0 deletions examples/cosky-service-provider/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<Test> {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions examples/cosky-service-provider/src/main/resources/bootstrap.yaml
Original file line number Diff line number Diff line change
@@ -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


8 changes: 8 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 16b4154

Please sign in to comment.