首页 > 其他分享 >Dubbo学习(二)- Dubbo的hello world

Dubbo学习(二)- Dubbo的hello world

时间:2022-10-11 17:09:20浏览次数:48  
标签:Dubbo java dubbo http maven world org com hello


项目说明

Dubbo-Demo
dubbo的消费者,负责调用服务

Dubbo-Hello-Api
标准api接口,避免服务提供者和消费者书写重复的代码

Dubbo-Hello-Service
dubbo的服务者,提供服务共其他应用调用

Dubbo-Hello-Api

myeclipse中,右键》new》other》maven project,如下

Dubbo学习(二)- Dubbo的hello world_spring


Dubbo学习(二)- Dubbo的hello world_maven_02


修改pom.xml

<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.ahut</groupId>
<artifactId>Dubbo-Hello-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>

<!-- 添加默认的全局,不然会打包失败 -->
<defaultGoal>compile</defaultGoal>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

创建com.ahut.hello.service包,添加如下接口

package com.ahut.hello.service;

/**
*
* @ClassName: HelloService
* @Description: Hello业务逻辑接口
* @author cheng
* @date
public interface HelloService

/**
*
* @Title: sayHello
* @Description:
* @param
void

发布项目到maven本地仓库

选中项目》右键,run as》run configuration》maven build

Dubbo学习(二)- Dubbo的hello world_dubbo_03

Dubbo-Hello-Service

创建Dubbo-Hello-Service项目,和Dubbo-Hello-Api一样,参考上面
修改pom.xml

<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.ahut</groupId>
<artifactId>Dubbo-Hello-Service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<!-- 引入Api -->
<dependency>
<groupId>com.ahut</groupId>
<artifactId>Dubbo-Hello-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>

<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>

<!-- 缺少的jar -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

创建com.ahut.hello.serviceImpl包,添加如下实现

package com.ahut.hello.serviceImpl;

import com.ahut.hello.service.HelloService;

/**
*
* @ClassName: HelloServiceImpl
* @Description: Hello业务逻辑实现
* @author cheng
* @date
public class HelloServiceImpl implements HelloService

/**
* 实现
*/
@Override
public void sayHello(String name) {
System.out.println("Hello "

添加spring-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="dubbo-hello"

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

<dubbo:protocol name="dubbo" port="20880"

<!-- 和本地bean一样实现服务 -->
<bean id="helloService" class="com.ahut.hello.serviceImpl.HelloServiceImpl"

<!-- 向注册中心注册暴漏服务地址,注册服务 -->
<dubbo:service interface="com.ahut.hello.service.HelloService"
ref="helloService" executes="10"

</beans>

模拟启动服务,前提要开启zookeeper

package com.ahut;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*
* @ClassName: ServiceMain
* @Description:
* @author cheng
* @date
public class ServiceMain

public static void main(String[] args) throws IOException {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "spring-dubbo.xml" });
context.start();

System.out.println("输入任意按键退出 ~ ");
System.in.read();
context.close();
}

}

Dubbo-Demo

创建Dubbo-Demo项目,和Dubbo-Hello-Api一样,参考上面
修改pom.xml

<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.ahut</groupId>
<artifactId>Dubbo-Demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<!-- 引入Api -->
<dependency>
<groupId>com.ahut</groupId>
<artifactId>Dubbo-Hello-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>

<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>

<!-- 缺少的jar -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

添加spring-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="dubbo-demo"

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

<!-- 获取bean -->
<dubbo:reference id="helloService"
interface="com.ahut.hello.service.HelloService"

</beans>

模拟调用服务

package com.ahut;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ahut.hello.service.HelloService;

/**
*
* @ClassName: DemoMain
* @Description:
* @author cheng
* @date
public class DemoMain

public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "spring-dubbo.xml" });
context.start();
HelloService helloService = (HelloService) context.getBean("helloService");
helloService.sayHello("world");

context.close();
}

}

可能出现的问题

Dubbo-Hello-Api项目打包失败,出现如下错误

[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.ahut:Dubbo-Hello-Api:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 10, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.096 s
[INFO] Finished at: 2018-04-08T14:32:14+08:00
[INFO] Final Memory: 6M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifiedException

解决办法:修改pom.xml文件,添加默认全局

<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.ahut</groupId>
<artifactId>Dubbo-Hello-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>

<!-- 添加默认的全局,不然会打包失败 -->
<defaultGoal>compile</defaultGoal>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Dubbo-Hello-Service或者Dubbo-Demo模拟启动失败,如下

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy
at com.alibaba.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter.connect(CuratorZookeeperTransporter.java:26)
at com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter$Adaptive.connect(ZookeeperTransporter$Adaptive.java)
at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry.<init>(ZookeeperRegistry.java:69)
at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistryFactory.createRegistry(ZookeeperRegistryFactory.java:37)
at com.alibaba.dubbo.registry.support.AbstractRegistryFactory.getRegistry(AbstractRegistryFactory.java:95)
at com.alibaba.dubbo.registry.RegistryFactory$Adaptive.getRegistry(RegistryFactory$Adaptive.java)
at com.alibaba.dubbo.registry.integration.RegistryProtocol.getRegistry(RegistryProtocol.java:200)
at com.alibaba.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:134)
at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:54)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.qos.protocol.QosProtocolWrapper.export(QosProtocolWrapper.java:54)
at com.alibaba.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java)
at com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:505)
at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:357)
at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:316)
at com.alibaba.dubbo.config.ServiceConfig.export(ServiceConfig.java:215)
at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:121)
at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:50)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at com.ahut.ServiceMain.main(ServiceMain.java:18)
Caused by: java.lang.ClassNotFoundException: org.apache.curator.RetryPolicy
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 27

解决办法:修改pom.xml,引入缺少的jar

<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.ahut</groupId>
<artifactId>Dubbo-Hello-Service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<!-- 引入Api -->
<dependency>
<groupId>com.ahut</groupId>
<artifactId>Dubbo-Hello-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>

<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>

<!-- 缺少的jar -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>


标签:Dubbo,java,dubbo,http,maven,world,org,com,hello
From: https://blog.51cto.com/u_15824687/5747136

相关文章

  • Hello,World!
    JDK,JRE,JVMJDK:JavaDevelopmentKit(Java所有的东西)JRE:JavaRuntimeEnvironment(Java运行的环境)JVM:JavaVitrualMachine(Java虚拟机)卸载JDK1.删除Java......
  • Java 我的第一个hello word
         项目—右键—选中添加框架的支持 选中“WebApplication”,然后确定  右键src,新建一个类          ......
  • Hello,World
      HelloWorld新建一个文件存放代码新建一个java文件文件后缀名为.javaHello.java[注意点]系统可能没有显示后缀名,需要手动打开编写代码public......
  • HMdubbo4【dubbo快速入门】
    框架提供者和消费者理论上是两个项目,此处用同一项目中两个模块进行模拟实现步骤①创建服务提供者Provider模块②创建服务消费者Consumer模块③在服务提供者模块......
  • Sanic学习之helloworld
    #text文本输出fromsanicimportSanicfromsanic.responseimporttextapp=Sanic(name="App")@app.route("/hello")asyncdefhello(request):returntex......
  • Java学习之路:HelloWorld
    2022-10-0816:13:57HelloWorld  1.随便新建一个文件夹,存放代码  2.新建一个Java文件文件后缀名为.javahello.java注意:系统没有显示后缀名时,需要手动打开......
  • 从 HelloWorld 看 Java 字节码文件结构
    很多时候,我们都是从代码层面去学习如何编程,却很少去看看一个个Java代码背后到底是什么。今天就让我们从一个最简单的HelloWorld开始看一看Java的类文件结构。在开始......
  • 3.Rust的“Hello World”
    在部署好Rust的编译程序之后,就可以编写Rust的第一个程序了。按照惯例,都是“HelloWorld”。在Rust的官方书的1.2节就是“HelloWorld”。在这个章节中,首先创建了一个目录来......
  • bcc之hello world
    bcc代码——Hello,world1、简单监控clone()系统调用,将相关的信息打印出来#!/usr/bin/pythonfrombccimportBPFBPF(text="""intkprobe__sys_clone(void*ctx){ bpf......
  • 第一个Java程序hello world
    day4:我的第一个JAVA程序HelloWorldpublicclassMain{publicstaticvoidmain(String[]args){System.out.println("hello,world");}}注意事项......