日期 2022-10-13
选取服务端核心关键部分
main 函数中
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("provider.xml");
context.start();
provider.xml 中 端口8005
<dubbo:registry address="N/A" />
<dubbo:protocol port="8005" name="dubbo" accesslog="true">
<dubbo:service
interface="com.www.service.ProviderService"
ref="ProviderService"
version="1.0.0"
timeout="3000">
</dubbo:service>
<!--Bean bean定义-->
<bean id="ProviderService" class="com.www.service.ProviderServiceImpl"/>
public interface ProviderService {
String SayHello(String word);
}
public class ProviderServiceImpl implements ProviderService{
@Override
public String SayHello(String word) {
System.out.println("server:"+word);
return "server return:"+word;
}
}
pom 中添加
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
启动main 函数就可以了
本文介绍了如何创建一个Dubbo服务端,通过配置provider.xml设置8005端口,注册服务,并提供SayHello接口。使用了dubbo 2.6.6版本,依赖包括netty、curator等。服务实现类ProviderServiceImpl实现了ProviderService接口,提供SayHello方法。只需启动main函数即可运行服务。
3976





