『高级篇』docker之课程管理dubbo入门操练(14)
2019-01-30 15:31:49
李明
  • 访问次数: 375
  • 注册日期: 2018-07-09
  • 最后登录: 2019-08-08
接下来我们即将开始说课程管理,课程服务他是基于dubbo实现的,所以先来预热下,dubbo,对不熟悉的dubbo的老铁进行一下讲解。 源码: github.com/limingios/du


v2-f2e1d1c6dac4582899fc5a8b6b4d6342_hd.j


dubbo 介绍

高性能的基于java的,RPC框架。dubbo是阿里巴巴开源的一个项目,就像大多的RPC框架,dubbo的思想围绕一个服务,指定一个方法的参数和返回值,这个方法可以被远程调用的,在服务端,会实现这个接口,会运行dubbo的服务用来处理客户端的调用,在客户端,会有一个存根它提供和服务端想通的方法。其实这些概念用java的术语:首先要定义一个接口,这个接口在服务端和客户端公用,服务端会完成这个接口的实现,客户端通过接口的描述来调用服务端。


v2-e0cacaecb172e914b12f6793fcb76916_hd.j



v2-6618b41d98a63941a820da39c9e0478c_hd.j


快速入门讲解 spring 版本,

  • 根据官网来创建


v2-18b1eb29afcca1f7e16a6001d585d126_hd.j


  • 创建三个api,provider,consumer 三个项目


v2-e7ff0448a3e0eab5b8a032e4d24fee16_hd.j



v2-4c7ff65571332beb895f617dd0011ca3_hd.j



v2-845a00e13e81466e8367144fd7b2f400_hd.j


  • 用了本地调用。
    provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="demo-provider"  />
    <!-- 使用multicast广播注册中心暴露服务地址 -->
    <dubbo:registry register="false" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
</beans>

consumer.xml

<?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>
        <artifactId>dubbo-demo</artifactId>
        <groupId>com.idig8</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>dubbo-demo-consumer</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.idig8</groupId>
            <artifactId>dubbo-demo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.0.RELEASE</version>
        </dependency>
    </dependencies>
</project>

快速入门讲解springboot版本

SpringBoot整合dubbo示例

一、定义API(API模块)

1. 定义api

package com.idig8.springboot.dubbo.demo;
public interface DemoService {
    String sayHello(String name);
}

2. install到本地

对外提供的maven坐标如下:

<dependency>
    <groupId>com.idig8</groupId>
    <artifactId>dubbo-demo-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

二、服务提供者(provider模块)

1. 增加maven依赖

<!-- springboot parent -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
</parent>
<!-- springboot dubbo starter -->
<dependency>
    <groupId>io.dubbo.springboot</groupId>
    <artifactId>spring-boot-starter-dubbo</artifactId>
    <version>1.0.0</version>
</dependency>
<!-- api dependence -->
<dependency>
    <groupId>com.idig8</groupId>
    <artifactId>dubbo-demo-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

2. 实现API接口

package com.idig8.springboot.dubbo.demo.provider;
import com.alibaba.dubbo.config.annotation.Service;
import com.idig8.springboot.dubbo.demo.DemoService;
@Service
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello, " + name + " (from Spring Boot)";
    }
}

3. springboot配置文件 - application.properties

spring.dubbo.application.name=demo-provider
#这里使用广播的注册方式,
#如果有Can't assign address异常需要加vm参数:
#-Djava.net.preferIPv4Stack=true
spring.dubbo.registry.address=multicast://224.5.6.7:1234
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.idig8.springboot.dubbo.demo.provider

4. 启动类

package com.idig8.springboot.dubbo.demo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainProvider {
    public static void main(String[] args) {
        SpringApplication.run(MainProvider.class,args);
    }
}

三、服务消费者(consumer模块)

1. 增加maven依赖

<!-- springboot parent -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
</parent>
<!-- springboot web starter -->
<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- springboot dubbo starter -->
<dependency>
    <groupId>io.dubbo.springboot</groupId>
    <artifactId>spring-boot-starter-dubbo</artifactId>
    <version>1.0.0</version>
</dependency>
<!-- api dependency -->
<dependency>
    <groupId>com.idig8</groupId>
    <artifactId>dubbo-demo-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

2. 实现controller

package com.idig8.springboot.dubbo.demo.consumer;
import com.alibaba.dubbo.config.annotation.Reference;
import com.idig8.springboot.dubbo.demo.DemoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoConsumerController {
    @Reference
    private DemoService demoService;
    @RequestMapping("/sayHello")
    public String sayHello(@RequestParam String name) {
        return demoService.sayHello(name);
    }
}

3. springboot配置文件 - application.properties

server.port=8080
#dubbo config
spring.dubbo.application.name=demo-consumer
#这里使用广播的注册方式,
#如果有Can't assign address异常需要加vm参数:
#-Djava.net.preferIPv4Stack=true
spring.dubbo.registry.address=multicast://224.5.6.7:1234
spring.dubbo.scan=com.idig8.springboot.dubbo.demo.consumer

4. 启动类

package com.idig8.springboot.dubbo.demo.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }
}

PS:dubbo的入门也就到这里,从spring 和springboot 对dubbo的整合。
流程基本之前也说,api 建立接口,provider 实现接口, consumer 调用接口。