当前位置:首页 » 《随便一记》 » 正文

spring boot 整合dubbo_txyllyyj的博客

18 人参与  2022年05月24日 17:40  分类 : 《随便一记》  评论

点击全文阅读


1.使用zookeeper作为注册中心,所以需要在服务器上安装zookeeper,将Dubbo Admin 先运行在服务器上,以便查看服务状态

2.导入sql:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `sex` varchar(25) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '王尔瓦', '女', 20);
INSERT INTO `user` VALUES (2, '李四', '男', 21);
INSERT INTO `user` VALUES (3, '王五', '女', 26);

SET FOREIGN_KEY_CHECKS = 1;

3.创建接口类和实体类的工程commons,创建消费者工程consumer,创建生产者工程provider

4.commos工程中创建

实体类:(由于远程服务调用需要网络传输,所以实体类需要实现序列化)

//使用lombok注解简化代码,记得引入依赖,安装Lombok插件
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
    private Integer id;
    private String name;
    private String sex;
    private Integer age;
}

 接口类:

public interface UserService {
    /**
     * 添加用户
     * @param user
     * @return
     */
    boolean addUser(User user);

    /**
     * 根据id删除用户
     * @param id
     * @return
     */

    boolean deleteUserById(int id);

    /**
     * 根据id删除用户
     * @param user
     * @return
     */
    boolean updateUserById(User user);

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    User getUserById(int id);

    /**
     * 获取所有用户
     * @return
     */
    List<User> getAllUser();

}

5.将commons工程安装到本地仓库,在provider工程中引入

pom.xml:

<?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>
        <artifactId>springboot_dubbo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo_provider</artifactId>
<dependencies>
    <!--共用模块-->
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>dubbo_commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--mybatis-plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.1</version>
    </dependency>
    <!--mysql-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>8.0.26</scope>
    </dependency>
    <!-- dubbo的依赖 -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.6</version>
    </dependency>
    <!-- zk的依赖 -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-dependencies-zookeeper</artifactId>
        <version>2.7.6</version>
        <type>pom</type>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

</project>

provider工程中创建服务实现类实现UserService接口(注意service注解是dubbo包中的)

@Service//不是spring中的service而是dubbo中的
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public boolean deleteUserById(int id) {
        return userMapper.deleteUserById(id);
    }

    @Override
    public boolean updateUserById(User user) {
        return userMapper.updateUserById(user);
    }

    @Override
    public User getUserById(int id) {
        return userMapper.getUserById(id);
    }

    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }
}

provider工程中创建mapper接口

public interface UserMapper {

    boolean addUser(User user);

    boolean deleteUserById(int id);

    boolean updateUserById(User user);

    User getUserById(int id);

    List<User> getAllUser();
}

provider工程中创建mapper.xml

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.it.mapper.UserMapper">
    <select id="getUserById" resultType="com.it.entity.User">
       SELECT * from user WHERE id=#{id}
    </select>
    <insert id="addUser" parameterType="com.it.entity.User">
        insert into user values (null ,#{name},#{sex},#{age})
    </insert>
    <delete id="deleteUserById" parameterType="com.it.entity.User">
        delete from user where id=#{id}
    </delete>
    <update id="updateUserById" parameterType="com.it.entity.User">
        update user set name=#{name},sex=#{sex},age=#{age} where id=#{id}
    </update>
    <select id="getAllUser" resultType="com.it.entity.User">
        select * from user
    </select>
</mapper>

provider配置文件:

server:
  port: 8080
spring:
  application:
    name: mc_provider
  #访问数据库
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql:///dubbo_demo?serverTimezone=UTC&useSSL=true&characterEncoding=utf-8
    username: root
    password: 123456
  #mybatis-plus
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.it.entity
  #dubbo,zookeeper
dubbo:
  application:
    name: dubbo_provider
  #zookeeper地址
  registry:
    address: zookeeper://192.14.80.206:2181
    timeout: 10000
  #暴露端口
  protocol:
    name: dubbo
    port: 20880

启动类:(注意加上开启dubbo注解)

@SpringBootApplication
@EnableDubbo//开启dubbo
@MapperScan("com.it.mapper")//扫描mapper接口
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class,args);
    }
}

6.将commons映入consumer工程

pom.xml:

<?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>
        <artifactId>springboot_dubbo</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo_customer</artifactId>
<dependencies>
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>dubbo_commons</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

</project>

在controller中调用服务(注意使用Dubbo中的@Reference才能调用服务,而不是jdk中的@Reference)

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private UserService userService;

    /**
     * 获取所有用户
     * @param
     * @return
     */
    @RequestMapping("/getAllUser")
    public List<User> getAllUser(){
       return  userService.getAllUser();
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("/addUser")
    public boolean addUser(@RequestBody User user){
        return  userService.addUser(user);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @RequestMapping("/deleteUserById")
    public boolean deleteUserById(int id){
        return  userService.deleteUserById(id);
    }

    /**
     * 根据id更新用户
     * @param user
     * @return
     */
    @RequestMapping("/updateUserById")
    public boolean updateUserById(@RequestBody User user){
        return  userService.updateUserById(user);
    }

    /**
     * 根据id获取用户
     * @param id
     * @return
     */
    @RequestMapping("/getUserById")
    public User getUserById(int id){
        return  userService.getUserById(id);
    }
}

配置文件:

server:
  port: 8081
dubbo:
  application:
    name: dubbo_customer
  registry:
    address: zookeeper://192.14.80.206:2181
    timeout: 10000

启动类:(注意加上开启dubbo注解)

@SpringBootApplication
@EnableDubbo//开启dubbo
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class,args);
    }
}

7.先启动生产者工程,再启动消费者工程

消费者能够调用到服务


点击全文阅读


本文链接:http://zhangshiyu.com/post/40718.html

工程  用户  创建  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1