专栏首页波波烤鸭Dubbo实战案例02【业务实现】

Dubbo实战案例02【业务实现】

  本文在上篇文章的基础上来实现用户信息的CRUD操作

业务实现

添加用户信息

1.实现添加业务的服务

1.1dubbo-Mapper

/**
 * UsersMapper接口文件
 * @author dengp
 *
 */
public interface UsersMapper {

	void insertUsers(User user);
}
<?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.bobo.mapper.UsersMapper">

	<insert id="insertUsers" parameterType="com.bobo.pojo.User">
		insert into users(username,userage)values(#{username},#{userage})
	</insert>
</mapper>

1.2 dubbo-user-interface

package com.bobo.dubbo.service;

import com.bobo.pojo.User;

/**
 * 将用户的CRUD四个操作我们拆成了4个服务,便于演示dubbo
 * 实际开发过程中不用
 * @author dengp
 *
 */
public interface AddUserDubboService {

	void addUser(User user);
}

1.3 dubbo-user-service

package com.bobo.dubbo.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.bobo.dubbo.service.AddUserDubboService;
import com.bobo.mapper.UsersMapper;
import com.bobo.pojo.User;
@Service
public class AddUserDubboServiceImpl implements AddUserDubboService {
	
	@Resource
	private UsersMapper userMapper;

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub
		userMapper.insertUsers(user);
	}
}

注意我们在服务端添加了服务,我们需要在Dubbo中注册该服务。

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
    <!-- 导入Spring相关的配置文件 -->
	<import resource="../../applicationContext-dao.xml" />
	<import resource="../../applicationContext-service.xml" />
	<import resource="../../applicationContext-trans.xml" />
	
	<dubbo:application name="user-provider" />
	<dubbo:registry protocol="zookeeper"
		address="node01:2181,node02:2181,node03:2181"/>
		
	<dubbo:protocol name="dubbo" port="20880"/>
	<!-- 向dubbo中注册的服务  -->
	<dubbo:service interface="com.bobo.dubbo.service.AddUserDubboService" 
				ref="addUserDubboServiceImpl"/>

</beans>

服务端测试

连接成功

2.实现添加业务的业务

2.1 dubbo-user-portal-service

/**
 * 用户管理的业务接口
 * @author dengp
 *
 */
public interface UserService {

	void addUser(User user);
}
@Service
public class UserServiceImpl implements UserService {
	
	@Resource
	private AddUserDubboService addUserService;

	@Override
	public void addUser(User user) {
		// TODO Auto-generated method stub
		addUserService.addUser(user);
	}
}

2.2 dubbo-user-portal

UserController

package com.bobo.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bobo.pojo.User;
import com.bobo.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;
	
	@RequestMapping("/addUser")
	public String addUser(User user){
		userService.addUser(user);
		return "success";
	}
}

Consumer需要指定从dubbo中获取什么服务

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        
 	<dubbo:application name="dubbo-consumer"/>
 	
 	<dubbo:registry  protocol="zookeeper"
 		address="node01:2181,node02:2181,node03:2181" /> 
 		
 	<!-- 指定获取的服务 -->
 	<dubbo:reference id="addUserDubboService"
 		 interface="com.bobo.dubbo.service.AddUserDubboService"/>
 </beans>

addUser.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="/user/addUser" method="post">
		用户名:<input type="text" name="username" /><br /> 
		用户年龄:<input type="text" name="userage" /><br /> 
		<input type="submit" value="提交" />
	</form>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>dubbo .... 欢迎</h1>
	<h2><a href="/addUser">添加用户信息</a></h2>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>操作成功</h1>
</body>
</html>

PageController

@Controller
public class PageController {

	@RequestMapping("/")
	public String index(){
		return "index";
	}
	
	@RequestMapping("/{path}")
	public String base(@PathVariable String path){
		return path;
	}
}

2.3 测试

报错

在访问

查询用户信息

1.实现查询用户服务

1.1 dubbo-mapper

1.2 dubbo-user-interface

1.3 dubbo-user-service

向dubbo注册服务

2.实现查询用户业务

2.1 dubbo-user-protal-service

2.2 dubbo-user-protal

userController

从dubbo中发现服务

index.jsp

user.jsp

<%@ page language="java" contentType="text/html; 
charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1" align="center">
		<tr>
			<th>用户 ID</th>
			<th>用户姓名</th>
			<th>用户年龄</th>
			<th>操作</th>
		</tr>
		<c:forEach items="${list }" var="user">
			<tr>
				<td>${user.id }</td>
				<td>${user.username }</td>
				<td>${user.userage }</td>
				<td><a href="">更新</a> <a href="">删除</a></td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

测试

成功查询出数据

删除用户信息

1.实现删除用户服务

1.1 dubbo-mapper

1.2 dubbo-user-interface

1.3 dubbo-user-service

注册业务

2.实现删除用户业务

2.1 dubbo-user-protal-service

2.2 dubbo-user-protal

UserController

dubbo中获取服务

删除按钮

测试

操作成功~

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Spring之事务管理

    用户4919348
  • SpringSecurity超详细入门介绍

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明...

    用户4919348
  • 好好编程-物流项目06【Spring整合】

      Spring的整合相对要简单很多,我们只要需要添加对应的配置文件,然后在web.xml文件中配置监听及配置文件路径。

    用户4919348
  • Dubbo(Dubbo与Zookeeper、SpringMVC整合)

    Zookeeper作为Dubbo服务的注册中心,Dubbo原先基于数据库的注册中心,没采用Zookeeper,Zookeeper一个分布式的服务框架,是树型的...

    用户1257215
  • day69_淘淘商城项目_02

      由于淘淘商城是基于soa的架构,表现层和服务层是不同的工程。所以要实现商品列表查询需要两个系统之间进行通信。   如何实现远程通信?

    黑泽君
  • SpringBoot之Dubbo

    DUBBO是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,每天为2,000+个服务提供3,...

    王念博客
  • Goto循环-c语言学习笔记

    Youngxj
  • TypeScript 期中考试现在开始!

    相信这段时间来,对 TypeScript 感兴趣的小伙伴们已经把这个神器给系统的学习了一遍了吧。如果计划开始学习但是还没有开始,或者没有找到资料的同学,可以看下...

    ssh1995
  • Dubbo无法访问远程Zookeeper已注册服务的问题

    使用Dubbo的时候发现当Zookeeper、Dubbo-admin、生产者和消费者都在 内网中的时候,生产者的生产和消费是没有问题的,但是当它Zookeep...

    MonroeCode
  • 10分钟教你Python爬虫(上)-- HTML和爬虫基础

    数据魔术师在这里祝愿大家新年快乐,身体安康。我们的指导老师秦虎教授,现在虽然在武汉疫区,但是仍然坚持督促小编们去学习,继续做一些公众号的内容去分享。供大家在这个...

    用户1621951

扫码关注云+社区

领取腾讯云代金券