Akka使用ProtoBuf序列化

这篇博客介绍了如何在Akka中使用ProtoBuf进行序列化。首先,通过proto文件生成Java代码,详细步骤可参考指定链接。接着,展示了pom文件配置和akka配置文件的设置。然后,给出了Actor类、集群各节点启动类的概要。最后,提到了启用ProtoBuf序列化所需的依赖项,特别是对于依赖akka-remote模块的情况。
摘要由CSDN通过智能技术生成

1、根据proto文件生成java代码,具体操作见之前的netty博客

https://blog.csdn.net/zsj777/article/details/102685200

生成的java类如下

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: cmd.proto

package sample.cluster.simple;

public final class MessageGoogle {
    private MessageGoogle() {}
    public static void registerAllExtensions(
            com.google.protobuf.ExtensionRegistryLite registry) {
    }

    public static void registerAllExtensions(
            com.google.protobuf.ExtensionRegistry registry) {
        registerAllExtensions(
                (com.google.protobuf.ExtensionRegistryLite) registry);
    }
    /**
     * Protobuf enum {@code CmdType}
     */
    public enum CmdType
            implements com.google.protobuf.ProtocolMessageEnum {
        /**
         * <code>NONE = 0;</code>
         */
        NONE(0),
        /**
         * <code>SEND = 1;</code>
         */
        SEND(1),
        /**
         * <code>RECEIVE = 2;</code>
         */
        RECEIVE(2),
        /**
         * <code>UP = 3;</code>
         */
        UP(3),
        /**
         * <code>DOWN = 4;</code>
         */
        DOWN(4),
        /**
         * <code>BROADCAST = 5;</code>
         */
        BROADCAST(5),
        /**
         * <code>UP_BROADCAST = 6;</code>
         */
        UP_BROADCAST(6),
        /**
         * <code>DOWN_BROADCAST = 7;</code>
         */
        DOWN_BROADCAST(7),
        /**
         * <code>ACK = 8;</code>
         */
        ACK(8),
        /**
         * <code>NO_ACK = 9;</code>
         */
        NO_ACK(9),
        /**
         * <code>CONSOLE = 10;</code>
         */
        CONSOLE(10),
        UNRECOGNIZED(-1),
        ;

        /**
         * <code>NONE = 0;</code>
         */
        public static final int NONE_VALUE = 0;
        /**
         * <code>SEND = 1;</code>
         */
        public static final int SEND_VALUE = 1;
        /**
         * <code>RECEIVE = 2;</code>
         */
        public static final int RECEIVE_VALUE = 2;
        /**
         * <code>UP = 3;</code>
         */
        public static final int UP_VALUE = 3;
        /**
         * <code>DOWN = 4;</code>
         */
        public static final int DOWN_VALUE = 4;
        /**
         * <code>BROADCAST = 5;</code>
         */
        public static final int BROADCAST_VALUE = 5;
        /**
         * <code>UP_BROADCAST = 6;</code>
         */
        public static final int UP_BROADCAST_VALUE = 6;
        /**
         * <code>DOWN_BROADCAST = 7;</code>
         */
        public static final int DOWN_BROADCAST_VALUE = 7;
        /**
         * <code>ACK = 8;</code>
         */
        public static final int ACK_VALUE = 8;
        /**
         * <code>NO_ACK = 9;</code>
         */
        public static final int NO_ACK_VALUE = 9;
        /**
         * <code>CONSOLE = 10;</code>
         */
        public static final int CONSOLE_VALUE = 10;


        public final int getNumber() {
            if (this == UNRECOGNIZED) {
                throw new IllegalArgumentException(
                        "Can't get the number of an unknown enum value.");
            }
            return value;
        }

        /**
         * @param value The numeric wire value of the corresponding enum entry.
         * @return The enum associated with the given numeric wire value.
         * @deprecated Use {@link #forNumber(int)} instead.
         */
        @Deprecated
        public static CmdType valueOf(int value) {
            return forNumber(value);
        }

        /**
         * @param value The numeric wire value of the corresponding enum entry.
         * @return The enum associated with the given numeric wire value.
         */
        public static CmdType forNumber(int value) {
            switch (value) {
                case 0: return NONE;
                case 1: return SEND;
                case 2: return RECEIVE;
                case 3: return UP;
                case 4: return DOWN;
                case 5: return BROADCAST;
                case 6: return UP_BROADCAST;
                case 7: return DOWN_BROADCAST;
                case 8: return ACK;
                case 9: return NO_ACK;
                case 10: return CONSOLE;
                default: return null;
            }
        }

        public static com.google.protobuf.Internal.EnumLiteMap<CmdType>
        internalGetValueMap() {
            return internalValueMap;
        }
        private static final com.google.protobuf.Internal.EnumLiteMap<
                CmdType> internalValueMap =
                new com.google.protobuf.Internal.EnumLiteMap<CmdType>() {
                    public CmdType findValueByNumber(int number) {
                        return CmdType.forNumber(number);
                    }
                };

        public final com.google.protobuf.Descriptors.EnumValueDescriptor
        getValueDescriptor() {
            return getDescriptor().getValues().get(ordinal());
        }
        public final com.google.protobuf.Descriptors.EnumDescriptor
        getDescriptorForType() {
            return getDescriptor();
        }
        public static final com.google.protobuf.Descriptors.EnumDescriptor
        getDescriptor() {
            return MessageGoogle.getDescriptor().getEnumTypes().get(0);
        }

        private static final CmdType[] VALUES = values();

        public static CmdType valueOf(
                com.google.protobuf.Descriptors.EnumValueDescriptor desc) {
            if (desc.getType() != getDescriptor()) {
                throw new IllegalArgumentException(
                        "EnumValueDescriptor is not for this type.");
            }
            if (desc.getIndex() == -1) {
                return UNRECOGNIZED;
            }
            return VALUES[desc.getIndex()];
        }

        private final int value;

        private CmdType(int value) {
            this.value = value;
        }

        // @@protoc_insertion_point(enum_scope:CmdType)
    }

    public interface ImMessageOrBuilder extends
            // @@protoc_insertion_point(interface_extends:ImMessage)
            com.google.protobuf.MessageOrBuilder {

        /**
         * <code>string from = 1;</code>
         * @return The from.
         */
        String getFrom();
        /**
         * <code>string from = 1;</code>
         * @return The bytes for from.
         */
        com.google.protobuf.ByteString
        getFromBytes();

        /**
         * <code>string to = 2;</code>
         * @return The to.
         */
        String getTo();
        /**
         * <code>string to = 2;</code>
         * @return The bytes for to.
         */
        com.google.protobuf.ByteString
        getToBytes();

        /**
         * <code>int64 msgId = 3;</code>
         * @return The msgId.
         */
        long getMsgId();

        /**
         * <code>string content = 4;</code>
         * @return The content.
         */
        String getContent();
        /**
         * <code>string content = 4;</code>
         * @return The bytes for content.
         */
        com.google.protobuf.ByteString
        getContentBytes();

        /**
         * <code>string device = 5;</code>
         * @return The device.
         */
        String getDevice();
        /**
         * <code>string device = 5;</code>
         * @return The bytes for device.
         */
        com.google.protobuf.ByteString
        getDeviceBytes();
    }
    /**
     * Protobuf type {@code ImMessage}
     */
    public  static final class ImMessage extends
            com.google.protobuf.GeneratedMessageV3 implements
            // @@protoc_insertion_point(message_implements:ImMessage)
            ImMessageOrBuilder {
        private static final long serialVersionUID = 0L;
        // Use ImMessage.newBuilder() to construct.
        private ImMessage(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
            super(builder);
        }
        private ImMessage() {
            from_ = "";
            to_ = "";
            content_ = "";
            device_ = "";
        }

        @Override
        @SuppressWarnings({"unused"})
        protected Object newInstance(
                UnusedPrivateParameter unused) {
            return new ImMessage();
        }

        @Override
        public final com.google.protobuf.UnknownFieldSet
        getUnknownFields() {
            return this.unknownFields;
        }
        private ImMessage(
                com.google.protobuf.CodedInputStream input,
                com.google.protobuf.ExtensionRegistryLite extensionRegistry)
                throws com.google.protobuf.InvalidProtocolBufferException {
            this();
            if (extensionRegistry == null) {
                throw new NullPointerException();
            }
            com.google.protobuf.UnknownFieldSet.Builder unknownFields =
                    com.google.protobuf.UnknownFieldSet.newBuilder();
            try {
                boolean done = false;
                while (!done) {
                    int tag = input.readTag();
                    switch (tag) {
                        case 0:
                            done = true;
                            break;
                        case 10: {
                            String s = input.readStringRequireUtf8();

                            from_ = s;
                            break;
                        }
                        case 18: {
                            String s = input.readStringRequireUtf8();

                            to_ = s;
                            break;
                        }
                        case 24: {

                            msgId_ = input.readInt64();
                            break;
                        }
                        case 34: {
                            String s = input.readStringRequireUtf8();

                            content_ = s;
                            break;
                        }
                        case 42: {
                            String s = input.readStringRequireUtf8();

                            device_ = s;
                            break;
                        }
                        default: {
                            if (!parseUnknownField(
                                    input, unknownFields, extensionRegistry, tag)) {
                                done = true;
                            }
                            break;
                        }
                    }
                }
            } catch (com.google.protobuf.InvalidProtocolBufferException e) {
                throw e.setUnfinishedMessage(this);
            } catch (java.io.IOException e) {
                throw new com.google.protobuf.InvalidProtocolBufferException(
                        e).setUnfinishedMessage(this);
            } finally {
                this.unknownFields = unknownFields.build();
                makeExtensionsImmutable();
            }
        }
        public static final com.google.protobuf.Descriptors.Descriptor
        getDescriptor() {
            return MessageGoogle.internal_static_ImMessage_descriptor;
        }

        @Override
        protected FieldAccessorTable
        internalGetFieldAccessorTable() {
            return MessageGoogle.internal_static_ImMessage_fieldAccessorTable
                    .ensureFieldAccessorsInitialized(
                            ImMessage.class, Builder.class);
        }

        public static final int FROM_FIELD_NUMBER = 1;
        private volatile Object from_;
        /**
         * <code>string from = 1;</code>
         * @return The from.
         */
        public String getFrom() {
            Object ref = from_;
            if (ref instanceof String) {
                return (String) ref;
            } else {
                com.google.protobuf.ByteString bs =
                        (com.google.protobuf.ByteString) ref;
                String s = bs.toStringUtf8();
                from_ = s;
                return s;
            }
        }
        /**
         * <code>string from = 1;</code>
         * @return The bytes for from.
         */
        public com.google.protobuf.ByteString
        getFromBytes() {
            Object ref = from_;
            if (ref instanceof String) {
                com.google.protobuf.ByteString b =
                        com.google.protobuf.ByteString.copyFromUtf8(
                                (String) ref);
                from_ = b;
                return b;
            } else {
                return (com.google.protobuf.ByteString) ref;
            }
        }

        public static final int TO_FIELD_NUMBER = 2;
        private volatile Object to_;
        /**
         * <code>string to = 2;</code>
         * @return The to.
         */
        public String getTo() {
            Object ref = to_;
            if (ref instanceof String) {
                return (String) ref;
            } else {
                com.google.protobuf.ByteString bs =
                        (com.google.protobuf.ByteString) ref;
                String s = bs.toStringUtf8();
                to_ = s;
                return s;
            }
        }
        /**
         * <code>string to = 2;</code>
         * @return The bytes for to.
         */
        public com.google.protobuf.ByteString
        getToBytes() {
            Object ref = to_;
            if (ref instanceof String) {
                com.google.protobuf.ByteString b =
                        com.google.protobuf.ByteString.copyFromUtf8(
                                (String) ref);
                to_ = b;
                return b;
            } else {
                return (com.google.protobuf.ByteString) ref;
            }
        }

        public static final int MSGID_FIELD_NUMBER = 3;
        private long msgId_;
        /**
         * <code>int64 msgId = 3;</code>
         * @return The msgId.
         */
        public long getMsgId() {
            return msgId_;
        }

        public static final int CONTENT_FIELD_NUMBER = 4;
        private volatile Object content_;
        /**
         * <code>string content = 4;</code>
         * @return The content.
         */
        public String getContent() {
            Object ref = content_;
            if (ref instanceof String) {
                return (String) ref;
            } else {
                com.google.protobuf.ByteString bs =
                        (com.google.protobuf.ByteString) ref;
                String s = bs.toStringUtf8();
                content_ = s;
                return s;
            }
        }
        /**
         * <code>string content = 4;</code>
         * @return The bytes for content.
         */
        public com.google.protobuf.ByteString
        getContentBytes() {
            Object ref = content_;
            if (ref instanceof String) {
                com.google.protobuf.ByteString b =
                        com.google.protobuf.ByteString.copyFromUtf8(
                                (String) ref);
                content_ = b;
                return b;
            } else {
                return (com.google.protobuf.ByteString) ref;
            }
        }

        public static final int DEVICE_FIELD_NUMBER = 5;
        private volatile Object device_;
        /**
         * <code>string device = 5;</code>
         * @return The device.
         */
        public String getDevice() {
            Object ref = device_;
            if (ref instanceof String) {
                return (String) ref;
            } else {
                com.google.protobuf.ByteString bs =
                        (com.google.protobuf.ByteString) ref;
                String s = bs.toStringUtf8();
                device_ = s;
                return s;
            }
        }
        /**
         * <code>string device = 5;</code>
         * @return The bytes for device.
         */
        public com.google.protobuf.ByteString
        getDeviceBytes() {
            Object ref = device_;
            if (ref instanceof String) {
                com.google.protobuf.ByteString b =
                        com.google.protobuf.ByteString.copyFromUtf8(
                                (String) ref);
                device_ = b;
                return b;
            } else {
                return (com.google.protobuf.ByteString) ref;
            }
        }

        private byte memoizedIsInitialized = -1;
        @Override
        public final boolean isInitialized() {
            byte isInitialized = memoizedIsInitialized;
            if (isInitialized == 1) return true;
            if (isInitialized == 0) return false;

            memoizedIsInitialized = 1;
            return true;
        }

        @Override
        public void writeTo(com.google.protobuf.CodedOutputStream output)
                throws java.io.IOException {
            if (!getFromBytes().isEmpty()) {
                com.google.protobuf.GeneratedMessageV3.writeString(output, 1, from_);
          
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非ban必选

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值