新建项目
新建一个Maven项目名称为DubboDemo,在该项目下新建一个子模块项目名称为Provider
创建java和resources文件夹
引入依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.46</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.0.35.Final</version> </dependency>
|
Java代码
服务提供者接口DemoProviderService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package service;
public interface DemoProviderService {
String sayHello(String name); }
|
服务提供者接口实现类DemoProviderServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package service.impl;
import service.DemoProviderService;
public class DemoProviderServiceImpl implements DemoProviderService {
@Override public String sayHello(String name) { return "服务员(端口:20880)" + name; } }
|
新建配置文件
在resources文件夹中新建,命名为dubbo-demo-provider.xml,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="demo-provider"/> <dubbo:registry address="zookeeper://localhost:2181"/> <dubbo:protocol name="dubbo" port="20880"/> <bean id="demoProviderService" class="service.impl.DemoProviderServiceImpl"/> <dubbo:service interface="service.DemoProviderService" ref="demoProviderService"/> </beans>
|
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class ProviderTest {
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-demo-provider.xml"); context.start(); System.out.println("服务提供者注册成功(端口:20880)"); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } context.close(); } }
|
运行结果
在Dubbo Admin控制台中查看服务,如何使用Dubbo Admin控制台查看博客:使用Dubbo Admin控制台