『高級篇』docker之開發(fā)課程EdgeService(16)

原創(chuàng)文章,歡迎轉(zhuǎn)載。轉(zhuǎn)載請注明:轉(zhuǎn)載自IT人故事會,謝謝!
原文鏈接地址:『高級篇』docker之開發(fā)課程EdgeService(16)

創(chuàng)新互聯(lián)是專業(yè)的木壘哈薩克網(wǎng)站建設(shè)公司,木壘哈薩克接單;提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行木壘哈薩克網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

課程的edgeService依賴于課程服務(wù)的dubbo服務(wù),對外提供的restAPI,跟用戶的EdgeService有點(diǎn)類似,只是一個(gè)調(diào)用的是thrift,一個(gè)調(diào)用的是dubbo,比較特殊的是課程的EdgeService需要用戶登錄后才可以訪問,如果沒有登錄的話,需要跳轉(zhuǎn)到登錄系統(tǒng)才可以訪問。源碼:https://github.com/limingios/msA-docker

『高級篇』docker之開發(fā)課程EdgeService(16)

新建模塊course-edge-servce

『高級篇』docker之開發(fā)課程EdgeService(16)

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

『高級篇』docker之開發(fā)課程EdgeService(16)

  • controller 調(diào)用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
    br/>*/
    @Controller
    public class CourseController {

    @Reference
    private ICourseService courseService;

    @RequestMapping(value = "/courseList", method = RequestMethod.GET)@ResponseBody
    br/>@ResponseBody

    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);
    }
}

『高級篇』docker之開發(fā)課程EdgeService(16)

  • 啟動類
    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<String> urlPatterns = new ArrayList<String>();
    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

『高級篇』docker之開發(fā)課程EdgeService(16)

業(yè)務(wù)流程梳理

『高級篇』docker之開發(fā)課程EdgeService(16)

  1. 課程EdgeService 依賴用戶EdgeService服務(wù),Thrift用戶服務(wù),課程服務(wù)。
  2. 課程 EdgeService pom 依賴了用戶登錄user-edge-service-client,user-edge-service-client用于檢測用戶是否登錄功能。需要調(diào)用用戶的服務(wù)。
  3. 當(dāng)用戶完成登錄后,課程EdgeService 訪問課程服務(wù),獲取課程的列表信息。
  4. 課程EdgeService 依賴用戶EdgeService服務(wù),Thrift用戶服務(wù)登錄控制,登錄后的跳轉(zhuǎn)功能。
  5. 課程EdgeService 依賴與course-dubbo-service-api服務(wù),用于獲取課程信息和用戶的課程信息。

梳理下dubbo的思路

  1. 建立對應(yīng)的api項(xiàng)目定義方法。最終提供一個(gè)jar包供調(diào)用方和服務(wù)提供方使用。
  2. 服務(wù)實(shí)現(xiàn)方引用api項(xiàng)目,實(shí)現(xiàn)里面的功能,提供端口,名稱,地址,zookeeper監(jiān)控中心。
  3. 服務(wù)調(diào)用方引用api項(xiàng)目,引用zookeeper的監(jiān)控中心發(fā)現(xiàn)服務(wù)。直接調(diào)用服務(wù)就可以用服務(wù)實(shí)現(xiàn)方的方法了。

程序演示

  • 啟動服務(wù)(按照順序)

    1. user-thrift-service
    2. user-edge-service
    3. course-dubbo-service
    4. course-edge-service
  • 界面演示
    1. 訪問http://127.0.0.1:8081 自動跳轉(zhuǎn)到http://127.0.0.1:8082/user/login
    2. 登錄獲取到token
      3.訪問地址http://127.0.0.1:8081/course/courseList?token=ux4g5z98mowv0qr6r6e6ietdo00nh0vl

『高級篇』docker之開發(fā)課程EdgeService(16)

『高級篇』docker之開發(fā)課程EdgeService(16)

PS:微服務(wù)跟之前說的一樣就是互相通過RPC的方式進(jìn)行通信,之間有自己的數(shù)據(jù)庫,只是RPC暴露接口的方式來獲取其他的微服務(wù)之間的數(shù)據(jù)。

『高級篇』docker之開發(fā)課程EdgeService(16)

本文名稱:『高級篇』docker之開發(fā)課程EdgeService(16)
瀏覽路徑:http://muchs.cn/article6/iepsig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航全網(wǎng)營銷推廣、微信公眾號網(wǎng)站改版、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)