1. 今日大纲

  1. Dubbo入门学习
  2. 使用dubbo优化单点登录系统
  1. 系统间服务调用方式

    1. 浏览器直接访问

浏览器发起请求,通过ajax或jsonp方式请求:

  1. Httpclient方式

系统与系统之间通过Httpclient发起http请求来请求数据:

http协议是短连接。

  1. RPC方式

采用长连接方式。

  1. 单点系统中存在的问题

在单点登录系统中的功能中,根据token查询用户信息的功能对系统性能要求最高,如果我们想单独调整该功能的性能是不可能的,因为该功能和其它的功能耦合再一起。

要想单独优化该功能的性能就必须把该功能单独出来,我们就可以借助与dubbo框架完成。

  1. Dubbo入门

具体参考课前资料中的《dubbo入门教程.docx》

  1. 使用dubbo优化单点系统的查询功能

    1. 创建taotao-sso-interface工程

      1. 创建maven工程

该工程定义查询接口。

  1. 导入依赖

<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">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>com.taotao.parent</groupId>

<artifactId>taotao-parent</artifactId>

<version>0.0.1-SNAPSHOT</version>

</parent>

<groupId>com.taotao.sso.query</groupId>

<artifactId>taotao-sso-query-api</artifactId>

<version>1.0.0-SNAPSHOT</version>

<dependencies>

<dependency>

<groupId>com.taotao.common</groupId>

<artifactId>taotao-common</artifactId>

<version>1.0.0-SNAPSHOT</version>

</dependency>

</dependencies>

</project>

  1. 将taotao-sso中的User对象拷贝到该工程

拷贝代码并且做一些修改,将jpa的注解删除:

package com.taotao.sso.query.bean;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonIgnore;

public
class User {

private Long id;

private String username;

@JsonIgnore

// 将对象序列化json字符串时忽略该字段

private String password;

private String phone;

private String email;

private Date created;

private Date updated;

public Long getId() {

return
id;

}

public
void setId(Long id) {

this.id = id;

}

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;

}

public String getPhone() {

return
phone;

}

public
void setPhone(String phone) {

this.phone = phone;

}

public String getEmail() {

return
email;

}

public
void setEmail(String email) {

this.email = email;

}

public Date getCreated() {

return
created;

}

public
void setCreated(Date created) {

this.created = created;

}

public Date getUpdated() {

return
updated;

}

public
void setUpdated(Date updated) {

this.updated = updated;

}

}

  1. 定义服务接口

package com.taotao.sso.query.api;

import com.taotao.sso.query.bean.User;

public
interface UserQueryService {

/**

* 根据token查询User对象

*

* @return

*/

public User queryUserByToken(String token);

}

  1. 创建taotao-sso-service(web方式)

    1. 创建maven工程

  1. 导入依赖

<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">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>com.taotao.parent</groupId>

<artifactId>taotao-parent</artifactId>

<version>0.0.1-SNAPSHOT</version>

</parent>

<groupId>com.taotao.sso.query</groupId>

<artifactId>taotao-sso-query-service</artifactId>

<version>1.0.0-SNAPSHOT</version>

<packaging>war</packaging>

<dependencies>

<dependency>

<groupId>com.taotao.common</groupId>

<artifactId>taotao-common</artifactId>

<version>1.0.0-SNAPSHOT</version>

</dependency>

<dependency>

<groupId>com.taotao.sso</groupId>

<artifactId>taotao-sso-interface</artifactId>

<version>1.0.0-SNAPSHOT</version>

</dependency>

<!-- dubbo采用spring配置方式,所以需要导入spring容器依赖 -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

</dependency>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>dubbo</artifactId>

<version>2.5.3</version>

<exclusions>

<exclusion>

<!-- 排除传递spring依赖 -->

<artifactId>spring</artifactId>

<groupId>org.springframework</groupId>

</exclusion>

</exclusions>

</dependency>

</dependencies>

<build>

<plugins>

<!-- 配置Tomcat插件 -->

<plugin>

<groupId>org.apache.tomcat.maven</groupId>

<artifactId>tomcat7-maven-plugin</artifactId>

<configuration>

<port>8087</port>

<path>/</path>

</configuration>

</plugin>

</plugins>

</build>

</project>

  1. 编写配置文件

applicationContext-redis.xml:

applicationContext.xml:

taotao-sso-service-servlet.xml:

Dubbo-server.xml:

  1. 编写web.xml

<?xml
version="1.0"
encoding="UTF-8"?>

<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

id="WebApp_ID"
version="2.5">

<display-name>taotao-sso-query-service</display-name>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring/applicationContext*.xml,classpath:dubbo/dubbo-*.xml</param-value>

</context-param>

<!--Spring的ApplicationContext 载入 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- 编码过滤器,以UTF8编码 -->

<filter>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- 配置SpringMVC框架入口 -->

<servlet>

<servlet-name>taotao-sso</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring/taotao-sso-query-servlet.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>taotao-sso</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

</web-app>

  1. 创建taotao-sso-service(jar方式)

    1. 配置文件

Spring的配置文件(和web工程一样):

Dubbo配置文件:

具体的配置:

指定spring的配置文件路径:

dubbo.spring.config=classpath:spring/*.xml,classpath:dubbo/*.xml

  1. 启动

  1. 编写UserQueryService的实现

package com.taotao.sso.query.service;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.taotao.common.service.RedisService;

import com.taotao.sso.query.api.UserQueryService;

import com.taotao.sso.query.bean.User;

@Service

public
class UserQueryServiceImpl implements UserQueryService {

@Autowired

private RedisService redisService;

private
static
final String REDIS_KEY = "TOKEN_";

private
static
final Integer REDIS_TIME = 60 * 30;

private
static
final ObjectMapper MAPPER = new ObjectMapper();

@Override

public User queryUserByToken(String token) {

String key = REDIS_KEY + token;

try {

String jsonData = this.redisService.get(key);

if (StringUtils.isEmpty(jsonData)) {

// 登录超时

return
null;

}

// 重新刷新用户的生存时间

this.redisService.expire(key, REDIS_TIME);

return
MAPPER.readValue(jsonData, User.class);

} catch (Exception e) {

e.printStackTrace();

}

return
null;

}

}

  1. 实现RESTful web service

package com.taotao.sso.query.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;

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

import org.springframework.web.bind.annotation.RequestMethod;

import com.taotao.sso.query.api.UserQueryService;

import com.taotao.sso.query.bean.User;

@RequestMapping("user")

@Controller

public
class UserController {

@Autowired

private UserQueryService userQueryService;

/**

* 根据token查询用户信息

*

* @param token

* @return

*/

@RequestMapping(value = "{token}", method = RequestMethod.GET)

public ResponseEntity<User> queryUserByToken(@PathVariable("token") String token) {

try {

User user = this.userQueryService.queryUserByToken(token);

if (null == user) {

return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);

}

return ResponseEntity.ok(user);

} catch (Exception e) {

e.printStackTrace();

}

return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

}

}

  1. 测试

  1. 修改为域名访问

配置nginx:

测试:

  1. 将查询服务发布到dubbo中

    1. 启动zookeeper服务

  1. 导入zookeeper客户端依赖

  1. 编写dubbo配置文件

<beans
xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!-- 提供方应用信息,用于计算依赖关系 -->

<dubbo:application
name="dubbo-sso-query-server"
/>

<!-- 这里使用的注册中心是zookeeper -->

<dubbo:registry
address="zookeeper://127.0.0.1:2181"
client="zkclient"/>

端口暴露服务 -->

<dubbo:protocol
name="dubbo"
port="20880"
/>

<!-- 将该接口暴露到dubbo中 -->

<dubbo:service
interface="com.taotao.sso.query.api.UserQueryService "
ref="userQueryServiceImpl"
/>

<!-- 将具体的实现类加入到Spring容器中 -->

<bean
id="userQueryServiceImpl"
class="com.taotao.sso.query.service.UserQueryServiceImpl"
/>

</beans>

  1. 在web.xml中配置Spring扫描dubbo配置文件

  1. 启动tomcat并且测试

  1. 禁用原有的taotao-sso中的服务

  1. 改造前台系统的调用方式

    1. 之前调用方式

之前前台系统是通过Httpclient方式调用sso系统的restful接口,现在我们改造成dubbo调用方式。

  1. 现在的实现

  1. 加入dubbo依赖

  1. 导入interface依赖

  1. 编写dubbo配置文件

<beans
xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<!-- 提供方应用信息,用于计算依赖关系 -->

<dubbo:application
name="dubbo-web-consumer"
/>

<!-- 这里使用的注册中心是zookeeper -->

<dubbo:registry
address="zookeeper://127.0.0.1:2181"
client="zkclient"/>

端口暴露服务 -->

<dubbo:protocol
name="dubbo"
port="20880"
/>

<!-- 从注册中心中查找服务 -->

<dubbo:reference id="userQueryService" interface="com.taotao.sso.service.UserQueryService" />

</beans>

  1. 在web.xml中配置Spring容器扫描dubbo

  1. 测试

功能一切正常。

  1. 作业

  1. 废弃原有sso系统中的查询功能;
  2. 改造所有系统中的调用方式;

淘淘商城_day10_课堂笔记的更多相关文章

  1. 淘淘商城_day11_课堂笔记

    今日大纲 发布前的准备 实施发布 一部分是由我来发布 一部分是由你们来发布 讲解分布式部署架构 测试 功能测试 压力测试 项目实战的准备以及分组 分组 抽取功能 讲解所需要开发的功能 项目部署上线流程 ...

  2. 淘淘商城_day01_课堂笔记

    今日大纲 聊聊电商行业 电商行业发展 11.11 2015双11: 2016年: 预测:2017年的双11交易额将达到:1400亿 电商行业技术特点 淘淘商城简介 淘淘商城的前身 电商行业的概念 B2 ...

  3. 淘淘商城_day04_课堂笔记

    今日大纲 实现首页的大广告位功能 实现内容管理系统 首页的大广告 什么是大广告 JS效果: 点击下面的序号选择查询哪个广告 自动切换 点击图片查询具体的页面 以上是由前端团队来开发. 数据结构 说明: ...

  4. 淘淘商城_day02_课堂笔记

    今日大纲 学习Nginx的使用 实现商品的管理 新增商品 查询商品列表 编辑商品 删除商品 上架和下架商品 学习nginx 开发阶段中的环境 开发环境:自己的电脑 测试环境:提供给测试人员使用的环境 ...

  5. 淘淘商城_day09_课堂笔记

    今日大纲 实现购物车 基于Mysql实现读写分离 购物车 需求描述 用户可以在登录状态下将商品添加到购物车 用户可以在未登录状态下将商品添加到购物车 用户可以使用购物车一起结算下单 用户可以查询自己的 ...

  6. 淘淘商城_day08_课堂笔记

    今日大纲 问题,如何实现商品数据的同步? 学习MQ(消息队列) 搭建RabbitMQ的环境 学习RabbitMQ的队列 学习Spring-Rabbit 使用RabbitMQ完成商品数据的同步 如何实现 ...

  7. 淘淘商城_day07_课堂笔记

    今日大纲 讲解订单系统 基于订单系统完成下单功能的开发 使用Solr完成商品的搜索功能 订单系统 说明:订单系统只是做讲解,不做开发. 导入taotao-order 表结构 订单表: 订单商品表: 疑 ...

  8. 淘淘商城_day05_课堂笔记

    今日大纲 学习Redis 使用Redis完成项目中缓存需求 实现商品详情页功能 缓存的需求 大广告位数据无需每次查询后台系统的接口,可以在前台系统添加缓存,提高访问首页的速度. 商品类目的数据也可以缓 ...

  9. 淘淘商城_day06_课堂笔记

    今日大纲 实现单点登录系统 基于单点登录系统实现,用户的注册和登录 商品数据同步问题 问题 后台系统中将商品修改,前台系统没有进行数据的同步,导致前端系统不能够实时显示最新的数据. 解决 后台系统中商 ...

随机推荐

  1. .NET基础——数组

    这一篇,我们来看C#中的数组. 1. 数组的概念 数组:存储相同类型多个数据元素的容器 数组的声明和初始化: 在创建数组的时候,必须指定数组的长度 ]; ,, }; ] { , , };//数组元素的 ...

  2. (细节控)swift3.0与融云IMKIT开发问题(一部分) override func onSelectedTableRow Method does not override any method from its superclass

    原官网文档方案如下,在swift3.0的情况下出现 override func onSelectedTableRow  Method does not override any method from ...

  3. java基础练习 10

    import java.util.Scanner; public class Tenth { /*有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数*/ public stati ...

  4. django 学习笔记(一)搭建基础环境

    1.安装django 下载地址 https://github.com/django/django 解压后进入文件夹运行指令 >> python setup.py install 2.创建工 ...

  5. BOGEER博格尔YT-813码表使用说明书 (我的是YT-823)

    BOGEER博格尔YT-813码表使用说明书.doc 源:http://w.gdu.me/wiki/Bike/BOGEER-YT-813.html 参数设置 首先要测量出车轮的周长,测出车轮周长后按住 ...

  6. vc++项目 : error PRJ0002 : 错误的结果 1 (从“C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\rc.exe”返回)。

    右击工程->属性->配置属性->清单工具->输入和输出->嵌入清单,把是改成否

  7. 面试题-Java Web-网络通信

    1.HTTP响应的结构是怎么样的? HTTP响应由三个部分组成:状态码(Status Code):描述了响应的状态.可以用来检查是否成功的完成了请求.请求失败的情况下,状态码可用来找出失败的原因.如果 ...

  8. HTML,CSS,JS,JQ

    CSS: <style> <!--属性选择器--> .container input[type="text"][name="txt"]{ ...

  9. Symfony命令大全

    执行命令: $ php bin/console 查看一下命令 Symfony version 3.1.5 - app/dev/debug Usage: command [options] [argum ...

  10. MultipartResolver 文件上传

    SpringMVC 中文件上传 MultipartResolver 博客分类: SpringMVC - 基础篇   基于前面文章的基础上. 一.准备 需要的jar  二.配置 1.  spmvc-se ...