Skip to content

Commit 2ee90f1

Browse files
authored
Merge pull request #475 from ZMRWEGO/master
2 parents ffc477b + b5e2f6f commit 2ee90f1

File tree

27 files changed

+647
-103
lines changed

27 files changed

+647
-103
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-consul-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,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 ConsulClientApplication {
31+
32+
public static void main(String[] args) {
33+
SpringApplication.run(ConsulClientApplication.class, args);
34+
}
35+
36+
}
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,50 @@
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.stereotype.Service;
21+
22+
import io.grpc.StatusRuntimeException;
23+
import lombok.extern.slf4j.Slf4j;
24+
import net.devh.boot.grpc.client.inject.GrpcClient;
25+
import net.devh.boot.grpc.examples.lib.HelloReply;
26+
import net.devh.boot.grpc.examples.lib.HelloRequest;
27+
import net.devh.boot.grpc.examples.lib.SimpleGrpc.SimpleBlockingStub;
28+
29+
/**
30+
* @author zhaochunlin ([email protected])
31+
* @since 2021/1/9
32+
*/
33+
@Service
34+
@Slf4j
35+
public class GrpcClientService {
36+
37+
@GrpcClient("cloud-grpc-server-consul")
38+
private SimpleBlockingStub simpleStub;
39+
40+
public String sendMessage(final String name) {
41+
try {
42+
final HelloReply response = this.simpleStub.sayHello(HelloRequest.newBuilder().setName(name).build());
43+
return response.getMessage();
44+
} catch (final StatusRuntimeException e) {
45+
log.error(e.getMessage());
46+
return "FAILED with " + e.getStatus().getCode();
47+
}
48+
}
49+
50+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
server:
2+
port: 8082
3+
spring:
4+
application:
5+
name: cloud-grpc-client-consul
6+
cloud:
7+
consul:
8+
discovery:
9+
registry: false
10+
port: 8500
11+
hostname: 127.0.0.1
12+
grpc:
13+
client:
14+
cloud-grpc-server-consul:
15+
enableKeepAlive: true
16+
keepAliveWithoutCalls: true
17+
negotiationType: plaintext
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,50 @@
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.stereotype.Service;
21+
22+
import io.grpc.StatusRuntimeException;
23+
import lombok.extern.slf4j.Slf4j;
24+
import net.devh.boot.grpc.client.inject.GrpcClient;
25+
import net.devh.boot.grpc.examples.lib.HelloReply;
26+
import net.devh.boot.grpc.examples.lib.HelloRequest;
27+
import net.devh.boot.grpc.examples.lib.SimpleGrpc.SimpleBlockingStub;
28+
29+
/**
30+
* @author zhaochunlin ([email protected])
31+
* @since 2021/1/9
32+
*/
33+
@Service
34+
@Slf4j
35+
public class GrpcClientService {
36+
37+
@GrpcClient("cloud-grpc-server-zookeeper")
38+
private SimpleBlockingStub simpleStub;
39+
40+
public String sendMessage(final String name) {
41+
try {
42+
final HelloReply response = this.simpleStub.sayHello(HelloRequest.newBuilder().setName(name).build());
43+
return response.getMessage();
44+
} catch (final StatusRuntimeException e) {
45+
log.error(e.getMessage());
46+
return "FAILED with " + e.getStatus().getCode();
47+
}
48+
}
49+
50+
}
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
server:
2+
port: 8082
3+
spring:
4+
application:
5+
name: cloud-grpc-client-zookeeper
6+
cloud:
7+
zookeeper:
8+
discovery:
9+
registry: false
10+
connect-string: localhost:2181
11+
grpc:
12+
client:
13+
cloud-grpc-server-zookeeper:
14+
enableKeepAlive: true
15+
keepAliveWithoutCalls: true
16+
negotiationType: plaintext

0 commit comments

Comments
 (0)