Docker Kubernetes 微服务容器化实践(二) 2.1 微服务实战 thrift篇 ②--用户服务Thrift

本文主要是介绍Docker Kubernetes 微服务容器化实践(二) 2.1 微服务实战 thrift篇 ②--用户服务Thrift,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

目录

1)API接口定义 

代码目标

接口定义实现

2)实现类接口


 

  • 1)API接口定义 

  1. 这里是api接口定义代码内容
  • 代码目标

  • 接口定义实现

  1. 在microservice项目中创建 user-thrift-service-api 模块
  2. user-thrift-service-api/pom.xml添加thrift版本build插件
  3. user-thrift-service-api/thrift/user_service.thrift 创建实体接口
    namespace java com.imooc.thrift.userstruct UserInfo {1:i32 id,2:string username,3:string password,4:string realName,5:string mobile,6:string email,7:string intro,8:i32 stars
    }service UserService {UserInfo getUserById(1:i32 id);UserInfo getTeacherById(1:i32 id);UserInfo getUserByName(1:string username);void regiserUser(1:UserInfo userInfo);}
  4. gen-code代码生成
    #!/usr/bin/env bash
    thrift --gen java -out ../src/main/java user_service.thrift

     

  • 2)实现类接口

  1. 在microservice项目中创建 user-thrift-service 模块
  2. user-thrift-service/src/main/java添加UserServiceImpl,前提先要导入不然找不到
  3. 创建数据库 docker启动mysql (略)
  4. https://mvnrepository.com 添加对应依赖 完整依赖如下:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version></parent><modelVersion>4.0.0</modelVersion><groupId>com.imooc</groupId><artifactId>user-thrift-service</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.apache.thrift</groupId><artifactId>libthrift</artifactId><version>0.10.0</version></dependency><dependency><groupId>com.imooc</groupId><artifactId>user-thrift-service-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.44</version></dependency><dependency><groupId>com.imooc</groupId><artifactId>message-thrift-service-api</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>1.7</source><target>1.7</target></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build>
    </project>
  5. application.properties 配置数据源 
    service.name=user-thrift-service
    service.port=7911#数据源的配置
    spring.datasource.url=jdbc:mysql://192.168.1.8:3306/db_user
    spring.datasource.username=root
    spring.datasource.password=aA111111
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    
  6.  microservice/user-thrift-service/src/main/java/com/imooc/user/mapper/UserMapper.java
    package com.imooc.user.mapper;import com.imooc.thrift.user.UserInfo;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;/*** Created by Michael on 2017/10/28.*/
    @Mapper
    public interface UserMapper {@Select("select id,username, password, real_name as realName," +"mobile, email from pe_user where id=#{id}")UserInfo getUserById(@Param("id")int id);@Select("select id,username, password, real_name as realName," +"mobile, email from pe_user where username=#{username}")UserInfo getUserByName(@Param("username")String username);@Insert("insert into pe_user (username, password, real_name, mobile, email)" +"values (#{u.username}, #{u.password}, #{u.realName}, #{u.mobile}, #{u.email})")void registerUser(@Param("u") UserInfo userInfo);@Select("select u.id,u.username,u.password,u.real_name as realName," +"u.mobile,u.email,t.intro,t.stars from pe_user u," +"pe_teacher t where u.id=#{id} " +"and u.id=t.user_id")UserInfo getTeacherById(@Param("id")int id);
    }
    

     

  7.  microservice/user-thrift-service/src/main/java/com/imooc/user/service/UserSerivceImpl.java
    package com.imooc.user.service;import com.imooc.thrift.user.UserInfo;
    import com.imooc.thrift.user.UserService;
    import com.imooc.user.mapper.UserMapper;
    import org.apache.thrift.TException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;/*** Created by Michael on 2017/10/28.*/
    @Service
    public class UserSerivceImpl implements UserService.Iface {@Autowiredprivate UserMapper userMapper;@Overridepublic UserInfo getUserById(int id) throws TException {return userMapper.getUserById(id);}@Overridepublic UserInfo getTeacherById(int id) throws TException {return userMapper.getTeacherById(id);}@Overridepublic UserInfo getUserByName(String username) throws TException {return userMapper.getUserByName(username);}@Overridepublic void regiserUser(UserInfo userInfo) throws TException {userMapper.registerUser(userInfo);}
    }
    

     

  8.   microservice/user-thrift-service/src/main/java/com/imooc/user/thrift/ThriftServer.java
    package com.imooc.user.thrift;import com.imooc.thrift.user.UserService;
    import org.apache.thrift.TProcessor;
    import org.apache.thrift.protocol.TBinaryProtocol;
    import org.apache.thrift.server.TNonblockingServer;
    import org.apache.thrift.server.TServer;
    import org.apache.thrift.transport.TFramedTransport;
    import org.apache.thrift.transport.TNonblockingServerSocket;
    import org.apache.thrift.transport.TTransportException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;/*** Created by Michael on 2017/10/28.*/
    @Configuration
    public class ThriftServer {@Value("${service.port}")private int servicePort;@Autowiredprivate UserService.Iface userService;@PostConstructpublic void startThriftServer() {TProcessor processor = new UserService.Processor<>(userService);TNonblockingServerSocket socket = null;try {socket = new TNonblockingServerSocket(servicePort);} catch (TTransportException e) {e.printStackTrace();}TNonblockingServer.Args args = new TNonblockingServer.Args(socket);args.processor(processor);args.transportFactory(new TFramedTransport.Factory());args.protocolFactory(new TBinaryProtocol.Factory());TServer server = new TNonblockingServer(args);server.serve();}
    }
    

     

  9.  注意必须创建完ThriftServer.java 才能启动ServiceApplication,不然启动失败

这篇关于Docker Kubernetes 微服务容器化实践(二) 2.1 微服务实战 thrift篇 ②--用户服务Thrift的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/492717

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

在Java中使用OpenCV实践

《在Java中使用OpenCV实践》用户分享了在Java项目中集成OpenCV4.10.0的实践经验,涵盖库简介、Windows安装、依赖配置及灰度图测试,强调其在图像处理领域的多功能性,并计划后续探... 目录前言一 、OpenCV1.简介2.下载与安装3.目录说明二、在Java项目中使用三 、测试1.测

PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例

《PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例》词嵌入解决NLP维度灾难,捕捉语义关系,PyTorch的nn.Embedding模块提供灵活实现,支持参数配置、预训练及变长... 目录一、词嵌入(Word Embedding)简介为什么需要词嵌入?二、PyTorch中的nn.Em

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

在IntelliJ IDEA中高效运行与调试Spring Boot项目的实战步骤

《在IntelliJIDEA中高效运行与调试SpringBoot项目的实战步骤》本章详解SpringBoot项目导入IntelliJIDEA的流程,教授运行与调试技巧,包括断点设置与变量查看,奠定... 目录引言:为良驹配上好鞍一、为何选择IntelliJ IDEA?二、实战:导入并运行你的第一个项目步骤1