新建模块course-edge-servce
pom增加依賴
<?xml version="1.0" encoding="UTF-8"?><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"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.idig8</groupId> <artifactId>course-edge-service</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>com.idig8</groupId> <artifactId>course-dubbo-service-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.idig8</groupId> <artifactId>user-edge-service-client</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies></project>
controller 调用course-dubbo-service-api 中的接口
package com.idig8.course.controller;import com.alibaba.dubbo.config.annotation.Reference;import com.idig8.course.dto.CourseDTO;import com.idig8.course.service.ICourseService;import com.idig8.thrift.user.dto.UserDTO;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest;import java.util.List;/** * Created by Michael on 2017/11/4. */@Controller@RequestMapping("/course")public class CourseController { @Reference private ICourseService courseService; @RequestMapping(value = "/courseList", method = RequestMethod.GET) @ResponseBody public List<CourseDTO> courseList(HttpServletRequest request) { UserDTO user = (UserDTO)request.getAttribute("user"); System.out.println(user.toString()); return courseService.courseList(); } }
增加filter组件
package com.idig8.course.filter;import com.idig8.thrift.user.dto.UserDTO;import com.idig8.user.client.LoginFilter;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Created by liming. */@Componentpublic class CourseFilter extends LoginFilter { @Value("${user.edge.service.addr}") private String userEdgeServiceAddr; @Override protected String userEdgeServiceAddr() { return userEdgeServiceAddr; } @Override protected void login(HttpServletRequest request, HttpServletResponse response, UserDTO userDTO) { request.setAttribute("user", userDTO); } }
启动类
package com.idig8.course;import com.idig8.course.filter.CourseFilter;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import java.util.ArrayList;import java.util.List;/** * Created by liming */@SpringBootApplicationpublic class ServiceApplication { public static void main(String args[]) { SpringApplication.run(ServiceApplication.class, args); } @Bean public FilterRegistrationBean filterRegistrationBean(CourseFilter courseFilter ) { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(courseFilter); List<String> urlPatterns = new ArrayList<String>(); urlPatterns.add("/*"); filterRegistrationBean.setUrlPatterns(urlPatterns); return filterRegistrationBean; } }
application.properties
server.port=8081#dubbo configspring.dubbo.application.name=course-service spring.dubbo.registry.address=zookeeper://47.98.183.16:2181spring.dubbo.scan=com.idig8.course user.edge.service.addr=127.0.0.1:8082
业务流程梳理
课程EdgeService 依赖用户EdgeService服务,Thrift用户服务,课程服务。
课程 EdgeService pom 依赖了用户登录user-edge-service-client,user-edge-service-client用于检测用户是否登录功能。需要调用用户的服务。
当用户完成登录后,课程EdgeService 访问课程服务,获取课程的列表信息。
课程EdgeService 依赖用户EdgeService服务,Thrift用户服务登录控制,登录后的跳转功能。
课程EdgeService 依赖与course-dubbo-service-api服务,用于获取课程信息和用户的课程信息。
梳理下dubbo的思路
建立对应的api项目定义方法。最终提供一个jar包供调用方和服务提供方使用。
服务实现方引用api项目,实现里面的功能,提供端口,名称,地址,zookeeper监控中心。
服务调用方引用api项目,引用zookeeper的监控中心发现服务。直接调用服务就可以用服务实现方的方法了。
程序演示
启动服务(按照顺序)
user-thrift-service
user-edge-service
course-dubbo-service
course-edge-service
界面演示
访问http://127.0.0.1:8081 自动跳转到http://127.0.0.1:8082/user/login
登录获取到token
3.访问地址http://127.0.0.1:8081/course/courseList?token=ux4g5z98mowv0qr6r6e6ietdo00nh0vl
作者:IT人故事会
链接:https://www.jianshu.com/p/6022a1f4229d
共同学习,写下你的评论
评论加载中...
作者其他优质文章