基于grp consul实现简单微服务
1 构建consul
consul 本机运行 /usr/local/bin/consul agent -dev -ui=true -client 0.0.0.0 &
http://127.0.0.1:8500/ui/dc1/services
定义proto
代码语言:txt复制syntax = "proto3";
option java_package = "cn.beckbi.pb";
option java_outer_classname = "AdInfo";
message Ad {
int32 id = 1;
string name = 2;
string description = 3;
float price = 4;
}
message AdId {
int32 id = 1;
}
service AdRpc {
rpc addAd(Ad) returns (AdId);
rpc getAd(AdId) returns (Ad);
}
2 运行server
代码语言:txt复制 <dependencies>
<dependency>
<groupId>cn.beckbi</groupId>
<artifactId>kgrpc-springboot-lib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-server-spring-boot-starter</artifactId>
</dependency>
</dependencies>
server代码
代码语言:txt复制package cn.beckbi.service;
import cn.beckbi.pb.AdInfo;
import cn.beckbi.pb.AdRpcGrpc;
import io.grpc.Status;
import io.grpc.StatusException;
import net.devh.boot.grpc.server.service.GrpcService;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.logging.Logger;
/**
* @program: kgrpc
* @description:
* @author: bikang
* @create: 2022-09-20 23:02
*/
@GrpcService
public class AdServiceImpl extends AdRpcGrpc.AdRpcImplBase {
private static final Logger logger = Logger.getLogger(AdServiceImpl.class.getName());
private final Random random = new Random();
private final Map<Integer, AdInfo.Ad> adInfoMap = new HashMap<>(50);
@Override
public void addAd(cn.beckbi.pb.AdInfo.Ad request,
io.grpc.stub.StreamObserver<cn.beckbi.pb.AdInfo.AdId> responseObserver) {
int id = random.nextInt(100000000);
logger.info("id:" id);
request = request.toBuilder().setId(id).build();
adInfoMap.put(id, request);
AdInfo.AdId adId = AdInfo.AdId.newBuilder().setId(id).build();
responseObserver.onNext(adId);
responseObserver.onCompleted();
}
@Override
public void getAd(cn.beckbi.pb.AdInfo.AdId request,
io.grpc.stub.StreamObserver<cn.beckbi.pb.AdInfo.Ad> responseObserver) {
int id = request.getId();
logger.info("id:" id);
if (adInfoMap.containsKey(id)) {
responseObserver.onNext((AdInfo.Ad) adInfoMap.get(id));
responseObserver.onCompleted();
}else {
responseObserver.onError(new StatusException(Status.NOT_FOUND));
}
}
}
3 运行client
代码语言:txt复制 <dependencies>
<dependency>
<groupId>cn.beckbi</groupId>
<artifactId>kgrpc-springboot-lib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
</dependency>
</dependencies>
client代码
代码语言:txt复制@Slf4j
@Service
public class GrpcClientService {
@GrpcClient("kgrpc-server-producer")
private Channel serverChannel;
public AdInfo.Ad createAd(String name) {
AdRpcGrpc.AdRpcBlockingStub stub = AdRpcGrpc.newBlockingStub(serverChannel);
AdInfo.AdId adId = stub.addAd(AdInfo.Ad.newBuilder()
.setName("cpl1")
.setDescription(name)
.setPrice(31.21f)
.build());
log.info("set ad " adId.getId() " add success !");
AdInfo.Ad ad = stub.getAd(adId);
log.info("Ad: " ad.toString());
return ad;
}
}
附录
代码路径: https://github.com/beckbikang/kgrpc/tree/main/kgrpc-springboot