diff --git a/examples/cloud-sleuth-zipkin-grpc-client/build.gradle b/examples/cloud-sleuth-zipkin-grpc-client/build.gradle new file mode 100644 index 000000000..f0f8f2323 --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-client/build.gradle @@ -0,0 +1,42 @@ +plugins { + id 'org.springframework.boot' +} + +def discoveryProvider = project.findProperty('discovery') ?: 'consul'; + +dependencies { + switch (discoveryProvider) { + case "nacos": { + implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery' + break; + } + case "consul": { + implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery' + break; + } + case "eureka": { + implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' + break; + } + case "zookeeper": { + implementation 'org.springframework.cloud:spring-cloud-starter-zookeeper-discovery' + break; + } + } + + implementation project(':grpc-client-spring-boot-starter') // Replace with actual dependency "net.devh:grpc-client-spring-boot-starter:${springBootGrpcVersion}" + implementation project(':examples:grpc-lib') // Replace with your grpc interface spec + + // For demonstration only + implementation 'org.springframework.boot:spring-boot-starter-actuator' + implementation 'org.springframework.boot:spring-boot-starter-web' + + // zipkin and sleuth + implementation "org.springframework.cloud:spring-cloud-starter-zipkin:2.2.7.RELEASE" + implementation "org.springframework.cloud:spring-cloud-starter-sleuth" + implementation "io.zipkin.brave:brave-instrumentation-grpc:5.13.2" +} + +bootRun { + args = ["--spring.profiles.active=" + discoveryProvider] +} diff --git a/examples/cloud-sleuth-zipkin-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/CloudSleuthClientApplication.java b/examples/cloud-sleuth-zipkin-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/CloudSleuthClientApplication.java new file mode 100644 index 000000000..b6916fc21 --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/CloudSleuthClientApplication.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2016-2021 Michael Zhang + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.devh.boot.grpc.examples.cloud.client; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * Example grpc client application using cloud discovery. + */ +@EnableDiscoveryClient +@SpringBootApplication +public class CloudSleuthClientApplication { + + /** + * Starts the grpc cloud discovery client application. + * + * @param args The arguments to pass to the application. + */ + public static void main(final String... args) { + SpringApplication.run(CloudSleuthClientApplication.class, args); + } + +} diff --git a/examples/cloud-sleuth-zipkin-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/GrpcClientController.java b/examples/cloud-sleuth-zipkin-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/GrpcClientController.java new file mode 100644 index 000000000..0e013e5c6 --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/GrpcClientController.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2016-2021 Michael Zhang + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.devh.boot.grpc.examples.cloud.client; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * Optional demo controller making the grpc service accessible via browser requests. + */ +@RestController +public class GrpcClientController { + + @Autowired + private GrpcClientService grpcClientService; + + @RequestMapping("/") + public String printMessage(@RequestParam(defaultValue = "Michael") final String name) { + return this.grpcClientService.sendMessage(name); + } + +} diff --git a/examples/cloud-sleuth-zipkin-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/GrpcClientService.java b/examples/cloud-sleuth-zipkin-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/GrpcClientService.java new file mode 100644 index 000000000..015445bc5 --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-client/src/main/java/net/devh/boot/grpc/examples/cloud/client/GrpcClientService.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2016-2021 Michael Zhang + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.devh.boot.grpc.examples.cloud.client; + +import org.springframework.stereotype.Service; + +import io.grpc.StatusRuntimeException; +import lombok.extern.slf4j.Slf4j; +import net.devh.boot.grpc.client.inject.GrpcClient; +import net.devh.boot.grpc.examples.lib.HelloReply; +import net.devh.boot.grpc.examples.lib.HelloRequest; +import net.devh.boot.grpc.examples.lib.SimpleGrpc.SimpleBlockingStub; + +/** + * Example class demonstrating the usage of {@link GrpcClient}s inside an application. + */ +@Service +@Slf4j +public class GrpcClientService { + + @GrpcClient("cloud-grpc-server") + private SimpleBlockingStub simpleStub; + + public String sendMessage(final String name) { + try { + final HelloReply response = this.simpleStub.sayHello(HelloRequest.newBuilder().setName(name).build()); + return response.getMessage(); + } catch (final StatusRuntimeException e) { + log.error("Request failed", e); + return "FAILED with " + e.getStatus().getCode(); + } + } + +} diff --git a/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-consul.yml b/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-consul.yml new file mode 100644 index 000000000..a8894a62e --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-consul.yml @@ -0,0 +1,7 @@ +spring: + cloud: + consul: + discovery: + register: false +# hostname: localhost +# port: 8500 diff --git a/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-eureka.yml b/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-eureka.yml new file mode 100644 index 000000000..3290476e4 --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-eureka.yml @@ -0,0 +1,10 @@ +eureka: + instance: + prefer-ip-address: true + status-page-url-path: /actuator/info + health-check-url-path: /actuator/health + client: + register-with-eureka: false + fetch-registry: true + service-url: + defaultZone: http://localhost:8761/eureka/ diff --git a/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-nacos.yml b/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-nacos.yml new file mode 100644 index 000000000..5f352bc1e --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-nacos.yml @@ -0,0 +1,6 @@ +spring: + cloud: + nacos: + discovery: + register-enabled: false +# server-addr: localhost:8848 diff --git a/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-zookeeper.yml b/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-zookeeper.yml new file mode 100644 index 000000000..712947cb4 --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application-zookeeper.yml @@ -0,0 +1,6 @@ +spring: + cloud: + zookeeper: +# connect-string: localhost:2181 + discovery: + registry: false diff --git a/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application.yml b/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application.yml new file mode 100644 index 000000000..3132a9add --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-client/src/main/resources/application.yml @@ -0,0 +1,19 @@ +server: + port: 8080 +spring: + application: + name: cloud-sleuth-zipkin-grpc-client + zipkin: + base-url: http://localhost:9411 + enabled: true + sleuth: + sampler: + probability: 1 + +grpc: + client: + cloud-grpc-server: + address: 'discovery:///cloud-sleuth-zipkin-grpc-server' + enableKeepAlive: true + keepAliveWithoutCalls: true + negotiationType: plaintext diff --git a/examples/cloud-sleuth-zipkin-grpc-server/build.gradle b/examples/cloud-sleuth-zipkin-grpc-server/build.gradle new file mode 100644 index 000000000..45ca16789 --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-server/build.gradle @@ -0,0 +1,42 @@ +plugins { + id 'org.springframework.boot' +} + +def discoveryProvider = project.findProperty('discovery') ?: 'consul'; + +dependencies { + switch (discoveryProvider) { + case "nacos": { + implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery' + break; + } + case "consul": { + implementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery' + break; + } + case "eureka": { + implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' + break; + } + case "zookeeper": { + implementation 'org.springframework.cloud:spring-cloud-starter-zookeeper-discovery' + break; + } + } + + implementation project(':grpc-server-spring-boot-starter') // Replace with actual dependency "net.devh:grpc-server-spring-boot-starter:${springBootGrpcVersion}" + implementation project(':examples:grpc-lib') // Replace with your grpc interface spec + + // For demonstration only + implementation 'org.springframework.boot:spring-boot-starter-actuator' + implementation 'org.springframework.boot:spring-boot-starter-web' + + // zipkin and sleuth + implementation "org.springframework.cloud:spring-cloud-starter-zipkin:2.2.7.RELEASE" + implementation "org.springframework.cloud:spring-cloud-starter-sleuth" + implementation "io.zipkin.brave:brave-instrumentation-grpc:5.13.2" +} + +bootRun { + args = ["--spring.profiles.active=" + discoveryProvider] +} diff --git a/examples/cloud-sleuth-zipkin-grpc-server/src/main/java/net/devh/boot/grpc/examples/cloud/server/CloudSleuthServerApplication.java b/examples/cloud-sleuth-zipkin-grpc-server/src/main/java/net/devh/boot/grpc/examples/cloud/server/CloudSleuthServerApplication.java new file mode 100644 index 000000000..d01a490b3 --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-server/src/main/java/net/devh/boot/grpc/examples/cloud/server/CloudSleuthServerApplication.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2016-2021 Michael Zhang + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.devh.boot.grpc.examples.cloud.server; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * Example grpc service application supporting cloud discovery. + */ +@EnableDiscoveryClient +@SpringBootApplication +public class CloudSleuthServerApplication { + + /** + * Starts the grpc cloud server application. + * + * @param args The arguments to pass to the application. + */ + public static void main(final String... args) { + SpringApplication.run(CloudSleuthServerApplication.class, args); + } + +} diff --git a/examples/cloud-sleuth-zipkin-grpc-server/src/main/java/net/devh/boot/grpc/examples/cloud/server/GrpcServerService.java b/examples/cloud-sleuth-zipkin-grpc-server/src/main/java/net/devh/boot/grpc/examples/cloud/server/GrpcServerService.java new file mode 100644 index 000000000..64b88c29d --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-server/src/main/java/net/devh/boot/grpc/examples/cloud/server/GrpcServerService.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2016-2021 Michael Zhang + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package net.devh.boot.grpc.examples.cloud.server; + +import io.grpc.stub.StreamObserver; +import net.devh.boot.grpc.examples.lib.HelloReply; +import net.devh.boot.grpc.examples.lib.HelloRequest; +import net.devh.boot.grpc.examples.lib.SimpleGrpc; +import net.devh.boot.grpc.server.service.GrpcService; + +/** + * Example grpc server service implementation class. + */ +@GrpcService +public class GrpcServerService extends SimpleGrpc.SimpleImplBase { + + @Override + public void sayHello(final HelloRequest req, final StreamObserver responseObserver) { + final HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + req.getName()).build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } + +} diff --git a/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-consul.yml b/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-consul.yml new file mode 100644 index 000000000..1113caba6 --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-consul.yml @@ -0,0 +1,7 @@ +#spring: +# cloud: +# consul: +# discovery: +# register: true +# hostname: localhost +# port: 8500 diff --git a/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-eureka.yml b/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-eureka.yml new file mode 100644 index 000000000..721b5b88a --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-eureka.yml @@ -0,0 +1,8 @@ +eureka: + instance: + prefer-ip-address: true + client: + register-with-eureka: true + fetch-registry: false + service-url: + defaultZone: http://localhost:8761/eureka/ diff --git a/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-nacos.yml b/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-nacos.yml new file mode 100644 index 000000000..161d7399f --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-nacos.yml @@ -0,0 +1,6 @@ +#spring: +# cloud: +# nacos: +# discovery: +# register-enabled: true +# server-addr: localhost:8848 diff --git a/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-zookeeper.yml b/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-zookeeper.yml new file mode 100644 index 000000000..38550ce0d --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application-zookeeper.yml @@ -0,0 +1,6 @@ +#spring: +# cloud: +# zookeeper: +# connect-string: localhost:2181 +# discovery: +# register: true diff --git a/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application.yml b/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application.yml new file mode 100644 index 000000000..598f45af9 --- /dev/null +++ b/examples/cloud-sleuth-zipkin-grpc-server/src/main/resources/application.yml @@ -0,0 +1,15 @@ +server: + port: 58080 + +spring: + application: + name: cloud-sleuth-zipkin-grpc-server + zipkin: + base-url: http://localhost:9411 + enabled: true + sleuth: + sampler: + probability: 1 +grpc: + server: + port: 0 diff --git a/settings.gradle b/settings.gradle index 7897a0ad0..6fbb350ca 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,3 +17,5 @@ include "examples:cloud-grpc-client" include "examples:cloud-grpc-server" include "examples:security-grpc-client" include "examples:security-grpc-server" +include "examples:cloud-sleuth-zipkin-grpc-client" +include "examples:cloud-sleuth-zipkin-grpc-server"