jdk8配置dubbo2.5.4

由于喜欢尝鲜,于是在Java9出来以后就马上把jdk换成了jdk1.9,结果最近刚学dubbo的时候一直都配置不成功,于是退回jdk1.8。一下就是配置dubbo的过程

安装Zookeeper

  1. 下载Zookeeper,然后把conf目录下的zoo.sample.cfg 改为 zoo.cfg
  2. 修改zoo.cfg,将dataDir修改为指定目录

    tickTime=2000
    initLimit=10
    syncLimit=5
    dataDir=/home/eros/data/zookeeper
    clientPort=2181
    maxClientCnxns=60

安装tomcat

下载dubbo-admin包

如果是使用jdk8的话,可以在网上下载别人已经修改好的包,jdk1.8-dubbo-admin
下载完成后将war包放在tomcat的webapps目录下

启动Zookeeper服务器

sudo ./zkServer.sh start

启动tomcat

sudo ./startup.sh

访问http://localhost:8080/ 观察是否启动成功

在浏览器输入http://localhost:8080/dubbo-admin-2.5.4/,用户名和密码都是root

在Common模块中定义所有要暴露的接口

public interface IService {
    int calculate(int x,int y);
}

启动Server

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: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">

    <bean id="providerImpl" class="org.eros.dubbo.server.service.ProviderServiceImpl"/>

    <!-- 用dubbo协议在19091端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="19091"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="ProviderService"/>
    <dubbo:service interface="org.eros.dubbo.common.entity.IService" ref="providerImpl" timeout="100000"/>

</beans>

Spring配置类

@Configuration
@ComponentScan
@ImportResource("classpath:dubbo.xml")
@EnableAspectJAutoProxy
public class Configure {
   
}

接口的具体实现

public class ProviderServiceImpl implements IService{
   

    @Override
    public int calculate(int x, int y) {
        return x+y;
    }
}
import java.io.IOException;

public class Main {
    private static final Logger logger= LogManager.getLogger(Main.class);
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Configure.class);
        context.start();
        logger.debug("提供者服务已注册成功!");
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

查看控制台

启动客户端

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: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="consumer"/>
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <dubbo:reference interface="org.eros.dubbo.common.entity.IService" id="providerService" timeout="100000" check="false"/>

</beans>

Spring配置文件

@Configuration
@ComponentScan
@ImportResource("classpath:dubbo.xml")
public class Configure {
   
}
public class Main {
    private static final Logger logger= LogManager.getLogger(Main.class);
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Configure.class);
        context.start();
        IService providerService= (IService) context.getBean("providerService");
        int result=providerService.calculate(3,7);
        logger.debug("result is "+result);
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


查看控制台

附:
Server.pom

<?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">
    <parent>
        <artifactId>DubboDemo</artifactId>
        &
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值