springboot dubbo 既是提供者又是消费者demo源码

项目provicderandconsumer 结构如下:
在这里插入图片描述
pom.xml (我的springboot 版本是2.0.4.RELEASE)

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>provicderandconsumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>provicderandconsumer</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>compile</scope>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

公共服务接口 UserService

package com.example.springbootapi.dubbo.service;

/**
 * @author
 */
public interface UserService {
    public String print(String s);
}

UserServiceImpl

package com.example.provicderandconsumer.service;

import com.example.springbootapi.dubbo.service.UserService;

//提供者提供的接口服务 service 采用xml配置
//@Service(interfaceClass = UserService.class)
//@Component
public class UserServiceImpl implements UserService {
    @Override
    public String print(String s) {
        return "say2:" + s;
    }
}

UserController

package com.example.provicderandconsumer.controller;

import com.example.springbootapi.dubbo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author
 */
@Controller
public class UserController {
    //reference 在xml配置,这里需要用注解Autowired
    @Autowired
    public UserService userService;

    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        String s = userService.print("123sssssssss");
        return s;
    }
}

dubbo.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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="60000"/>
    <!-- port值-1 表示让dubbo自行找一个可用的port -->
    <dubbo:protocol port="-1" name="dubbo"/>
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo"/>

    <!--消费者-->
    <!--关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成。url="dubbo//172.16.1.112:20880"-->
    <dubbo:consumer check="false" />
    <!--reference 采用xml配置实现,在代码中获取远程服务需要加注解@Autowired-->
    <dubbo:reference id="userService" check="false"  interface="com.example.springbootapi.dubbo.service.UserService"/>
    <dubbo:annotation package="com.example.provicderandconsumer.*"/>

    <!--提供者-->
    <dubbo:service protocol="dubbo" ref="userService" interface="com.example.springbootapi.dubbo.service.UserService"/>
    <bean id="userService" class="com.example.provicderandconsumer.service.UserServiceImpl"/>

</beans>

启动类 ProvicderandconsumerApplication

package com.example.provicderandconsumer;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

@EnableDubboConfiguration
@ImportResource(locations = {"classpath:dubbo.xml"})
@ComponentScan(basePackages = "com.example.provicderandconsumer.*")
@SpringBootApplication
public class ProvicderandconsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProvicderandconsumerApplication.class, args);
    }

}

application.properties

server.port=9993

启动springboot程序,如下:
在这里插入图片描述

登录 dubbo管理平台 , 这里显示虽然没有消费者(这个暂时不知道原因),实际是有的,下面测试运行正常。
在这里插入图片描述

浏览器输入: http://127.0.0.1:9993/test
在这里插入图片描述

源码

链接:https://pan.baidu.com/s/1hlGIkmFHpvBs-1uv2eahXQ
提取码:qwfj

注意:需要先启动 zookeeper 和 dubbo-admin(管理平台) ,如果没有安装看下我之前的文章:https://blog.csdn.net/a704397849/article/details/91904085

更多的springboot源码看下我的博客分类dubbo

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值