dubbo入門之springboot+dubbo

1、創建mave項目:

圖片.png

2、修改pom.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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ym</groupId>
    <artifactId>springbootdubbo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>service</module>
        <module>serviceImpl</module>
        <module>testweb</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.4.6.v20170531</version>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

3、創建遠程接口定義模塊:

圖片.png

4、創建接口:

圖片.png

5、創建接口實現模塊:

圖片.png

6、springboot整合application.yml:

spring:
  application:
    name: dubbo-provider-app
server:
  port: 9090
dubbo:
  scan:
    base-packages: com.ym.service
  application:
    id: dubbo-provider
    name: dubbo-provider
  protocol:
    id: duboo
    name: dubbo
    port: 12345
    status: server #標明是一個server
  registry:
    id: my-reg
    address: zookeeper://192.168.1.224:2181
endpoint:
  dubbo:
    enabled: true
management:
  port: 9091
  health:
    dubbo:
      status:
        extras: load,threadpool
        defaults: memory

7、實現接口:

圖片.png

package com.ym.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ym.service.TestService;

/**
 * Created with IntelliJ IDEA.
 * User: Dony
 * Date: 2018/11/17
 * Time: 16:04
 * Description:
 */
//@Service此時不再使用這個註解
@Service(version = "1.0",application = "${dubbo.application.id}",protocol = "${dubbo.protocol.id}",registry = "${dubbo.registry.id}") //這個註解時dubbo提供的,其作用是創建此類型的對象,然後作爲服務提供者發佈
public class TestServiceImpl implements TestService {
    @Override
    public String getData(String name) {
        return "result=" + name;
    }
}

8、創建啓動程序:

package ym;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created with IntelliJ IDEA.
 * User: Dony
 * Date: 2018/11/17
 * Time: 20:34
 * Description:
 */
@SpringBootApplication
public class StartSpringBootMain {
    public static void main(String[] args) {
        SpringApplication.run(StartSpringBootMain.class);
    }
}

觀察dubbo控制檯:

圖片.png

圖片.png


9、創建消費端:

圖片.png


10、整合springboot和dubbo(application.yml):

spring:
  application:
    name: dubbo-consumer-app
server:
  port: 8080
dubbo:
  application:
    id: dubbo-consumer
    name: dubbo-consumer
  protocol:
    id: duboo
    name: dubbo
    port: 54321
  registry:
    id: my-reg
    address: zookeeper://192.168.1.224:2181

11、創建controller程序,引用遠程接口:

package com.ym.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ym.service.TestService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * User: Dony
 * Date: 2018/11/17
 * Time: 20:58
 * Description:
 */
@RestController
@RequestMapping("/test")
public class TestController {

    //@Autowired 不是使用這個註解,使用dubbo註解引用遠程服務
    @Reference(version = "1.0", application = "${dubbo.application.id}")
    private TestService testService;

    @RequestMapping("/getdata/{name}")
    public String getData(@PathVariable("name") String name) {
        return testService.getData(name);
    }
}

12、創建啓動程序,並啓動項目:

package com.ym;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created with IntelliJ IDEA.
 * User: Dony
 * Date: 2018/11/17
 * Time: 21:14
 * Description:
 */
@SpringBootApplication(scanBasePackages = "com.ym")
public class StartSpringBootMain {
    public static void main(String[] args) {
        SpringApplication.run(StartSpringBootMain.class);
    }
}

13、觀察dubbo控制檯:

圖片.png

圖片.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章