Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nacos example #504

Merged
merged 8 commits into from Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -167,6 +167,7 @@ allprojects { project ->
imports {
mavenBom "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom "com.alibaba.cloud:spring-cloud-alibaba-dependencies:${springCloudAlibabaNacosVersion}"
mavenBom "com.google.protobuf:protobuf-bom:${protobufVersion}"
mavenBom "com.google.guava:guava-bom:${guavaVersion}"
mavenBom "io.grpc:grpc-bom:${grpcVersion}"
Expand Down
8 changes: 8 additions & 0 deletions docs/zh-CN/examples.md
Expand Up @@ -23,6 +23,14 @@
- [客户端](https://github.com/yidongnan/grpc-spring-boot-starter/tree/master/examples/cloud-grpc-client)
- [说明](https://github.com/yidongnan/grpc-spring-boot-starter/tree/master/examples#cloud-mode)

## Cloud-Nacos 示例

使用 nacos 服务发现的 Cloud 环境。

- [服务端](https://github.com/yidongnan/grpc-spring-boot-starter/tree/master/examples/cloud-grpc-server-nacos)
- [客户端](https://github.com/yidongnan/grpc-spring-boot-starter/tree/master/examples/cloud-grpc-client-nacos)
- [说明](https://github.com/yidongnan/grpc-spring-boot-starter/tree/master/examples#cloud-mode)

## 基础认证示例

演示了 grpc 跟 spring security 的设置。 为了简单起见,此设置使用 Basic 身份验证,但也可以为其使用其他身份验证机制。
Expand Down
10 changes: 10 additions & 0 deletions examples/cloud-grpc-client-nacos/build.gradle
@@ -0,0 +1,10 @@
plugins {
id 'org.springframework.boot'
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
implementation project(':grpc-client-spring-boot-starter') // replace to implementation("net.devh:grpc-client-spring-boot-starter:${springBootGrpcVersion}")
implementation project(':examples:grpc-lib')
}
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2016-2021 Michael Zhang <yidongnan@gmail.com>
*
* 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;

/**
* @author xiehui1956@gmail.com on 2021/3/5 8:00 下午
* @version 1.0.0
*/
@EnableDiscoveryClient
@SpringBootApplication
public class CloudGrpcNacosClientApplication {

public static void main(String[] args) {
SpringApplication.run(CloudGrpcNacosClientApplication.class, args);
}

}
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2016-2021 Michael Zhang <yidongnan@gmail.com>
*
* 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 javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* @author xiehui1956@gmail.com on 2021/3/5 8:00 下午
* @version 1.0.0
*/
@RestController
public class GrpcClientController {

@Resource
private GrpcClientService clientService;

@GetMapping("/")
public String msg(@RequestParam(defaultValue = "Michael") String name) {
return clientService.sendMessage(name);
}

}
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2016-2021 Michael Zhang <yidongnan@gmail.com>
*
* 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 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;

/**
* @author xiehui1956@gmail.com on 2021/3/5 8:01 下午
* @version 1.0.0
*/
@Service
public class GrpcClientService {

@GrpcClient("cloud-grpc-server-nacos")
private SimpleGrpc.SimpleBlockingStub blockingStub;

public String sendMessage(final String name) {
try {
final HelloReply response = this.blockingStub.sayHello(HelloRequest.newBuilder().setName(name).build());
return response.getMessage();
} catch (final StatusRuntimeException e) {
return "FAILED with " + e.getStatus().getCode().name();
}
}
}
@@ -0,0 +1,20 @@
server:
port: 8080

spring:
application:
name: cloud-grpc-client-nacos
sleuth:
sampler:
probability: 1
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

grpc:
client:
cloud-grpc-server-nacos:
enableKeepAlive: true
keepAliveWithoutCalls: true
negotiationType: plaintext
10 changes: 10 additions & 0 deletions examples/cloud-grpc-server-nacos/build.gradle
@@ -0,0 +1,10 @@
plugins {
id 'org.springframework.boot'
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
implementation project(':grpc-server-spring-boot-starter')
implementation project(':examples:grpc-lib')
}
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2016-2021 Michael Zhang <yidongnan@gmail.com>
*
* 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;

/**
* @author xiehui1956@gmail.com on 2021/3/5 7:44 下午
* @version 1.0.0
*/
@EnableDiscoveryClient
@SpringBootApplication
public class CloudGrpcNacosServerApplication {

public static void main(String[] args) {
SpringApplication.run(CloudGrpcNacosServerApplication.class, args);
}

}
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2016-2021 Michael Zhang <yidongnan@gmail.com>
*
* 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;

/**
* @author xiehui1956@gmail.com on 2021/3/5 7:44 下午
* @version 1.0.0
*/
@GrpcService
public class GrpcServerService extends SimpleGrpc.SimpleImplBase {

@Override
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + request.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
@@ -0,0 +1,17 @@
server:
port: 0

spring:
application:
name: cloud-grpc-server-nacos
sleuth:
sampler:
probability: 1
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

grpc:
server:
port: 0
2 changes: 1 addition & 1 deletion grpc-server-spring-boot-autoconfigure/build.gradle
Expand Up @@ -21,7 +21,7 @@ dependencies {
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-zookeeper-discovery'
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
optionalSupportImplementation "com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:${springCloudAlibabaNacosVersion}"
optionalSupportImplementation "com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery"
optionalSupportImplementation 'com.google.inject:guice:4.2.3' // Only needed to avoid some warnings during compilation (for eureka)
optionalSupportImplementation 'io.zipkin.brave:brave-instrumentation-grpc'
optionalSupportApi 'io.grpc:grpc-netty'
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle
Expand Up @@ -23,3 +23,5 @@ include 'examples:cloud-grpc-server-consul'
include 'examples:cloud-grpc-client-consul'
include 'examples:cloud-grpc-server-zookeeper'
include 'examples:cloud-grpc-client-zookeeper'
include 'examples:cloud-grpc-server-nacos'
include 'examples:cloud-grpc-client-nacos'