dobbu入门

来源:互联网 发布:福州网络推广 编辑:程序博客网 时间:2024/06/07 01:57

整个项目分三个工程:interface,server,web

一、interface 工程

  1. interface pom的依赖:
<dependency>        <groupId>com.alibaba</groupId>        <artifactId>dubbo</artifactId>        <version>2.5.3</version>        <exclusions>            <exclusion>                <groupId>org.springframework</groupId>                <artifactId>spring</artifactId>            </exclusion>        </exclusions>    </dependency>    <dependency>        <groupId>com.github.sgroschupf</groupId>        <artifactId>zkclient</artifactId>        <version>0.1</version>    </dependency>
  1. dto 与接口定义:
public class UserDTO implements Serializable{    private static final long serialVersionUID = -6159390245100691517L;    private String username;    private String password;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}
  1. 接口定义:
public interface UserService {    public UserDTO findUserById(long id);}

二、server工程

  1. server工程依赖interface工程
  2. 实现 UserService接口:
public class UserServiceImpl implements UserService {    @Override    public UserDTO findUserById(long id) {        UserDTO dto = new UserDTO();        dto.setPassword("pass");        dto.setUsername("user");        return dto;    }}
  1. 配置文件(已启动zookeeper)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"    xmlns:p="http://www.springframework.org/schema/p"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo            http://code.alibabatech.com/schema/dubbo/dubbo.xsd         http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">    <dubbo:application name="hello-world-app" />        <dubbo:registry  protocol="zookeeper" address="192.168.1.108:2181" />        <dubbo:protocol name="dubbo" port="20880" />    <dubbo:service interface="com.terry.service.UserService" ref="userService" />           <bean id="userService" class="com.terry.service.impl.UserServiceImpl" /></beans>

三、web工程

  1. web工程也依赖interface
  2. 配置文件:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"    xmlns:p="http://www.springframework.org/schema/p"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo            http://code.alibabatech.com/schema/dubbo/dubbo.xsd         http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">    <dubbo:application name="consumer-of-helloworld-app" />        <dubbo:registry  protocol="zookeeper" address="192.168.1.108:2181" />      <dubbo:reference id="userService" interface="com.terry.service.UserService" /></beans>
  1. 测试一下:
@RestController@RequestMapping("/user")public class UserAction {    @Autowired    private UserService userService;    @RequestMapping("/get")    public UserDTO get() {        UserDTO dto = userService.findUserById(1L);        return dto;    }}
  1. 然后就可以跑起来了。
0 0
原创粉丝点击