重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!
原文链接地址:『高级篇』docker之开发课程EdgeService(16)创新互联是专业的木垒哈萨克网站建设公司,木垒哈萨克接单;提供成都网站制作、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行木垒哈萨克网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
课程的edgeService依赖于课程服务的dubbo服务,对外提供的restAPI,跟用户的EdgeService有点类似,只是一个调用的是thrift,一个调用的是dubbo,比较特殊的是课程的EdgeService需要用户登录后才可以访问,如果没有登录的话,需要跳转到登录系统才可以访问。源码:https://github.com/limingios/msA-docker
org.springframework.boot
spring-boot-starter-parent
1.5.3.RELEASE
4.0.0
com.idig8
course-edge-service
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
commons-lang
commons-lang
2.6
io.dubbo.springboot
spring-boot-starter-dubbo
1.0.0
com.idig8
course-dubbo-service-api
1.0-SNAPSHOT
com.idig8
user-edge-service-client
1.0-SNAPSHOT
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
br/>*/
@Controller
public class CourseController {
@Reference
private ICourseService courseService;
@RequestMapping(value = "/courseList", method = RequestMethod.GET)@ResponseBody
br/>@ResponseBody
courseList(HttpServletRequest request) {
UserDTO user = (UserDTO)request.getAttribute("user");
System.out.println(user.toString());
return courseService.courseList();
}
}
![](/upload/otherpica42/11223715-58de6c1baaf6bccf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* 增加filter组件
``` java
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.
*/
@Component
public 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*/
@SpringBootApplication
br/>*/
@SpringBootApplication
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 urlPatterns = new ArrayList();
urlPatterns.add("/*");
filterRegistrationBean.setUrlPatterns(urlPatterns);
return filterRegistrationBean;
}
}
![](/upload/otherpica42/11223715-5cf13f17115c9b6d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
* application.properties
``` properties
server.port=8081
#dubbo config
spring.dubbo.application.name=course-service
spring.dubbo.registry.address=zookeeper://47.98.183.16:2181
spring.dubbo.scan=com.idig8.course
user.edge.service.addr=127.0.0.1:8082
启动服务(按照顺序)
PS:微服务跟之前说的一样就是互相通过RPC的方式进行通信,之间有自己的数据库,只是RPC暴露接口的方式来获取其他的微服务之间的数据。