Simple use case of dubbo-with/without registration center

1. Create a dubbo-server-api project, the directory structure is as follows

1.1 The pom.xml file is as follows

<?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.dubbo.server.demo</groupId>
  <artifactId>dubbo-server-api</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>dubbo-server-api</name>

</project>

1.2 Define the interface contract

package com.dubbo.server.demo;

/**
 * @description: 登陆服务
 * @author: tiger
 * @create: 2021-03-07 11:18
 */
public interface LoginServer {

    /**
     * 登陆接口
     *
     * @param password
     * @param name
     * @return
     */
    String login(String password, String name);
}

1.3 Type dubbo-server-api into a jar package for service providers and service callers to use

2. Create the dubbo-server project, the directory structure is as follows

2.1 The pom.xml file is as follows, relying on the dubbo-server-api project jar package and dubbo 2.7.8 package to implement the interfaces defined in it and provide external services

<?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.dubbo.server.demo</groupId>
    <artifactId>dubbo-server</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>dubbo-server</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>

        <!-- api接口契约 -->
        <dependency>
            <groupId>com.dubbo.server.demo</groupId>
            <artifactId>dubbo-server-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- dubbo 依赖包,2.7.8 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.8</version>
        </dependency>

        <!-- zk注册中心依赖的包  -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>2.7.8</version>
            <type>pom</type>
        </dependency>

        <!-- nacos注册中心依赖的包 -->
        <!--    <dependency>-->
        <!--      <groupId>com.alibaba.nacos</groupId>-->
        <!--      <artifactId>nacos-client</artifactId>-->
        <!--      <version>1.2.1</version>-->
        <!--    </dependency>-->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>
2.2 创建文件 dubbo-server/src/resources/META-INF/spring/application.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:application name="dubbo-server"/>

    <!-- 不需要注册到服务注册中心 -->
    <!--    <dubbo:registry address="N/A"/>-->

    <!-- 注册的注册中心【zk】,可以是其他注册中心,例如nacos -->
    <dubbo:registry address="zookeeper://49.234.55.50:2181" timeout="10000"/>

    <!--服务发布的协议【dubbo】以及端口【20880】-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 定义一个bean到IOC容器 -->
    <bean id="loginServer" class="com.dubbo.server.demo.LoginServerImpl"/>
    <!-- 暴露的API接口 -->
    <dubbo:service interface="com.dubbo.server.demo.LoginServer" ref="loginServer"/>


</beans>

2.3 Implementing the interface 

package com.dubbo.server.demo;

/**
 * @description: 登录服务
 * @author: tiger
 * @create: 2021-03-07 11:25
 */
public class LoginServerImpl implements LoginServer {
    /**
     * 登陆接口
     *
     * @param password
     * @param name
     * @return
     */
    @Override
    public String login(String password, String name) {

        // 写业务逻辑

        return name + "登录成功!";
    }
}

2.4 Start the service

package com.dubbo.server.demo;

import org.apache.dubbo.container.Main;

/**
 * dubbo服务启动类
 *
 * @description:
 * @author: tiger
 * @create: 2021-03-07 11:52
 */
public class App {

    public static void main(String[] args) {
        Main.main(args);
    }
}

Pay attention to the startup information

url dubbo://169.254.124.214:20880/com.dubbo.server.demo.LoginServer 

3. Create the dubbo-client-demo project, the directory structure is as follows

3.1 pom.xml file

<?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.dubbo.client.demo</groupId>
    <artifactId>dubbo-client-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>dubbo-client-demo</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>

        <!-- api接口契约 -->
        <dependency>
            <groupId>com.dubbo.server.demo</groupId>
            <artifactId>dubbo-server-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- dubbo 依赖包,2.7.8 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.8</version>
        </dependency>

    <!-- zk注册中心依赖的包  -->
    <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-dependencies-zookeeper</artifactId>
      <version>2.7.8</version>
      <type>pom</type>
    </dependency>
    </dependencies>

</project>
3.2 创建文件 src/resources/META-INF/spring/application.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:application name="dubbo-client"/>

    <!-- 不需要注册到服务注册中心 -->
    <dubbo:registry address="N/A"/>

    <!-- 注册中心 -->
    <!--<dubbo:registry address="zookeeper://49.234.55.50:2181" timeout="10000" />-->

    <!-- 引用的登陆服务接口,
    其中的url地址是启动dubbo服务时控制台打印出的地址,
    因为这里是没有指定注册中心,需要这样弄,比较麻烦 -->
    <dubbo:reference id="loginServer"
                     interface="com.dubbo.server.demo.LoginServer"
                     url="dubbo://169.254.124.214:20880/com.dubbo.server.demo.LoginServer"
    />
    <!-- 信息:  [DUBBO] Export dubbo service com.dubbo.server.demo.LoginServer to
    url dubbo://169.254.124.214:20880/com.dubbo.server.demo.LoginServer
    ?anyhost=true&application=dubbo-server&bind.ip=169.254.124.214&bind.port=20880&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=com.dubbo.server.demo.LoginServer&methods=login&pid=3704&release=2.7.8&side=provider&timestamp=1615089263567, dubbo version: 2.7.8, current host: 192.168.240.1-->


</beans>

3.3 Start

package com.dubbo.client.demo;

import com.dubbo.server.demo.LoginServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {

        LoginServer loginServer = null;

        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:META-INF/spring/application.xml");
        loginServer = (LoginServer) context.getBean("loginServer");
        String ret = loginServer.login("123456", "tiger");
        System.out.println(ret);

    }
}

 

Guess you like

Origin blog.csdn.net/qq_36336332/article/details/114481455