淘淘商城day10课堂笔记.docx

上传人:b****8 文档编号:30475877 上传时间:2023-08-15 格式:DOCX 页数:24 大小:655.77KB
下载 相关 举报
淘淘商城day10课堂笔记.docx_第1页
第1页 / 共24页
淘淘商城day10课堂笔记.docx_第2页
第2页 / 共24页
淘淘商城day10课堂笔记.docx_第3页
第3页 / 共24页
淘淘商城day10课堂笔记.docx_第4页
第4页 / 共24页
淘淘商城day10课堂笔记.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

淘淘商城day10课堂笔记.docx

《淘淘商城day10课堂笔记.docx》由会员分享,可在线阅读,更多相关《淘淘商城day10课堂笔记.docx(24页珍藏版)》请在冰豆网上搜索。

淘淘商城day10课堂笔记.docx

淘淘商城day10课堂笔记

1.今日大纲

1、Dubbo入门学习

2、使用dubbo优化单点登录系统

2.系统间服务调用方式

2.1.浏览器直接访问

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

2.2.Httpclient方式

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

http协议是短连接。

2.3.RPC方式

采用长连接方式。

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

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

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

4.Dubbo入门

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

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

5.1.创建taotao-sso-query-api工程

5.1.1.创建maven工程

该工程定义查询接口。

5.1.2.导入依赖

//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.0http:

//maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.taotao.parent

taotao-parent

0.0.1-SNAPSHOT

com.taotao.sso.query

taotao-sso-query-api

1.0.0-SNAPSHOT

mon

taotao-common

1.0.0-SNAPSHOT

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

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

packagecom.taotao.sso.query.bean;

importjava.util.Date;

importcom.fasterxml.jackson.annotation.JsonIgnore;

publicclassUser{

privateLongid;

privateStringusername;

@JsonIgnore

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

privateStringpassword;

privateStringphone;

privateStringemail;

privateDatecreated;

privateDateupdated;

publicLonggetId(){

returnid;

}

publicvoidsetId(Longid){

this.id=id;

}

publicStringgetUsername(){

returnusername;

}

publicvoidsetUsername(Stringusername){

this.username=username;

}

publicStringgetPassword(){

returnpassword;

}

publicvoidsetPassword(Stringpassword){

this.password=password;

}

publicStringgetPhone(){

returnphone;

}

publicvoidsetPhone(Stringphone){

this.phone=phone;

}

publicStringgetEmail(){

returnemail;

}

publicvoidsetEmail(Stringemail){

this.email=email;

}

publicDategetCreated(){

returncreated;

}

publicvoidsetCreated(Datecreated){

this.created=created;

}

publicDategetUpdated(){

returnupdated;

}

publicvoidsetUpdated(Dateupdated){

this.updated=updated;

}

}

5.1.4.定义服务接口

packagecom.taotao.sso.query.api;

importcom.taotao.sso.query.bean.User;

publicinterfaceUserQueryService{

/**

*根据token查询User对象

*

*@return

*/

publicUserqueryUserByToken(Stringtoken);

}

5.2.创建taotao-sso-query-service

5.2.1.创建maven工程

5.2.2.导入依赖

//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.0http:

//maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.taotao.parent

taotao-parent

0.0.1-SNAPSHOT

com.taotao.sso.query

taotao-sso-query-service

1.0.0-SNAPSHOT

war

mon

taotao-common

1.0.0-SNAPSHOT

com.taotao.sso.query

taotao-sso-query-api

1.0.0-SNAPSHOT

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

org.springframework

spring-webmvc

org.slf4j

slf4j-log4j12

com.alibaba

dubbo

2.5.3

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

spring

org.springframework

--配置Tomcat插件-->

org.apache.tomcat.maven

tomcat7-maven-plugin

8087

/

5.2.3.编写配置文件

applicationContext-redis.xml:

applicationContext.xml:

taotao-sso-query-servlet.xml:

5.2.4.编写web.xml

xmlversion="1.0"encoding="UTF-8"?

>

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xmlns="

xsi:

schemaLocation="

id="WebApp_ID"version="2.5">

taotao-sso-query-service

contextConfigLocation

classpath:

spring/applicationContext*.xml

--Spring的ApplicationContext载入-->

org.springframework.web.context.ContextLoaderListener

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

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF8

encodingFilter

/*

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

taotao-sso

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:

spring/taotao-sso-query-servlet.xml

1

taotao-sso

/

index.html

5.3.编写UserQueryService的实现

packagecom.taotao.sso.query.service;

importmons.lang3.StringUtils;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.stereotype.Service;

importcom.fasterxml.jackson.databind.ObjectMapper;

importmon.service.RedisService;

importcom.taotao.sso.query.api.UserQueryService;

importcom.taotao.sso.query.bean.User;

@Service

publicclassUserQueryServiceImplimplementsUserQueryService{

@Autowired

privateRedisServiceredisService;

privatestaticfinalStringREDIS_KEY="TOKEN_";

privatestaticfinalIntegerREDIS_TIME=60*30;

privatestaticfinalObjectMapperMAPPER=newObjectMapper();

@Override

publicUserqueryUserByToken(Stringtoken){

Stringkey=REDIS_KEY+token;

try{

StringjsonData=this.redisService.get(key);

if(StringUtils.isEmpty(jsonData)){

//登录超时

returnnull;

}

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

this.redisService.expire(key,REDIS_TIME);

returnMAPPER.readValue(jsonData,User.class);

}catch(Exceptione){

e.printStackTrace();

}

returnnull;

}

}

5.4.实现RESTfulwebservice

packagecom.taotao.sso.query.controller;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.http.HttpStatus;

importorg.springframework.http.ResponseEntity;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.PathVariable;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.RequestMethod;

importcom.taotao.sso.query.api.UserQueryService;

importcom.taotao.sso.query.bean.User;

@RequestMapping("user")

@Controller

publicclassUserController{

@Autowired

privateUserQueryServiceuserQueryService;

/**

*根据token查询用户信息

*

*@paramtoken

*@return

*/

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

publicResponseEntityqueryUserByToken(@PathVariable("token")Stringtoken){

try{

Useruser=this.userQueryService.queryUserByToken(token);

if(null==user){

returnResponseEntity.status(HttpStatus.NOT_FOUND).body(null);

}

returnResponseEntity.ok(user);

}catch(Exceptione){

e.printStackTrace();

}

returnResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);

}

}

5.4.1.测试

5.4.2.修改为域名访问

配置nginx:

测试:

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

5.5.1.启动zookeeper服务

5.5.2.导入zookeeper客户端依赖

5.5.3.编写dubbo配置文件

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

xsi:

schemaLocation="http:

//www.springframework.org/schema/beanshttp:

//www.springframework.org/schema/beans/spring-beans-4.0.xsd

http:

//www.springframework.org/schema/contexthttp:

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

http:

//www.springframework.org/schema/aophttp:

//www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp:

//www.springframework.org/schema/txhttp:

//www.springframework.org/schema/tx/spring-tx-4.0.xsd

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

applicationname="dubbo-sso-query-server"/>

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

registryaddress="zookeeper:

//127.0.0.1:

2181"client="zkclient"/>

--用dubbo协议在20880端口暴露服务-->

protocolname="dubbo"port="20880"/>

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

serviceinterface="com.taotao.sso.query.api.UserQueryService"ref="userQueryServiceIm

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1