SpringBoot 整合 Dubbo实践(实用文章)

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> SpringBoot 整合 Dubbo实践(实用文章)

SpringBoot 整合 Dubbo实践(实用文章)
作者:Cott cnblogs.com/cott/p/12373043.html

1.选择ZooKeeper作为注册中心

在linux环境中使用docker安装ZooKeeper


//拉取zookeeper镜像
docker pull zookeeper

//启动zookeeper
docker run -d -p 2181:2181 -v /mysoft/zookeeper/data/:/data/ --name=zookeeper --privileged zookeeper

使用zookeeper-dev-ZooInspector客户端连接工具查看

SpringBoot 整合 Dubbo实践(实用文章) SpringBoot 整合 Dubbo实践(实用文章)

2.创建maven项目统一声明接口,把所有接口提取到单独的项目,通过maven引入到其他项目

SpringBoot 整合 Dubbo实践(实用文章)

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"
    modelVersion4.0.0/modelVersion

    groupIdcom.cott.gmail/groupId
    artifactIdapi-interface/artifactId
    version1.0-SNAPSHOT/version

/project

UserAddress.java


package com.cott.gmail.bean;

import java.io.Serializable;

public class UserAddress implements Serializable {
    private Integer id;
    private String userAddress;
    private String userId;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }
}

OrderService.java


package com.cott.gmail.service;

import com.cott.gmail.bean.UserAddress;

import java.util.List;

public interface OrderService {

    ListUserAddress initOrder(String id);
}

UserService.java


package com.cott.gmail.service;

import com.cott.gmail.bean.UserAddress;

import java.util.List;

public interface UserService {
    ListUserAddress getAddress(String userId);
}

3.创建生产者SpringBoot项目

SpringBoot 整合 Dubbo实践(实用文章)

pom.xml引入上文接口项目,引入dubbo依赖


?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"
    modelVersion4.0.0/modelVersion
    parent
        groupIdorg.springframework.boot/groupId
        artifactIdspring-boot-starter-parent/artifactId
        version2.2.4.RELEASE/version
        relativePath/ !-- lookup parent from repository --
    /parent
    groupIdcom.cott.gmail/groupId
    artifactIdboot-user-service-provider/artifactId
    version0.0.1-SNAPSHOT/version
    nameboot-user-service-provider/name
    descriptionDemo project for Spring Boot/description

    properties
        java.version1.8/java.version
    /properties

    dependencies
        dependency
            groupIdcom.cott.gmail/groupId
            artifactIdapi-interface/artifactId
            version1.0-SNAPSHOT/version
        /dependency

        dependency
            groupIdcom.alibaba.boot/groupId
            artifactIddubbo-spring-boot-starter/artifactId
            version0.2.0/version
        /dependency

        dependency
            groupIdorg.springframework.boot/groupId
            artifactIdspring-boot-starter/artifactId
        /dependency

        dependency
            groupIdorg.springframework.boot/groupId
            artifactIdspring-boot-starter-test/artifactId
            scopetest/scope
            exclusions
                exclusion
                    groupIdorg.junit.vintage/groupId
                    artifactIdjunit-vintage-engine/artifactId
                /exclusion
            /exclusions
        /dependency
    /dependencies

    build
        plugins
            plugin
                groupIdorg.springframework.boot/groupId
                artifactIdspring-boot-maven-plugin/artifactId
            /plugin
        /plugins
    /build

/project

实现UserService接口,作为dubbo的生产者需要用@Service注解,为了和spring的@Service注解区分,所以这里用的是@Component注解。更多springboot实战类文章:

UserServiceImpl.java


package com.cott.gmail.bootuserserviceprovider.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cott.gmail.bean.UserAddress;
import com.cott.gmail.service.UserService;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Service
@Component
public class UserServiceImpl implements UserService {

    @Override
    public ListUserAddress getAddress(String uesrId) {
        UserAddress userAddress1 = new UserAddress();
        userAddress1.setId(1);
        userAddress1.setUserAddress("1");
        userAddress1.setUserId("1");
        UserAddress userAddress2 = new UserAddress();
        userAddress2.setId(2);
        userAddress2.setUserAddress("2");
        userAddress2.setUserId("2");

        return Arrays.asList(userAddress1, userAddress2);
    }
}

主方法上用@EnableDubbo启用Dubbo注解

BootUserServiceProviderApplication.java


package com.cott.gmail.bootuserserviceprovider;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class BootUserServiceProviderApplication {

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

}

配置文件指定应用名称,注册中心,服务协议和端口号

application.yml


dubbo:
  application:
    name: boot-user-service-provider
  registry:
    address: 192.168.200.128:2181
    protocol: zookeeper
  protocol:
    name: dubbo
    port: 20880

最后启动程序,在dubbo-admin中查看服务已经注册上去了。

SpringBoot 整合 Dubbo实践(实用文章)

4.创建消费者SpringBoot项目

SpringBoot 整合 Dubbo实践(实用文章)

pom.xml引入上文接口项目,引入dubbo依赖


?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"
    modelVersion4.0.0/modelVersion
    parent
        groupIdorg.springframework.boot/groupId
        artifactIdspring-boot-starter-parent/artifactId
        version2.2.4.RELEASE/version
        relativePath/ !-- lookup parent from repository --
    /parent
    groupIdcom.cott.gmail/groupId
    artifactIdboot-order-service-consumer/artifactId
    version0.0.1-SNAPSHOT/version
    nameboot-order-service-consumer/name
    descriptionDemo project for Spring Boot/description

    properties
        java.version1.8/java.version
    /properties

    dependencies
        dependency
            groupIdorg.springframework.boot/groupId
            artifactIdspring-boot-starter/artifactId
        /dependency

        dependency
            groupIdorg.springframework.boot/groupId
            artifactIdspring-boot-starter-test/artifactId
            scopetest/scope
            exclusions
                exclusion
                    groupIdorg.junit.vintage/groupId
                    artifactIdjunit-vintage-engine/artifactId
                /exclusion
            /exclusions
        /dependency
        dependency
            groupIdorg.springframework.boot/groupId
            artifactIdspring-boot-starter-web/artifactId
        /dependency
        dependency
            groupIdcom.cott.gmail/groupId
            artifactIdapi-interface/artifactId
            version1.0-SNAPSHOT/version
            scopecompile/scope
        /dependency
        dependency
            groupIdcom.alibaba.boot/groupId
            artifactIddubbo-spring-boot-starter/artifactId
            version0.2.0/version
        /dependency
    /dependencies

    build
        plugins
            plugin
                groupIdorg.springframework.boot/groupId
                artifactIdspring-boot-maven-plugin/artifactId
            /plugin
        /plugins
    /build

/project

实现OrderServiece接口,这里的@Service是spring的,这里使用@Reference注解声明UserService是通过远程调用注入进来

OrderServiceImpl.java


package com.cott.gmail.bootorderserviceconsumer.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cott.gmail.bean.UserAddress;
import com.cott.gmail.service.OrderService;
import com.cott.gmail.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Reference
    UserService userService;

    @Override
    public ListUserAddress initOrder(String id) {
        System.out.println("id= " + id);
        ListUserAddress list = userService.getAddress("1");
        for (UserAddress user : list
        ) {
            System.out.println(user.getUserAddress());
        }
        return list;
    }
}

OrderController.java


package com.cott.gmail.bootorderserviceconsumer.controller;

import com.cott.gmail.bean.UserAddress;
import com.cott.gmail.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class OrderController {

    @Autowired
    OrderService orderService;

    @ResponseBody
    @GetMapping("/initOrder")
    public ListUserAddress initOrder(@RequestParam(name = "id") String id) {
        return orderService.initOrder(id);
    }
}

@EnableDubbo启用Dubbo注解

BootOrderServiceConsumerApplication.java


package com.cott.gmail.bootorderserviceconsumer;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class BootOrderServiceConsumerApplication {

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

}

配置文件中声明服务名称,注册中心,以及tomcat对外端口号

application.yml


dubbo:
  application:
    name: order-service-provider
  registry:
    address: 192.168.200.128:2181
    protocol: zookeeper

server:
  port: 8081

至此,一个简单的dubbo项目已经开发完成,下面启动消费者项目,在浏览器中输入url,得到返回结果

SpringBoot 整合 Dubbo实践(实用文章)

END

Java面试题专栏

SpringBoot 整合 Dubbo实践(实用文章)

我知道你 “在看SpringBoot 整合 Dubbo实践(实用文章)

原文始发于微信公众号(Java知音):

本人花费半年的时间总结的《Java面试指南》已拿腾讯等大厂offer,已开源在github ,欢迎star!

本文GitHub https://github.com/OUYANGSIHAI/JavaInterview 已收录,这是我花了6个月总结的一线大厂Java面试总结,本人已拿大厂offer,欢迎star

原文链接:blog.ouyangsihai.cn >> SpringBoot 整合 Dubbo实践(实用文章)


 上一篇
最详细的 Spring Boot 多模块开发与排坑指南 最详细的 Spring Boot 多模块开发与排坑指南
来自:未读代码 创建项目创建一个 SpringBoot 项目非常的简单,简单到这里根本不用再提。你可以在使用 IDEA 新建项目时直接选择 Spring Initlalize 创建一个 Spring Boot 项目,也可以使用 Sprin
下一篇 
SpringBoot 整合 RabbitMQ 实践 SpringBoot 整合 RabbitMQ 实践
作者:Cott www.cnblogs.com/cott/p/12402423.html 前文总结了,本文基于这套框架,引入RabbitMQ消息队列。 1.在linux环境使用docker安装RabbitMQ //拉取镜