SpringBoot (七) :SpringBoot 整合dubbo

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012081441/article/details/80696311

Dubbo是什么

Dubbo是alibaba的一款开源软件,它是基于java的RPC调用框架。
Dubbo主要提供了三种功能:

  1. 提供了基于接口的远程调用接口
  2. 容错性和负载均衡
  3. 服务自动注册及发现

安装ZooKeeper 服务注册中心

ZooKeeper 是一个分布式的,开放源码的分布式应用程序协调服务。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
下载 ZooKeeper ,地址 http://www.apache.org/dyn/closer.cgi/zookeeper

在这就不详细写安装ZooKeeper 的步骤了,不会的安装的亲可以问度娘哟~

项目结构

SpringBootDubbo-Server

这里写图片描述

springboot-dubbo-client

这里写图片描述

SpringBootDubbo-Server代码实现

1、pom.xml文件

<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>SpringBootDubbo-Server</groupId>
    <artifactId>springboot-dubbo-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- SpringBoot父类依赖引用 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <properties>
        <dubbo-spring-boot>1.0.0</dubbo-spring-boot>
    </properties>
    <dependencies>
        <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>${dubbo-spring-boot}</version>
        </dependency>
        <!-- SpringBoot web 组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

2、application.properties

## Dubbo 服务提供者配置
spring.dubbo.application.name=provider
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.fit

3、User.java

public class User implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String id;
    private String name;

    public User(String id, String name) {
        this.id = id;
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

4、Service层

public interface UserDubboService {
    public User getUser();
}


@Service
public class UserDubboServiceImpl implements UserDubboService {

    public User getUser() {
        // TODO Auto-generated method stub
        User user = new User("1","zlzhaoe");
        return user;
    }

}

5、ServerApp.java

@SpringBootApplication
public class ServerApp {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SpringApplication.run(ServerApp.class, args);
    }

}

springboot-dubbo-client代码实现

1、pom.xml文件

<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>SpringBootDubbo-Server</groupId>
    <artifactId>springboot-dubbo-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- SpringBoot父类依赖引用 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>
    <properties>
        <dubbo-spring-boot>1.0.0</dubbo-spring-boot>
    </properties>
    <dependencies>
        <!-- Spring Boot Dubbo 依赖 -->
        <dependency>
            <groupId>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>${dubbo-spring-boot}</version>
        </dependency>
        <!-- SpringBoot web 组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

2、application.properties

## 端口号
server.port=8081
## Dubbo
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
spring.dubbo.scan=com.fit

3、User.java

public class User implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String id;
    private String name;

    public User(String id, String name) {
        this.id = id;
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

4、service层

public interface UserDubboService {
    public User getUser();
}

5、Controller层

@RestController
public class UserController {
     @Reference
     private UserDubboService userDubboService;
     @RequestMapping(value ="getUser")
     public User getUser() {
            // TODO Auto-generated method stub
        User user = userDubboService.getUser();
        return user;
     }
}

6、启动SpringBootDubbo-Server,springboot-dubbo-client运行访问http://localhost:8081/getUser
这里写图片描述

源码下载

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页