『高级篇』docker之课程管理dubbo入门操练(14)

发布时间:2020-07-08 13:52:09 作者:IT人故事
来源:网络 阅读:188

原创文章,欢迎转载。转载请注明:转载自IT人故事会,谢谢!
原文链接地址:『高级篇』docker之课程管理dubbo入门操练(14)

接下来我们即将开始说课程管理,课程服务他是基于dubbo实现的,所以先来预热下,dubbo,对不熟悉的dubbo的老铁进行一下讲解。 源码:https://github.com/limingios/dubbo.git

『高级篇』docker之课程管理dubbo入门操练(14)

dubbo 介绍

『高级篇』docker之课程管理dubbo入门操练(14)

『高级篇』docker之课程管理dubbo入门操练(14)

快速入门讲解 spring 版本,

『高级篇』docker之课程管理dubbo入门操练(14)

『高级篇』docker之课程管理dubbo入门操练(14)

『高级篇』docker之课程管理dubbo入门操练(14)

『高级篇』docker之课程管理dubbo入门操练(14)

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 调用接口。

『高级篇』docker之课程管理dubbo入门操练(14)

推荐阅读:
  1. 『高级篇』docker之CICD(终结篇)(44)
  2. 『高级篇』docker之gitlab和jenkins安装(42)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

docker 容器 微服务

上一篇:解决python爬取视频时url地址中文乱码的方法

下一篇:python运行效率不高是什么原因

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》