1. 程式人生 > 其它 >springboot使用dubbo分散式開發示例

springboot使用dubbo分散式開發示例

  分散式程式和普通的程式的區別,從程式碼的角度看我覺得就是將controller層和service層分開,但是它們都是可以單獨部署的。當然我們也可以把一下重複的程式碼單獨拿出來,避免程式碼冗餘,比如pojo類。也可以定義一些公共介面,保證介面的一致性。這些在下面的程式碼中也會有體現。

  • 提供者模組。

  這是我的目錄結構。提供者模組主要是dao層和service層。

  dao層程式碼:

package com.example.dubbo.dao;

import com.example.pojo.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List; @Mapper public interface UserMapper { void addUser(User user); List<User> findAllUser(); }

  mapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<
mapper namespace="com.example.dubbo.dao.UserMapper"> <select id="findAllUser" resultType="User"> select * from user </select> <insert id="addUser" parameterType="User"> insert into user(id,name,age) values (#{id},#{name},#{age}) </insert> </
mapper>

  application.yml配置檔案:

## Dubbo \u670D\u52A1\u63D0\u4F9B\u8005\u914D\u7F6E
dubbo:
  application:
    name: provider
  registry:
    protocol: zookeeper
    address: 192.168.31.233:2181
  protocol:
    port: 28803


#datasource
spring:
  datasource:
    url: jdbc:mysql:///dubbo_test?serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver


# mybatis
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml  # mapper對映檔案位置
  type-aliases-package: com.example.pojo #持久化類包位置
  # cofig-location #指定mybatis核心配置檔案

  service層程式碼:

package com.example.dubbo.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.dubbo.dao.UserMapper;
import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service 
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

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

    @Override
    public List<User> findAllUser() {
        return userMapper.findAllUser();
    }
}
  • 介面模組和pojo模組。這兩個模組寫好之後要install一下,需要用到的模組,直接匯入依賴就可以用了。

  介面模組:

package com.example.service;

import com.example.pojo.User;

import java.util.List;

public interface UserService {

    void addUser(User user);

    List<User> findAllUser();
}

  pojo模組:

package com.example.pojo;

import java.io.Serializable;

public class User implements Serializable {  //注意實現序列化介面

    private Integer id;

    private String name;

    private Integer age;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
  • 消費者模組:
    package com.example.controller;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.example.pojo.User;
    import com.example.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    @RequestMapping("/user")
    public class TestController {
    
        @Reference
        private UserService userService;
    
        @PostMapping("/addUser")
        public String addUser(@RequestBody User user){
            userService.addUser(user);
            return "新增使用者成功";
        }
    
        @GetMapping("findUser")
        public String findAllUser(){
            String s = userService.findAllUser().toString();
            return s;
        }
    
        @GetMapping("hello")
        public String test(){
            return "hello";
        }
    }
  • 測試:

  新增使用者。

  查詢使用者:

每天堅持學一點點!