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

相关文章

MySQL 迁移至 Doris 最佳实践方案(最新整理)

《MySQL迁移至Doris最佳实践方案(最新整理)》本文将深入剖析三种经过实践验证的MySQL迁移至Doris的最佳方案,涵盖全量迁移、增量同步、混合迁移以及基于CDC(ChangeData... 目录一、China编程JDBC Catalog 联邦查询方案(适合跨库实时查询)1. 方案概述2. 环境要求3.

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

PowerShell中15个提升运维效率关键命令实战指南

《PowerShell中15个提升运维效率关键命令实战指南》作为网络安全专业人员的必备技能,PowerShell在系统管理、日志分析、威胁检测和自动化响应方面展现出强大能力,下面我们就来看看15个提升... 目录一、PowerShell在网络安全中的战略价值二、网络安全关键场景命令实战1. 系统安全基线核查

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

关于DNS域名解析服务

《关于DNS域名解析服务》:本文主要介绍关于DNS域名解析服务,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录DNS系统的作用及类型DNS使用的协议及端口号DNS系统的分布式数据结构DNS的分布式互联网解析库域名体系结构两种查询方式DNS服务器类型统计构建DNS域

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp