dubbo怎么写?dubbo入门教程

阳光 2022-06-30 15:45:28 java常见问答 4605

dubbo是一款微服务开发框架,它提供了RPC通信与微服务治理两大关键能力,因此dubbo也越来越深受各大公司的青睐,那dubbo怎么写?下面来我们就来给大家讲解一下dubbo入门教程。

dubbo怎么写.jpg

安装zookeeper

1.解压zookeeper-3.4.8.tar.gz,将/conf/zoo_sample.cfg修改为zoo.cfg,修改配置文件

dubbo怎么写?dubbo入门教程.jpg

2.双击启动zookeeper

1.jpg

创建工程B

新建B工程Maven项目

2.jpg

导入依赖

<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>io.menm91</groupId>
    <artifactId>dubbo-b</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>io.menm91</groupId>
            <artifactId>dubbo-b-api</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <!-- dubbo采用spring配置方式,所以需要导入spring容器依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <!-- 排除传递spring依赖 -->
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <!-- 导入zookeeper依赖 -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8081</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

配置log4j文件

log4j.rootLogger = DEBUG, A1
log4j.appender.A1 = org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = % -d
{
    yyyy - MM - dd HH: mm: ss, SSS
} [ % t][ % c] - [ % p] % m % n

创建B-API工程

新建B-API工程MAVEN项目

3 .jpg

新建User类

package io.menm91.bean;
//使用dubbo要求传输的对象必须实现序列化接口
public class User implements java.io.Serializable
{
    private static final long serialVersionUID = -2668999087589887337 L;
    private Long id;
    private String username;
    private String password;
    private Integer age;
    public Long getId()
    {
        return id;
    }
    public void setId(Long id)
    {
        this.id = id;
    }
    public String getUsername()
    {
        return username;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
    public String getPassword()
    {
        return password;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    public Integer getAge()
    {
        return age;
    }
    public void setAge(Integer age)
    {
        this.age = age;
    }
    @Override
    public String toString()
    {
        return "User [username=" + username + ", age=" + age + "]";
    }
}

创建UserService(接口)提供查询服务

package io.menm91.service;
import io.menm91.bean.User;
import java.util.List;
public interface UserService
{
    /**
     * 查询所有用户
     * @return
     */
    public List < User > queryAll();
}

在B工程中导入B-API的依赖

导入B-API工程的依赖

4.jpg

创建UserServiceImpl实现类

package io.menm91.service.impl;
import io.menm91.bean.User;
import io.menm91.service.UserService;
import java.util.ArrayList;
import java.util.List;
public class UserServiceImpl implements UserService
{
    /**
     * 这里通过模拟查询数据库返回用户信息
     */
    public List < User > queryAll()
    {
        List < User > list = new ArrayList < User > ();
        for (int i = 0; i < 10; i++)
        {
            User user = new User();
            user.setAge(10 + i);
            user.setId(Long.valueOf(i + 1));
            user.setPassword("123456");
            user.setUsername("username_" + i);
            list.add(user);
        }
        return list;
    }
}

编写dubbo配置文件

5.jpg

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-b-server" />
    <!-- 这里使用的注册中心是zookeeper -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"
        client="zkclient" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 将该接口暴露到dubbo中 -->
    <dubbo:service interface="io.menm91.service.UserService"
        ref="userServiceImpl" />
    <!-- 将具体的实现类加入到Spring容器中 -->
    <bean id="userServiceImpl" class="io.menm91.service.impl.UserServiceImpl" />
</beans>

编写web.xml文件

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>dubbo-b</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:dubbo/dubbo-*.xml</param-value>
    </context-param>
    <!--Spring的ApplicationContext 载入 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

启动tomcat

6.jpg

搭建A系统MAVEN项目

创建A系统maven项目

7.jpg

导入依赖

<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>io.menm91</groupId>
    <artifactId>dubbo-a</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>io.menm91</groupId>
            <artifactId>dubbo-b-api</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <!-- dubbo采用spring配置方式,所以需要导入spring容器依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.3</version>
            <exclusions>
                <exclusion>
                    <!-- 排除传递spring依赖 -->
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
</project>

配置log4j.properties文件

log4j.rootLogger = DEBUG, A1
log4j.appender.A1 = org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = % -d
{
    yyyy - MM - dd HH: mm: ss, SSS
} [ % t][ % c] - [ % p] % m % n

编写dubbo配置文件

8.jpg

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-a-consumer" />
    <!-- 这里使用的注册中心是zookeeper -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"
        client="zkclient" />
    <!-- 从注册中心中查找服务 -->
    <dubbo:reference id="userService"
        interface="io.menm91.service.UserService" />
</beans>

编写UserServiceTest测试类

9.jpg

package io.menm91.service;
import io.menm91.bean.User;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceTest
{
    private UserService userService;
    @Before
    public void setUp() throws Exception
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "classpath:dubbo/dubbo-*.xml");
        userService = applicationContext.getBean(UserService.class);
    }
    @Test
    public void testQueryAll()
    {
        List < User > userList = userService.queryAll();
        for (User user: userList)
        {
            System.out.println(user.toString());
        }
    }
}

查看输出结果

10.jpg

其实Dubbo是一款高性能、轻量级的开源Java RPC框架,有着非常广泛的应用,在开发中也是经常会使用到的一种框架,因此我们掌握好Dubbo的入门非常重要!最后大家如果想要了解更多java架构师知识,敬请关注奇Q工具网。

推荐阅读:

java中的泛型怎么用?Java泛型使用与不使用有区别吗?

java如何写一个加法算式?Java开发要注意什么?

java地图定位如何实现?java怎么做定位打卡功能?