Skip to content

Commit cff64fb

Browse files
authored
Merge pull request #504 from xieyucan/dev
2 parents 7782f60 + 154217f commit cff64fb

File tree

14 files changed

+364
-17
lines changed

14 files changed

+364
-17
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ allprojects { project ->
167167
imports {
168168
mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
169169
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
170+
mavenBom "com.alibaba.cloud:spring-cloud-alibaba-dependencies:${springCloudAlibabaNacosVersion}"
170171
mavenBom "com.google.protobuf:protobuf-bom:${protobufVersion}"
171172
mavenBom "com.google.guava:guava-bom:${guavaVersion}"
172173
mavenBom "io.grpc:grpc-bom:${grpcVersion}"

docs/zh-CN/examples.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
- [客户端](https://github.com/yidongnan/grpc-spring-boot-starter/tree/master/examples/cloud-grpc-client)
2424
- [说明](https://github.com/yidongnan/grpc-spring-boot-starter/tree/master/examples#cloud-mode)
2525

26+
## Cloud-Nacos 示例
27+
28+
使用 nacos 服务发现的 Cloud 环境。
29+
30+
- [服务端](https://github.com/yidongnan/grpc-spring-boot-starter/tree/master/examples/cloud-grpc-server-nacos)
31+
- [客户端](https://github.com/yidongnan/grpc-spring-boot-starter/tree/master/examples/cloud-grpc-client-nacos)
32+
- [说明](https://github.com/yidongnan/grpc-spring-boot-starter/tree/master/examples#cloud-mode)
33+
2634
## 基础认证示例
2735

2836
演示了 grpc 跟 spring security 的设置。 为了简单起见,此设置使用 Basic 身份验证,但也可以为其使用其他身份验证机制。
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
plugins {
2+
id 'org.springframework.boot'
3+
}
4+
5+
dependencies {
6+
implementation 'org.springframework.boot:spring-boot-starter-web'
7+
implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
8+
implementation project(':grpc-client-spring-boot-starter') // replace to implementation("net.devh:grpc-client-spring-boot-starter:${springBootGrpcVersion}")
9+
implementation project(':examples:grpc-lib')
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2016-2021 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 [email protected] on 2021/3/5 8:00 下午
26+
* @version 1.0.0
27+
*/
28+
@EnableDiscoveryClient
29+
@SpringBootApplication
30+
public class CloudGrpcNacosClientApplication {
31+
32+
public static void main(String[] args) {
33+
SpringApplication.run(CloudGrpcNacosClientApplication.class, args);
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2016-2021 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 javax.annotation.Resource;
21+
22+
import org.springframework.web.bind.annotation.GetMapping;
23+
import org.springframework.web.bind.annotation.RequestParam;
24+
import org.springframework.web.bind.annotation.RestController;
25+
26+
/**
27+
* @author [email protected] on 2021/3/5 8:00 下午
28+
* @version 1.0.0
29+
*/
30+
@RestController
31+
public class GrpcClientController {
32+
33+
@Resource
34+
private GrpcClientService clientService;
35+
36+
@GetMapping("/")
37+
public String msg(@RequestParam(defaultValue = "Michael") String name) {
38+
return clientService.sendMessage(name);
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2016-2021 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 net.devh.boot.grpc.client.inject.GrpcClient;
24+
import net.devh.boot.grpc.examples.lib.HelloReply;
25+
import net.devh.boot.grpc.examples.lib.HelloRequest;
26+
import net.devh.boot.grpc.examples.lib.SimpleGrpc;
27+
28+
/**
29+
* @author [email protected] on 2021/3/5 8:01 下午
30+
* @version 1.0.0
31+
*/
32+
@Service
33+
public class GrpcClientService {
34+
35+
@GrpcClient("cloud-grpc-server-nacos")
36+
private SimpleGrpc.SimpleBlockingStub blockingStub;
37+
38+
public String sendMessage(final String name) {
39+
try {
40+
final HelloReply response = this.blockingStub.sayHello(HelloRequest.newBuilder().setName(name).build());
41+
return response.getMessage();
42+
} catch (final StatusRuntimeException e) {
43+
return "FAILED with " + e.getStatus().getCode().name();
44+
}
45+
}
46+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
server:
2+
port: 8080
3+
4+
spring:
5+
application:
6+
name: cloud-grpc-client-nacos
7+
sleuth:
8+
sampler:
9+
probability: 1
10+
cloud:
11+
nacos:
12+
discovery:
13+
server-addr: 127.0.0.1:8848
14+
15+
grpc:
16+
client:
17+
cloud-grpc-server-nacos:
18+
enableKeepAlive: true
19+
keepAliveWithoutCalls: true
20+
negotiationType: plaintext
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
plugins {
2+
id 'org.springframework.boot'
3+
}
4+
5+
dependencies {
6+
implementation 'org.springframework.boot:spring-boot-starter-web'
7+
implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
8+
implementation project(':grpc-server-spring-boot-starter')
9+
implementation project(':examples:grpc-lib')
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2016-2021 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 [email protected] on 2021/3/5 7:44 下午
26+
* @version 1.0.0
27+
*/
28+
@EnableDiscoveryClient
29+
@SpringBootApplication
30+
public class CloudGrpcNacosServerApplication {
31+
32+
public static void main(String[] args) {
33+
SpringApplication.run(CloudGrpcNacosServerApplication.class, args);
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2016-2021 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+
* @author [email protected] on 2021/3/5 7:44 下午
28+
* @version 1.0.0
29+
*/
30+
@GrpcService
31+
public class GrpcServerService extends SimpleGrpc.SimpleImplBase {
32+
33+
@Override
34+
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
35+
HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + request.getName()).build();
36+
responseObserver.onNext(reply);
37+
responseObserver.onCompleted();
38+
}
39+
}

0 commit comments

Comments
 (0)