windows下的编译器 protoc-3.9.0-win64.zip,以及特定的语言编码器protobuf-java-3.9.0.zip
dependencies {
//testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'io.netty', name: 'netty-all', version: '4.1.10.Final'
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.6.1'
//针对移动端
compile group: 'com.google.protobuf', name: 'protobuf-java-util', version: '3.9.0'
}
syntax = "proto3";
package com.xuge.protobuf;
option optimize_for= SPEED;
option java_package= "com.xuge.protobuf";
option java_outer_classname="Datainfo";
message Student{
string name=1;
int32 age=2;
string address=3;
}
E:\person\nettyIDEA\src>protoc --java_out=main/java/ protobuf/Student.proto
package com.xuge.protobuf;
import com.google.protobuf.InvalidProtocolBufferException;
public class TestProtobuf {
public static void main(String[]args) throws InvalidProtocolBufferException {
Datainfo.Student student=Datainfo.Student.newBuilder().setName("张三")
.setAge(20).setAddress("北京").build();
byte[]student2ByteArray=student.toByteArray();
Datainfo.Student student2=Datainfo.Student.parseFrom(student2ByteArray);
System.out.println(student2.getName());
System.out.println(student2.getAge());
System.out.println(student2.getAddress());
}
}
ChannelPipeline pipeline=ch.pipeline();
pipeline.addLast(new ProtobufVarint32FrameDecoder());
//解码器
pipeline.addLast(new ProtobufDecoder(MyDataInfo.Student.getDefaultInstance()));//TestServerHandler
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(new ProtobufEncoder());
pipeline.addLast(new TestServerHandler());
public class TestServerHandler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.Student msg) throws Exception {
System.out.println(msg);
System.out.println(msg.getAddress());
System.out.println(msg.getAge());
}
}
MyDataInfo.Student student=MyDataInfo.Student.newBuilder().
setName("周洁").
setAge(21).
setAddress("贺州").
build();
ctx.channel().writeAndFlush(student);
syntax = "proto3";
package sixthExample;
option optimize_for= SPEED;
option java_package= "sixthExample";
option java_outer_classname="MyDataInfo";
message MyMessage{
enum DataType{
StudentType =0;
DogType=1;
CatType=2;
}
DataType data_type=1;
oneof dataBody{ //oneof多个消息中只能出现一个
Student student=2;
Dog dog=3;
Cat cat=4;
}
}
message Student{
string name=1;
int32 age=2;
string address=3;
}
message Dog{
string name=1;
int32 age=2;
}
message Cat{
string name=1;
string city=2;
}
public class TestInitalizer extends ChannelInitializer {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline=ch.pipeline();
pipeline.addLast(new ProtobufVarint32FrameDecoder());
//解码器
pipeline.addLast(new ProtobufDecoder(MyDataInfo.MyMessage.getDefaultInstance()));//TestServerHandler
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(new ProtobufEncoder());
pipeline.addLast(new TestServerHandler());
}
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
int randomInt=new Random().nextInt(3);
MyDataInfo.MyMessage myMessage=null;
if(randomInt==0){
myMessage=MyDataInfo.MyMessage.newBuilder().
setDataType(MyDataInfo.MyMessage.DataType.StudentType).
setStudent(MyDataInfo.Student.newBuilder().
setName("小康").
setAge(21).
setAddress("贺州").
build()).build();
}else if (randomInt==1){
myMessage=MyDataInfo.MyMessage.newBuilder().
setDataType(MyDataInfo.MyMessage.DataType.DogType).
setDog(MyDataInfo.Dog.newBuilder().
setName("猫").
setAge(10).
build()).build();
}else {
myMessage=MyDataInfo.MyMessage.newBuilder().
setDataType(MyDataInfo.MyMessage.DataType.CatType).
setCat(MyDataInfo.Cat.newBuilder().
setName("小康").
setCity("南宁").
build()).build();
}
ctx.channel().writeAndFlush(myMessage);
}
public class TestServerHandler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {
MyDataInfo.MyMessage.DataType dataType=msg.getDataType();
if(dataType==MyDataInfo.MyMessage.DataType.StudentType){
MyDataInfo.Student student=msg.getStudent();
System.out.println(student.getName());
System.out.println(student.getAddress());
System.out.println(student.getAge());
}else if(dataType==MyDataInfo.MyMessage.DataType.DogType){
MyDataInfo.Dog dog=msg.getDog();
System.out.println(dog.getName());
System.out.println(dog.getAge());
}else {
MyDataInfo.Cat cat=msg.getCat();
System.out.println(cat.getName());
System.out.println(cat.getCity());
}
}
}
compile group: 'org.apache.thrift', name: 'libthrift', version: '0.12.0'
namespace java thrift.generated //包的名字
typedef i16 short
typedef i32 int
typedef i64 long
typedef bool boolean
typedef string String
struct Person{
1: optional String username,
2: optional int age,
3: optional boolean married
}
exception DataException{
1: optional String message,
2: optional String callStack,
3: optional String date
}
service PersonService{
Person getPersonByUsermae(1:required String username)throws (1:DataException dataException),
void savePerson(1:required Person person)throws(1:DataException dataException)
}
如下 E:\何旭杰person\nettyIDEA\src\thrift>thrift --gen java data.thrift
package com.xuge.thrift;
import org.apache.thrift.TException;
import thrift.generated.DataException;
import thrift.generated.Person;
import thrift.generated.PersonService;
public class PersonServiceImpl implements PersonService.Iface {
@Override
public Person getPersonByUsermae(String username) throws DataException, TException {
System.out.println("Got Client Param: "+username);
Person person=new Person();
person.setUsername(username);
person.setAge(20);
person.setMarried(false);
return person;
}
@Override
public void savePerson(Person person) throws DataException, TException {
System.out.println("Got Client Param: ");
System.out.println(person.getUsername());
System.out.println(person.getAge());
System.out.println(person.isMarried());
}
}
使用命令生成python代码
thrift --gen py src/thrift/data.thrift
/thrift-0.10.0\lib\py 接着输入命令sudo python setup.py install安装
##3.2 编写python客户端代码
from py.thrift.generated import PersonService
from py.thrift.generated import ttypes
from thrift import Thrift
from thrift.transport import TSocket, TTransport
from thrift.protocol import TCompactProtocol
try:
tSocket=TSocket.TSocket('localhost',10092)
tSocket.setTimeout(600)
transport=TTransport.TFramedTransport(tSocket)
protocol=TCompactProtocol.TCompactProtocol(transport)
client=PersonService.Client(protocol)
transport.open()
person=client.getPersonByUsermae('张三')
print(person.username)
print(person.age)
print(person.married)
print('--------')
newPerson=ttypes.Person()
newPerson.username='lisi'
newPerson.age=30
newPerson.married=True
client.savePerson(newPerson)
transport.close()
except Thrift.TException as tx:
print('%s'% tx.message)
io.grpc:grpc-netty-shaded:1.22.1
io.grpc:grpc-protobuf:1.22.1
io.grpc:grpc-stub:1.22.1
apply plugin: 'com.google.protobuf'
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.2.0"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.4.0'
}
}
//自定义文件生成路径
generateProtoTasks.generatedFilesBaseDir = "src"
generateProtoTasks {
all()*.plugins {
grpc {
//自定义文件生成路径
setOutputSubDir 'java'
}
}
}
}
//注意如果编译出错,将plugins {
id 'java'
} 改成apply plugin: 'java'
src/main/proto
项目Github网址:https://github.com/332870852/netty_study