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

>>2020,微服务装逼指南

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">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cott.gmail</groupId>
    <artifactId>api-interface</artifactId>
    <version>1.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 {

    List<UserAddress> initOrder(String id);
}

UserService.java

package com.cott.gmail.service;

import com.cott.gmail.bean.UserAddress;

import java.util.List;

public interface UserService {
    List<UserAddress> 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">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cott.gmail</groupId>
    <artifactId>boot-user-service-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-user-service-provider</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>com.cott.gmail</groupId>
            <artifactId>api-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

实现UserService接口,作为dubbo的生产者需要用@Service注解,为了和spring的@Service注解区分,所以这里用的是@Component注解。更多springboot实战类文章: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 List<UserAddress> 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:
    nameboot-user-service-provider
  registry:
    address: 192.168.200.128:2181
    protocolzookeeper
  protocol:
    namedubbo
    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">

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cott.gmail</groupId>
    <artifactId>boot-order-service-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot-order-service-consumer</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.cott.gmail</groupId>
            <artifactId>api-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-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 List<UserAddress> initOrder(String id) {
        System.out.println("id= " + id);
        List<UserAddress> 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 List<UserAddress> 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:
    nameorder-service-provider
  registry:
    address: 192.168.200.128:2181
    protocolzookeeper

server:
  port: 8081

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

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

END

Java面试题专栏

【51期】一道阿里面试题:说说你知道的关于BeanFactory和FactoryBean的区别
【52期】记一道简单的Java面试题,但答错率很高!
【53期】面试官:谈一下数据库分库分表之后,你是如何解决事务问题?
【54期】Java序列化三连问,是什么?为什么需要?如何实现?
【55期】面试中经常被问到Java引用类型原理,带你深入剖析
【56期】你说你熟悉并发编程,那么你说说Java锁有哪些种类,以及区别
【57期】面试官问,MySQL建索引需要遵循哪些原则呢?
【58期】盘点那些面试中最常问的MySQL问题,第一弹!
【59期】MySQL索引是如何提高查询效率的呢?(MySQL面试第二弹)
【60期】事务隔离级别中的可重复读能防幻读吗?(MySQL面试第三弹)


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

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

原文始发于微信公众号(Java知音):SpringBoot 整合 Dubbo实践(实用文章)