From 1370036ef9e6905070d4037c597da9ebfce9e032 Mon Sep 17 00:00:00 2001 From: jixianzhao Date: Sat, 8 Apr 2017 23:09:07 +0800 Subject: [PATCH 1/5] add logback config file, and add springboot module into root pom.xml --- pom.xml | 1 + .../java/com/example/grpc/springboot/Cmd.java | 5 +++-- .../grpc-client/src/main/resources/logback.xml | 17 +++++++++++++++++ .../grpc/springboot/EchoServiceImpl.java | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 springboot/grpc-client/src/main/resources/logback.xml diff --git a/pom.xml b/pom.xml index a5f64e5..2f3db2b 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,7 @@ chat-javafx-client metadata-context-example zipkin-prometheus-example + springboot diff --git a/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java b/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java index 3e55d86..5c7a18e 100644 --- a/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java +++ b/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java @@ -20,6 +20,7 @@ import com.example.echo.EchoServiceGrpc; import io.grpc.Channel; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.autoconfigure.grpc.client.GrpcChannelFactory; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @@ -32,7 +33,7 @@ @EnableDiscoveryClient public class Cmd { @Autowired - public Cmd(ApplicationArguments args, GrpcChannelFactory channelFactory) { + public Cmd(ApplicationArguments args, @Qualifier("discoveryClientChannelFactory") GrpcChannelFactory channelFactory) { System.out.println("hello"); Channel channel = channelFactory.createChannel("EchoService"); @@ -45,7 +46,7 @@ public Cmd(ApplicationArguments args, GrpcChannelFactory channelFactory) { i++; try { - Thread.sleep(100L); + Thread.sleep(10_000L); } catch (InterruptedException e) { } } diff --git a/springboot/grpc-client/src/main/resources/logback.xml b/springboot/grpc-client/src/main/resources/logback.xml new file mode 100644 index 0000000..239f6c2 --- /dev/null +++ b/springboot/grpc-client/src/main/resources/logback.xml @@ -0,0 +1,17 @@ + + + + + + + + UTF-8 + %date{yyyy-MM-dd HH:mm:ss} [%thread] %-5level [%file:%line] - %msg%n + + + + + + + + diff --git a/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java b/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java index b6829bb..10054a8 100644 --- a/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java +++ b/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java @@ -28,7 +28,7 @@ public class EchoServiceImpl implements EchoServiceGrpc.EchoService { @Override public void echo(EchoOuterClass.Echo request, StreamObserver responseObserver) { - System.out.println(request); + System.out.println("#################################################################" + request); responseObserver.onNext(request); responseObserver.onCompleted(); } From b6b4a143e99ca17348f04a26bb16d353ce6584a3 Mon Sep 17 00:00:00 2001 From: jixianzhao Date: Fri, 14 Apr 2017 19:55:32 +0800 Subject: [PATCH 2/5] refactor grpc client. --- .../src/main/resources/application.properties | 5 --- .../src/main/resources/application.yml | 17 +++++++++ springboot/grpc-client/pom.xml | 17 +++++++++ .../java/com/example/grpc/springboot/Cmd.java | 38 ++++++++++--------- .../example/grpc/springboot/GrpcClient.java | 4 -- .../grpc/springboot/EchoServiceImpl.java | 5 ++- .../src/main/resources/application.properties | 3 -- .../src/main/resources/application.yml | 19 ++++++++++ 8 files changed, 78 insertions(+), 30 deletions(-) delete mode 100644 springboot/eureka-server/src/main/resources/application.properties create mode 100644 springboot/eureka-server/src/main/resources/application.yml delete mode 100644 springboot/grpc-server/src/main/resources/application.properties create mode 100644 springboot/grpc-server/src/main/resources/application.yml diff --git a/springboot/eureka-server/src/main/resources/application.properties b/springboot/eureka-server/src/main/resources/application.properties deleted file mode 100644 index 89272de..0000000 --- a/springboot/eureka-server/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -server.port=8761 -spring.application.name=Eureka Server -eureka.client.registerWithEureka=false -eureka.client.fetchRegistry=false -eureka.client.server.waitTimeInMsWhenSyncEmpty=0 diff --git a/springboot/eureka-server/src/main/resources/application.yml b/springboot/eureka-server/src/main/resources/application.yml new file mode 100644 index 0000000..7d47295 --- /dev/null +++ b/springboot/eureka-server/src/main/resources/application.yml @@ -0,0 +1,17 @@ +server: + port: 8761 +spring: + application: + name: Eureka Server + + +eureka: + server: + enableSelfPreservation: false + instance: + leaseRenewalIntervalInSeconds: 3 + client: + registerWithEureka: false + fetchRegistry: false + server: + waitTimeInMsWhenSyncEmpty: 0 diff --git a/springboot/grpc-client/pom.xml b/springboot/grpc-client/pom.xml index 90845dc..e5da3d0 100644 --- a/springboot/grpc-client/pom.xml +++ b/springboot/grpc-client/pom.xml @@ -66,6 +66,23 @@ grpc-client-starter 1.0.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.cloud + spring-cloud-starter-config + + + org.springframework.cloud + spring-cloud-starter-eureka + + + org.springframework.cloud + spring-cloud-starter-feign + diff --git a/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java b/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java index 5c7a18e..2f724ec 100644 --- a/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java +++ b/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java @@ -21,9 +21,10 @@ import io.grpc.Channel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.ApplicationArguments; import org.springframework.boot.autoconfigure.grpc.client.GrpcChannelFactory; +import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** @@ -32,23 +33,26 @@ @Component @EnableDiscoveryClient public class Cmd { - @Autowired - public Cmd(ApplicationArguments args, @Qualifier("discoveryClientChannelFactory") GrpcChannelFactory channelFactory) { - System.out.println("hello"); + private int i = 0; + private GrpcChannelFactory channelFactory; + private DiscoveryClient discoveryClient; - Channel channel = channelFactory.createChannel("EchoService"); + @Autowired + public Cmd(@Qualifier("discoveryClientChannelFactory") GrpcChannelFactory channelFactory, + DiscoveryClient discoveryClient ) { + this.channelFactory = channelFactory; + this.discoveryClient = discoveryClient; + } - int i = 0; - while (true) { - EchoServiceGrpc.EchoServiceBlockingStub stub = EchoServiceGrpc.newBlockingStub(channel); - EchoOuterClass.Echo response = stub.echo(EchoOuterClass.Echo.newBuilder().setMessage("Hello " + i).build()); - System.out.println(response); - i++; + @Scheduled(fixedRate = 5000) + public void requestRegular() { + System.out.println("hello"); + Channel channel = channelFactory.createChannel("EchoService"); + discoveryClient.getServices(); - try { - Thread.sleep(10_000L); - } catch (InterruptedException e) { - } - } - } + EchoServiceGrpc.EchoServiceBlockingStub stub = EchoServiceGrpc.newBlockingStub(channel); + EchoOuterClass.Echo response = stub.echo(EchoOuterClass.Echo.newBuilder().setMessage("Hello " + i).build()); + System.out.println(response); + i++; + } } diff --git a/springboot/grpc-client/src/main/java/com/example/grpc/springboot/GrpcClient.java b/springboot/grpc-client/src/main/java/com/example/grpc/springboot/GrpcClient.java index 5f4331c..6177c34 100644 --- a/springboot/grpc-client/src/main/java/com/example/grpc/springboot/GrpcClient.java +++ b/springboot/grpc-client/src/main/java/com/example/grpc/springboot/GrpcClient.java @@ -16,18 +16,14 @@ package com.example.grpc.springboot; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.ApplicationArguments; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; -import org.springframework.stereotype.Component; /** * Created by rayt on 5/18/16. */ @SpringBootApplication -@Component @EnableEurekaClient public class GrpcClient { public static void main(String[] args) { diff --git a/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java b/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java index 10054a8..ff8eb23 100644 --- a/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java +++ b/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java @@ -26,9 +26,12 @@ */ @GrpcService(EchoServiceGrpc.class) public class EchoServiceImpl implements EchoServiceGrpc.EchoService { + + + @Override public void echo(EchoOuterClass.Echo request, StreamObserver responseObserver) { - System.out.println("#################################################################" + request); + System.out.println("####################" + request + " port "); responseObserver.onNext(request); responseObserver.onCompleted(); } diff --git a/springboot/grpc-server/src/main/resources/application.properties b/springboot/grpc-server/src/main/resources/application.properties deleted file mode 100644 index 5775383..0000000 --- a/springboot/grpc-server/src/main/resources/application.properties +++ /dev/null @@ -1,3 +0,0 @@ -grpc.server.port=${server.port:9090} -spring.application.name=EchoService -eureka.instance.nonSecurePort=${server.port} diff --git a/springboot/grpc-server/src/main/resources/application.yml b/springboot/grpc-server/src/main/resources/application.yml new file mode 100644 index 0000000..c906441 --- /dev/null +++ b/springboot/grpc-server/src/main/resources/application.yml @@ -0,0 +1,19 @@ +port: 9000 +grpc: + server: + port: ${port} +spring: + application: + name: EchoService +server: + port: ${port} +#eureka.instance.instanceId=instance-2 +#eureka.instance.nonSecurePort=${server.port:9091} +#eureka.instance.nonSecurePort=${server.port} + + +eureka: + instance: + nonSecurePort: ${port} + leaseRenewalIntervalInSeconds: 1 + leaseExpirationDurationInSeconds: 2 From c8a76f06502948610c1ddb241f81680dd5db173f Mon Sep 17 00:00:00 2001 From: jixianzhao Date: Sun, 16 Apr 2017 13:07:02 +0800 Subject: [PATCH 3/5] spring-boot-starter-parent 1.5.2.RELEASE. --- springboot/grpc-client/pom.xml | 63 +++++++++++-------- .../grpc-client/src/main/proto/Echo.proto | 11 ++++ .../grpc/springboot/EchoServiceImpl.java | 4 +- springboot/pom.xml | 39 +++++++++++- 4 files changed, 85 insertions(+), 32 deletions(-) create mode 100644 springboot/grpc-client/src/main/proto/Echo.proto diff --git a/springboot/grpc-client/pom.xml b/springboot/grpc-client/pom.xml index e5da3d0..c8642f8 100644 --- a/springboot/grpc-client/pom.xml +++ b/springboot/grpc-client/pom.xml @@ -27,6 +27,15 @@ 4.0.0 springboot-grpc-client + + + + 1.0.0 + 3.0.0-beta-2 + 0.5.0 + + + org.springframework.cloud @@ -42,25 +51,6 @@ - - ${project.groupId} - springboot-grpc-server - ${project.version} - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-web - - - com.github.saturnism.springboot - grpc-server-starter - - - com.github.saturnism.springboot grpc-client-starter @@ -71,14 +61,6 @@ org.springframework.boot spring-boot-starter-actuator - - org.springframework.cloud - spring-cloud-starter-config - - - org.springframework.cloud - spring-cloud-starter-eureka - org.springframework.cloud spring-cloud-starter-feign @@ -92,5 +74,32 @@ os-maven-plugin + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + ${protoc.plugin.version} + + com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} + grpc-java + io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} + + + + + compile + compile-custom + + + + + + kr.motd.maven + os-maven-plugin + ${os.plugin.version} + + + diff --git a/springboot/grpc-client/src/main/proto/Echo.proto b/springboot/grpc-client/src/main/proto/Echo.proto new file mode 100644 index 0000000..296d058 --- /dev/null +++ b/springboot/grpc-client/src/main/proto/Echo.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package com.example.echo; + +message Echo { + string message = 1; +} + +service EchoService { + rpc echo(Echo) returns (Echo); +} diff --git a/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java b/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java index ff8eb23..ad5de32 100644 --- a/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java +++ b/springboot/grpc-server/src/main/java/com/example/grpc/springboot/EchoServiceImpl.java @@ -25,9 +25,7 @@ * Created by rayt on 5/18/16. */ @GrpcService(EchoServiceGrpc.class) -public class EchoServiceImpl implements EchoServiceGrpc.EchoService { - - +public class EchoServiceImpl extends EchoServiceGrpc.EchoServiceImplBase { @Override public void echo(EchoOuterClass.Echo request, StreamObserver responseObserver) { diff --git a/springboot/pom.xml b/springboot/pom.xml index 9fc6c33..8ff71fb 100644 --- a/springboot/pom.xml +++ b/springboot/pom.xml @@ -14,8 +14,17 @@ pom - 1.3.5.RELEASE - Brixton.RELEASE + 1.5.2.RELEASE + Dalston.RELEASE + 1.0.0 + 3.0.0-beta-2 + 0.5.0 + 1.5.2.RELEASE + 1.8 + 1.8 + 1.1.33.Fork14 + 1.0.0 + 1.4.0.Final @@ -57,6 +66,32 @@ spring-boot-maven-plugin ${spring.boot.version} + + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + ${protoc.plugin.version} + + com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} + grpc-java + io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} + + + + + compile + compile-custom + + + + + + kr.motd.maven + os-maven-plugin + ${os.plugin.version} + From bd0279419bc57f547398bf84a4a7bea8d509b354 Mon Sep 17 00:00:00 2001 From: jixianzhao Date: Sun, 16 Apr 2017 14:37:20 +0800 Subject: [PATCH 4/5] remove parent pom from module springboot. --- springboot/grpc-client/pom.xml | 26 +++++++++++++++++--------- springboot/pom.xml | 12 ++++-------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/springboot/grpc-client/pom.xml b/springboot/grpc-client/pom.xml index c8642f8..fec2d27 100644 --- a/springboot/grpc-client/pom.xml +++ b/springboot/grpc-client/pom.xml @@ -27,16 +27,24 @@ 4.0.0 springboot-grpc-client - - - - 1.0.0 - 3.0.0-beta-2 - 0.5.0 - - - + + + io.grpc + grpc-netty + ${grpc.version} + + + io.grpc + grpc-protobuf + ${grpc.version} + + + io.grpc + grpc-stub + ${grpc.version} + + org.springframework.cloud spring-cloud-starter-eureka diff --git a/springboot/pom.xml b/springboot/pom.xml index 8ff71fb..586b80a 100644 --- a/springboot/pom.xml +++ b/springboot/pom.xml @@ -2,28 +2,24 @@ - - grpc-demos - com.example - 1.0-SNAPSHOT - .. - + 4.0.0 + com.example springboot + 1.0-SNAPSHOT pom 1.5.2.RELEASE Dalston.RELEASE 1.0.0 - 3.0.0-beta-2 + 3.0.0 0.5.0 1.5.2.RELEASE 1.8 1.8 1.1.33.Fork14 - 1.0.0 1.4.0.Final From fb7a738a3c23c8b985af443e947589413fb3d93c Mon Sep 17 00:00:00 2001 From: jixianzhao Date: Sun, 16 Apr 2017 15:34:34 +0800 Subject: [PATCH 5/5] arrange pom.xml's dependencies. --- .../java/com/example/grpc/springboot/Cmd.java | 4 ++-- .../src/main/resources/application.properties | 4 +++- springboot/grpc-server/pom.xml | 16 ++++++++++++++++ .../src/main/resources/application.yml | 2 +- springboot/pom.xml | 2 +- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java b/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java index 2f724ec..bce8a37 100644 --- a/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java +++ b/springboot/grpc-client/src/main/java/com/example/grpc/springboot/Cmd.java @@ -48,11 +48,11 @@ public Cmd(@Qualifier("discoveryClientChannelFactory") GrpcChannelFactory channe public void requestRegular() { System.out.println("hello"); Channel channel = channelFactory.createChannel("EchoService"); - discoveryClient.getServices(); +// discoveryClient.getServices(); + i++; EchoServiceGrpc.EchoServiceBlockingStub stub = EchoServiceGrpc.newBlockingStub(channel); EchoOuterClass.Echo response = stub.echo(EchoOuterClass.Echo.newBuilder().setMessage("Hello " + i).build()); System.out.println(response); - i++; } } diff --git a/springboot/grpc-client/src/main/resources/application.properties b/springboot/grpc-client/src/main/resources/application.properties index 601fd12..fd1c651 100644 --- a/springboot/grpc-client/src/main/resources/application.properties +++ b/springboot/grpc-client/src/main/resources/application.properties @@ -13,7 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. # - +port=19001 +server.port=19000 spring.application.name=grpc-client +spring.main.web-environment=false grpc.client.channels.EchoService.discovery=true diff --git a/springboot/grpc-server/pom.xml b/springboot/grpc-server/pom.xml index f1b2365..b594e45 100644 --- a/springboot/grpc-server/pom.xml +++ b/springboot/grpc-server/pom.xml @@ -26,6 +26,22 @@ + + io.grpc + grpc-netty + ${grpc.version} + + + io.grpc + grpc-protobuf + ${grpc.version} + + + io.grpc + grpc-stub + ${grpc.version} + + org.springframework.boot spring-boot-starter diff --git a/springboot/grpc-server/src/main/resources/application.yml b/springboot/grpc-server/src/main/resources/application.yml index c906441..0a2c798 100644 --- a/springboot/grpc-server/src/main/resources/application.yml +++ b/springboot/grpc-server/src/main/resources/application.yml @@ -1,4 +1,4 @@ -port: 9000 +port: 9004 grpc: server: port: ${port} diff --git a/springboot/pom.xml b/springboot/pom.xml index 586b80a..b0b5e10 100644 --- a/springboot/pom.xml +++ b/springboot/pom.xml @@ -19,7 +19,7 @@ 1.5.2.RELEASE 1.8 1.8 - 1.1.33.Fork14 + 2.0.0.Final 1.4.0.Final