分布式程序和普通的程序的区别,从代码的角度看我觉得就是将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"; } }
- 测试:
添加用户。
查询用户: