spingboot+dubbo+zk

本文详细介绍如何在SpringBoot环境中整合Dubbo微服务框架,包括项目搭建、接口定义、服务提供与消费等步骤,实现远程服务调用。

软件版本:

Springboot2.0

Dubbo2.6

zookeeper3.4.6

废话少说,正式开始。

Step1:创建项目:使用maven新建一个DubboExample的pom工程,然后在这个parent项目下面创建三个module项目,一个DubboProvider,一个DubboConsumer,一个DubboCommon。

\

上图:创建DubboExample父项目

\

上图:创建好后的项目。

右键点击DubboExample项目,创建maven的module项目

\

\

重复上面的动作创建DubboConsumer,创建好后如下图:

\

Step2:DubboExample工程的pom文件修改如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelversion>4.0.0</modelversion>

    <parent>

        <groupid>org.springframework.boot</groupid>

        spring-boot-starter-parent</artifactid>

        <version>2.0.3.RELEASE</version>

    </parent>

    <groupid>com.example</groupid>

    DubboExample</artifactid>

    <version>1.0.0</version>

    <packaging>pom</packaging>

    <name>DubboExample</name>

    <description>DubboExample</description>

    <modules>

        <module>DubboProvider</module>

        <module>DubboConsumer</module>

        <module>DubboCommon</module>

    </modules>

    <dependencies>

        <dependency>

            <groupid>org.springframework.boot</groupid>

            spring-boot-starter</artifactid>

        </dependency>

        <dependency>

            <groupid>org.springframework.boot</groupid>

            spring-boot-starter-test</artifactid>

            <scope>test</scope>

        </dependency>

        <dependency>

            <groupid>org.springframework.boot</groupid>

            spring-boot-starter-web</artifactid>

        </dependency>

        <dependency>

            <groupid>com.alibaba.spring.boot</groupid>

            dubbo-spring-boot-starter</artifactid>

            <version>2.0.0</version>

        </dependency>

        <dependency>

            <groupid>com.101tec</groupid>

            zkclient</artifactid>

            <version>0.10</version>

        </dependency>

    </dependencies>

</project>

Step3:DubboProvider和DubboConsumer工程的pom分别添加如下内容(依赖DubboCommon工程)。

1

2

3

4

5

6

7

<dependencies>

    <dependency>

        <groupid>com.example</groupid>

        DubboCommon</artifactid>

        <version>0.0.1-SNAPSHOT</version>

    </dependency>

</dependencies>

Step4:DubboCommon工程添加Service接口如下:

DemoService接口

\

DemoService.java

1

2

3

4

package com.example.service;

public interface DemoService {

    public String getDemoString();

}

Step5:我们看看服务的提供者DubboProvider

\

ApplicationProvider.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

package com.example.provider.service;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ComponentScan;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;

 

@SpringBootApplication

@ComponentScan("com.example")

@EnableDubboConfiguration

public class ApplicationProvider {

    public static void main(String[] args) {

        SpringApplication.run(ApplicationProvider.class, args);

    }

}

DemoServiceImpl.java

1

package com.example.provider.service;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;

import com.example.service.DemoService;

 

@Service

@Component //这个必须要加

public class DemoServiceImpl implements DemoService {

 

    @Override

    public String getDemoString() {

        return "Hello Dubbo!";

    }

 

}

application.properties

1

2

3

4

5

6

7

8

9

server.port=9092

spring.dubbo.scan.basePackages  = com.example.provider.service

spring.dubbo.application.id = dubbo-dustin-demo

spring.dubbo.application.name = dubbo-dustin-demo

spring.dubbo.protocol.id = dubbo

spring.dubbo.protocol.name = dubbo

spring.dubbo.protocol.port = 12345

spring.dubbo.registry.id = my-registry

spring.dubbo.registry.address = zookeeper://127.0.0.1:2181

Step6:我们看看服务的提供者DubboConsumer

\

DemoController.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

package com.example.controller;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.example.service.DemoService;

 

@RestController

@RequestMapping("demo")

public class DemoController {

    @Autowired

    private DemoService demoService;

     

    @RequestMapping("index")

    public String index(){

        return demoService.getDemoString();

    }

}

ApplicationConsumer.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

package com.example.system;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ImportResource;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;

 

@SpringBootApplication

@EnableDubboConfig

@ComponentScan("com.example")

@ImportResource(locations= {"classpath:spring-dubbo.xml"})  //加载dubbo配置文件

public class ApplicationConsumer {

    public static void main(String[] args) {

        SpringApplication.run(ApplicationConsumer.class, args);

    }

}

application.properties

1

server.port=9093

spring-dubbo.xml

1

2

3

4

5

6

7

8

9

10

11

<!--?xml version="1.0" encoding="UTF-8"?-->

<beans xmlns="https://www.springframework.org/schema/beans" xmlns:dubbo="https://code.alibabatech.com/schema/dubbo" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://www.springframework.org/schema/beans

       https://www.springframework.org/schema/beans/spring-beans.xsd

       https://code.alibabatech.com/schema/dubbo

       https://code.alibabatech.com/schema/dubbo/dubbo.xsd">

     

       <dubbo:application id="dubbo-consumer-demo" name="dubbo-consumer-demo">

       <dubbo:registry address="zookeeper://127.0.0.1:2181">

       <dubbo:protocol id="dubbo" name="dubbo" port="12345">

       <dubbo:reference id="demoService" interface="com.example.service.DemoService">

</dubbo:reference></dubbo:protocol></dubbo:registry></dubbo:application></beans>

Step7:启动zookeeper

安装启动参考文章?zookeeper安装和使用(Windows环境)

Step8:启动DubboProvider

\

\

Step8:启动DubboConsumer

\

Step8:测试

在搜索栏输入localhost:9093/demo/index,查看结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值