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