首先在这里感谢yidongnan的开源grpc,为了整合nacos我到处找案例,找源码看,都失败了,直到使用了他的开源grpc包,最后终于解决了,也感谢yidongnan给我的邮件回复。
地址:https://github.com/yidongnan/grpc-spring-boot-starter
1.首先安装nacos,这个不细说了,百度上一大堆
2.pom-server端:
引入grpc-spring-boot-starter以及nacos-discovery:
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>net.devh</groupId> <artifactId>grpc-server-spring-boot-starter</artifactId> </dependency>
3.pom-client端
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>net.devh</groupId> <artifactId>grpc-client-spring-boot-starter</artifactId> <version>2.7.0.RELEASE</version> </dependency>
4.配置文件GrpcServerAutoConfig,主要是注解@EnableDiscoveryClient:
import net.devh.boot.grpc.server.interceptor.GlobalServerInterceptorConfigurer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @title
* @description
* @author YuPeng
* @date
*/
@Configuration
@EnableDiscoveryClient
public class GrpcServerAutoConfig {
@Bean
public GlobalServerInterceptorConfigurer globalInterceptorConfigurerAdapter() {
return registry -> registry.addServerInterceptors(new MainGrpcInterceptor());
}
}
5.默认拦截器MainGrpcInterceptor:
import lombok.extern.slf4j.Slf4j;
import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
@Slf4j
public class MainGrpcInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall, Metadata metadata,
ServerCallHandler<ReqT, RespT> serverCallHandler) {
log.info(serverCall.getMethodDescriptor().getFullMethodName());
return serverCallHandler.startCall(serverCall, metadata);
}
}
6.client端GrpcClientAutoConfig,我因为是模块化开发的,所以很简单,就一个config,主要是注解@EnableDiscoveryClient,你放在启动类上也可以:
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;
/**
* @title
* @description
* @author YuPeng
* @date
*/
@Configuration
@EnableDiscoveryClient
public class GrpcClientAutoConfig {
}
7.server端-application.yml配置:
spring:
cloud:
nacos:
discovery:
server-addr: http://192.168.18.21:8848
enabled: true
8.client端-application.yml配置:
grpc:
client:
changyun-api:
enableKeepAlive: true
keepAliveWithoutCalls: true
negotiationType: plaintext
spring:
cloud:
nacos:
discovery:
server-addr: http://192.168.18.21:8848
enabled: true
9.client-调用:
@GrpcClient("XXXXX")
private XXXXGrpc.XXXXBlockingStub XXXStub;
10.server接口:
import net.devh.boot.grpc.server.service.GrpcService;
@GrpcService
@Slf4j
public class XXXXGrpcImpl extends XXXXGrpc.XXXXImplBase{
@Override
public void findById(Request request, StreamObserver<Result> responseObserver) {
....
....
}
}
11.测试,调用成功:
版权声明:本文为qq_35875671原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。