通用技术 Dubbo 构建服务

Yan · September 03, 2021 · 1245 hits

Dubbo 构建服务
一、 基于 maven-spring 框架
1、 pom 依赖主要有:
(1)spring-context

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>x.x.x</version>
        </dependency>

(2)dubbo

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>x.x.x</version>
        </dependency>

注:额外可能用到:
(1)spring-webmvc 或 spring-web,curator-framework,zkclient,zookeeper,log4j 等等
2、 pom 插件主要有(测试用):
(1)tomcat7-maven-plugin

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
  <configuration>
    <port>9001</port>
    <path>/</path>
  </configuration>
</plugin>

3、 创建服务接口:(Provider 与 Consumer 所需服务接口)
InterfaceService.java

public interface InterfaceService {
            String sayHello(String name);
}

4、 Provider 实现接口:(服务提供者进行实现类)
ProviderServiceImpl.java

public class ProviderServiceImpl implements InterfaceService {
            public String sayHello(String name) {
                return "Hello " + name;
            }
}

5、 Provider 进行 Spring 配置来注册服务(资源中创建 Spring Config 文件)
provider.xml
(1)需要添加 Xmlns 属性:

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

(2)Xsi 属性中需要添加 schema 资源:

http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd

(3)正文需要添加:

    <!-- 提供方应用名 -->
    <dubbo:application name="provider"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper:// 127.0.0.1:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.home.yan. InterfaceService" ref="interfaceService"/>
    <!-- 和本地bean一样实现服务 -->
<bean id=" interfaceService " class="com.home.yan.ProviderServiceImpl"/>

注:dubbo:service 中 ref 值要于 bean 中 id 值一致
6、 Provider 加载 Spring 配置(web.xml 文件中定义要装入的 Spring 配置)
web.xml

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:provider.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

7、 Consumer 进行 Spring 配置来获取服务(资源中创建 Spring Config 文件)
consumer.xml
(1)需要添加 Xmlns 属性:

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

(2)Xsi 属性中需要添加 schema 资源:

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

(3)正文需要添加:

    <!-- 消费方应用名,不要与提供方一样 -->
    <dubbo:application name="consumer"/>
    <!-- 使用zookeeper注册中心获取暴露服务地址 -->
    <dubbo:registry address="zookeeper:// 127.0.0.1:2181"/>
    <!--从注册中心中查找服务-->
<dubbo:reference id=" interfaceService " interface="com.home.yan. InterfaceService "/>

注:dubbo:reference 中 id 值最好跟提供方 spring 配置文件 dubbo:service 中 ref 值一致(可以不一致)
8、 Consumer 加载 Spring 配置,并调用远程服务:(服务消费者调用提供者服务)
ServiceConsumer.java

public class ServiceConsumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:consumer.xml");
        applicationContext.start();
        InterfaceService interfaceService = (InterfaceService)applicationContext.getBean("interfaceService");
        System.in.read();
    }
}

注:getBean 中值要于 Spring 配置文件中 dubbo:reference 的 id 值一致
9、 运行 Provider 与 Consumer 程序
(1)查看 dubbo-admin 管理控制台
服务提供方与消费方会在控制台中显示

(2)查看 zookeeper 客户端
注册中心 dubbo 下会显示所注册的服务

(3)在具体的服务中可以查看 provider 与 consumer 已连接的地址

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
No Reply at the moment.
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up