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

 

目录

1)API接口定义 

代码目标

接口定义实现

2)实现类接口


 

  • 1)API接口定义 

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

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

  • 接口定义实现

  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.user
    
    struct 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,前提先要导入不然找不到Docker Kubernetes 微服务容器化实践(二) 2.1 微服务实战 thrift篇 ②--用户服务Thrift
  3. Docker Kubernetes 微服务容器化实践(二) 2.1 微服务实战 thrift篇 ②--用户服务Thrift
  4. 创建数据库 docker启动mysql (略)
  5. 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>
  6. 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
    
  7.  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);
    }
    

     

  8.  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 {
    
    
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public UserInfo getUserById(int id) throws TException {
    
            return userMapper.getUserById(id);
        }
    
        @Override
        public UserInfo getTeacherById(int id) throws TException {
            return userMapper.getTeacherById(id);
        }
    
        @Override
        public UserInfo getUserByName(String username) throws TException {
            return userMapper.getUserByName(username);
        }
    
        @Override
        public void regiserUser(UserInfo userInfo) throws TException {
            userMapper.registerUser(userInfo);
        }
    }
    

     

  9.   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;
    
        @Autowired
        private UserService.Iface userService;
    
        @PostConstruct
        public 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();
        }
    }
    

     

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