基于http的protobuf服务实现

本文介绍在http协议中,使用protobuf格式进行二进制数据通信。双方需设置http的header中ContentType为application/x-protobuf。

1、springboot下实现protobuf:

1)pom.xml

html 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- protobuf -->
<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java</artifactId>
    <version>3.11.0</version>
</dependency>
<dependency>
    <groupId>com.google.protobuf</groupId>
    <artifactId>protobuf-java-util</artifactId>
    <version>3.11.0</version>
</dependency>
<dependency>
    <groupId>com.googlecode.protobuf-java-format</groupId>
    <artifactId>protobuf-java-format</artifactId>
    <version>1.2</version>
</dependency>
<!-- 网络请求依赖 -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4</version>
</dependency>

2)user_login.proto

bash 复制代码
syntax = "proto3";
// 转成java后所在包
option java_package = "com.tencent.rating.audi_model_api.pb";
// 转成java后的类名
option java_outer_classname = "MessageUserLogin";
// 转java后生成一个java文件
option java_multiple_files = false;
 
message MessageUserLoginRequest {
    string username = 1;
    string password = 2;
}
message MessageUserLoginResponse {
    string access_token = 1;
    string username = 2;
}

编译:

bash 复制代码
protoc --proto_path=/data/ --java_out=./ /data/user_login.proto

3)创建springboot工程,编写config:

java 复制代码
@Configuration
public class WebConfig {
	@Bean
    ProtobufHttpMessageConverter protobufHttpMessageConverter() {
        return new ProtobufHttpMessageConverter();
    }
	/**
     * protobuf 反序列化 可以不用配置
     */
//    @Bean
//    RestTemplate restTemplate(ProtobufHttpMessageConverter protobufHttpMessageConverter) {
//        return new RestTemplate(Collections.singletonList(protobufHttpMessageConverter));
//    }
}

4)编写contorller:

java 复制代码
import java.util.UUID;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tencent.rating.audi_model_api.pb.MessageUserLogin;

@Controller
public class PbTestController {	
    @RequestMapping(value = "/pbtest", produces = "application/x-protobuf")
    @ResponseBody
    public MessageUserLogin.MessageUserLoginResponse getPersonProto(@RequestBody MessageUserLogin.MessageUserLoginRequest request) {
    	System.out.println("request:" + request.toString());
    	
        MessageUserLogin.MessageUserLoginResponse.Builder builder = MessageUserLogin.MessageUserLoginResponse.newBuilder();
        builder.setAccessToken(UUID.randomUUID().toString()+"_res");
        builder.setUsername(request.getUsername()+"_res");
        return builder.build();
    }
}

然后启动sprinboot工程。监听8080端口

5)编写客户端请求:

java 复制代码
public class PbTest {
	
	public static void main(String...strings) {
		try {
            String uri = "http://127.0.0.1:8080/clue/pbtest";
            MessageUserLogin.MessageUserLoginRequest.Builder builder = MessageUserLogin.MessageUserLoginRequest.newBuilder();
            builder.setUsername("tom");
            builder.setPassword("123456");
            HttpResponse response = doPost(uri, builder.build().toByteArray());
            MessageUserLogin.MessageUserLoginResponse messageUserLoginResponse = MessageUserLogin.MessageUserLoginResponse.parseFrom(response.getEntity().getContent());
            System.out.println("response:" + messageUserLoginResponse.toString());
            System.err.println(messageUserLoginResponse.getAccessToken());
        } catch (Exception e) {
 
        }
	}
	
	private static HttpResponse doPost(String uri, byte[] bytes) throws Exception {
        CloseableHttpResponse closeableHttpResponse = null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost post = new HttpPost(uri);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
            InputStreamEntity inputStreamEntity = new InputStreamEntity(inputStream);
            post.setEntity(inputStreamEntity);
            post.addHeader("Content-Type", "application/x-protobuf");
            closeableHttpResponse = httpclient.execute(post);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // httpclient.close();
        }
        return closeableHttpResponse;
    }
}

2、servlet下实现protobuf:

1)创建servlet项目,编写servlet代码:

java 复制代码
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import servlet_test.pb.MessageUserLogin;
import servlet_test.pb.MessageUserLogin.MessageUserLoginRequest;
import servlet_test.pb.MessageUserLogin.MessageUserLoginResponse.Builder;

@WebServlet(urlPatterns = {"/syncServlet2"})
public class SyncServlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //解析请求
        InputStream stream=request.getInputStream();
        MessageUserLoginRequest mlr = MessageUserLogin.MessageUserLoginRequest.parseFrom(stream);
        System.out.println(mlr.toString());
        //创建响应
        Builder newBuilder = MessageUserLogin.MessageUserLoginResponse.newBuilder();
        newBuilder.setAccessToken(UUID.randomUUID().toString()+"_res");
        newBuilder.setUsername(mlr.getUsername()+"_res");
        response.setCharacterEncoding("UTF-8");
        ServletOutputStream out = response.getOutputStream();
        out.write(newBuilder.build().toByteArray());
        
        out.flush();
        out.close();
    }
}

2)客户端测试:

同上。

相关推荐
徐*红15 分钟前
java 线程池
java·开发语言
尚学教辅学习资料15 分钟前
基于SSM的养老院管理系统+LW示例参考
java·开发语言·java毕设·养老院
2401_8576363915 分钟前
计算机课程管理平台:Spring Boot与工程认证的结合
java·spring boot·后端
1 9 J17 分钟前
Java 上机实践4(类与对象)
java·开发语言·算法
Code apprenticeship18 分钟前
Java面试题(2)
java·开发语言
也无晴也无风雨1 小时前
深入剖析输入URL按下回车,浏览器做了什么
前端·后端·计算机网络
憨子周1 小时前
2M的带宽怎么怎么设置tcp滑动窗口以及连接池
java·网络·网络协议·tcp/ip
霖雨3 小时前
使用Visual Studio Code 快速新建Net项目
java·ide·windows·vscode·编辑器
SRY122404193 小时前
javaSE面试题
java·开发语言·面试
Fiercezm3 小时前
JUC学习
java