dubbo 发布 RPC 服务

Dubbo 发布 RPC 服务

建立服务提供者项目

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.bjsxt</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!-- 添加dubbo的依赖 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.6</version>
        </dependency>
        <!-- zk的依赖 -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>

</project>

 

application-dubbo.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:context="http://www.springframework.org/schema/context"
       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.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    ">
    <!--扫描业务对象-->
    <context:component-scan base-package="com.bjsxt.service.impl"></context:component-scan>

    <!--通过xml配置发布dubbo服务-->
    <dubbo:application name="dubbo-prodvider"></dubbo:application>

    <!--使用ZooKeeper注册中心暴露服务地址-->
    <dubbo:registry protocol="zookeeper" address="192.168.181.129:2181,192.168.181.129:2182,192.168.181.129:2183,"/>

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20882" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.bjsxt.service.UserService" ref="userServiceImpl" />
</beans>

 

建立业务接口

com.bjsxt.service.UserService

package com.bjsxt.service;

public interface UserService {

    public String loadUserService(int id);
}

 

建立实现类

com.bjsxt.service.impl.UserServiceImpl

package com.bjsxt.service.impl;

import com.bjsxt.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public String loadUserService(int id) {
        return "20882===========加载的用户id为"+id;
    }
}

 

创建启动类实现服务发布

启动类,连续启动两次,每次启动前修改暴露的服务的端口号,实现服务的集群发布

com.bjsxt.app.ProviderApp

package com.bjsxt.app;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ProviderApp {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application-dubbo.xml");
        context.start();
        System.in.read(); // 按任意键退出
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-12-11 08:56  sakura-yxf  阅读(190)  评论(0)    收藏  举报