Skip to content

Commit 535a825

Browse files
committed
添加zookeeper注册中心 examples
1 parent b6cec89 commit 535a825

File tree

10 files changed

+264
-0
lines changed

10 files changed

+264
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id 'org.springframework.boot'
3+
}
4+
5+
dependencies {
6+
implementation 'org.springframework.cloud:spring-cloud-starter-zookeeper-discovery'
7+
implementation 'org.springframework.boot:spring-boot-starter-actuator'
8+
implementation 'org.springframework.boot:spring-boot-starter-web'
9+
10+
implementation project(':examples:grpc-lib')
11+
implementation project(':grpc-client-spring-boot-starter') // replace to implementation "net.devh:grpc-server-spring-boot-starter:${springBootGrpcVersion}"
12+
13+
testImplementation('org.springframework.boot:spring-boot-starter-test'){
14+
exclude module: 'junit-vintage-engine'
15+
exclude module: 'junit'
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.examples.cloud.client;
19+
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.web.bind.annotation.RequestMapping;
22+
import org.springframework.web.bind.annotation.RequestParam;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
/**
26+
* @author zhaochunlin ([email protected])
27+
* @since 2021/1/9
28+
*/
29+
@RestController
30+
public class GrpcClientController {
31+
32+
@Autowired
33+
private GrpcClientService grpcClientService;
34+
35+
@RequestMapping("/")
36+
public String printMessage(@RequestParam(defaultValue = "Michael") String name) {
37+
return grpcClientService.sendMessage(name);
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.examples.cloud.client;
19+
20+
import io.grpc.StatusRuntimeException;
21+
import lombok.extern.slf4j.Slf4j;
22+
import net.devh.boot.grpc.client.inject.GrpcClient;
23+
import net.devh.boot.grpc.examples.lib.HelloReply;
24+
import net.devh.boot.grpc.examples.lib.HelloRequest;
25+
import net.devh.boot.grpc.examples.lib.SimpleGrpc.SimpleBlockingStub;
26+
import org.springframework.stereotype.Service;
27+
28+
/**
29+
* @author zhaochunlin ([email protected])
30+
* @since 2021/1/9
31+
*/
32+
@Service
33+
@Slf4j
34+
public class GrpcClientService {
35+
36+
@GrpcClient("cloud-grpc-server-zookeeper")
37+
private SimpleBlockingStub simpleStub;
38+
39+
public String sendMessage(final String name) {
40+
try {
41+
final HelloReply response = this.simpleStub.sayHello(HelloRequest.newBuilder().setName(name).build());
42+
return response.getMessage();
43+
} catch (final StatusRuntimeException e) {
44+
log.error(e.getMessage());
45+
return "FAILED with " + e.getStatus().getCode();
46+
}
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.examples.cloud.client;
19+
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
23+
24+
/**
25+
* @author zhaochunlin ([email protected])
26+
* @since 2021/1/9
27+
*/
28+
@EnableDiscoveryClient
29+
@SpringBootApplication
30+
public class ZookeeperClientApplication {
31+
32+
public static void main(String[] args) {
33+
SpringApplication.run(ZookeeperClientApplication.class, args);
34+
}
35+
36+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server:
2+
port: 8082
3+
spring:
4+
application:
5+
name: cloud-grpc-consul-client
6+
cloud:
7+
zookeeper:
8+
discovery:
9+
registry: false
10+
connect-string: localhost:2181
11+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plugins {
2+
id 'org.springframework.boot'
3+
}
4+
5+
dependencies {
6+
implementation 'org.springframework.cloud:spring-cloud-starter-zookeeper-discovery'
7+
implementation 'org.springframework.boot:spring-boot-starter-actuator'
8+
implementation 'org.springframework.boot:spring-boot-starter-web'
9+
implementation project(':examples:grpc-lib')
10+
implementation project(':grpc-server-spring-boot-starter') // replace to implementation "net.devh:grpc-server-spring-boot-starter:${springBootGrpcVersion}"
11+
12+
testImplementation('org.springframework.boot:spring-boot-starter-test'){
13+
exclude module: 'junit-vintage-engine'
14+
exclude module: 'junit'
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.examples.cloud.server;
19+
20+
import io.grpc.stub.StreamObserver;
21+
import net.devh.boot.grpc.examples.lib.HelloReply;
22+
import net.devh.boot.grpc.examples.lib.HelloRequest;
23+
import net.devh.boot.grpc.examples.lib.SimpleGrpc;
24+
import net.devh.boot.grpc.server.service.GrpcService;
25+
26+
27+
/**
28+
* @author zhaochunlin ([email protected])
29+
* @since 2021/1/9
30+
*/
31+
@GrpcService
32+
public class GrpcServerService extends SimpleGrpc.SimpleImplBase {
33+
34+
@Override
35+
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
36+
HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + req.getName()).build();
37+
responseObserver.onNext(reply);
38+
responseObserver.onCompleted();
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2016-2020 Michael Zhang <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package net.devh.boot.grpc.examples.cloud.server;
19+
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
23+
24+
/**
25+
* @author zhaochunlin ([email protected])
26+
* @since 2021/1/9
27+
*/
28+
@EnableDiscoveryClient
29+
@SpringBootApplication
30+
public class ZookeeperServerApplication {
31+
32+
public static void main(String[] args) {
33+
SpringApplication.run(ZookeeperServerApplication.class, args);
34+
}
35+
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
server:
2+
port: 8081
3+
4+
spring:
5+
application:
6+
name: cloud-grpc-server-zookeeper
7+
cloud:
8+
zookeeper:
9+
discovery:
10+
register: true
11+
connect-string: localhost:2181
12+
13+
grpc:
14+
server:
15+
port: 9999

settings.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ include 'examples:cloud-grpc-server-consul'
2727
findProject(':examples:cloud-grpc-server-consul')?.name = 'cloud-grpc-server-consul'
2828
include 'examples:cloud-grpc-client-consul'
2929
findProject(':examples:cloud-grpc-client-consul')?.name = 'cloud-grpc-client-consul'
30+
include 'examples:cloud-grpc-server-zookeeper'
31+
findProject(':examples:cloud-grpc-server-zookeeper')?.name = 'cloud-grpc-server-zookeeper'
32+
include 'examples:cloud-grpc-client-zookeeper'
33+
findProject(':examples:cloud-grpc-client-zookeeper')?.name = 'cloud-grpc-client-zookeeper'
3034

0 commit comments

Comments
 (0)