第一个dubbo项目

创建四个项目

下载项目地址:
https://download.csdn.net/download/han_xiaoxue/10957427
在这里插入图片描述

消费者项目

在这里插入图片描述

dubbo-consumer.xml

在这里插入图片描述

生产者项目

在这里插入图片描述

dubbo-provider.xml

在这里插入图片描述

DubboProvider

public class DubboProvider {
	
	private static final Log log = LogFactory.getLog(DubboProvider.class);

	public static void main(String[] args) {
		try {
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");
			context.start();
		} catch (Exception e) {
			log.error("== DubboProvider context start error:",e);
		}
		synchronized (DubboProvider.class) {
			while (true) {
				try {
					DubboProvider.class.wait();
				} catch (InterruptedException e) {
					log.error("== synchronized error:",e);
				}
			}
		}
	}
    
}

运行

启动zookeper

https://blog.csdn.net/han_xiaoxue/article/details/86747509
1、运行 DubboProvider 里的main方法,启动生产者项目
2、tomcat中启动 消费者项目
在这里插入图片描述
3、访问结果
用户名输入为空的时候,点击登录 会调用服务接口 查询出某表的条数。显示到下方。


成功!


在这里插入图片描述

以下是dubbo和Zookeeper的安装和第一个程序的详细步骤: ## 安装Zookeeper 1. 下载Zookeeper,可以从官网上下载最新版本的Zookeeper。 2. 解压下载的文件,将解压后的文件夹复制到你想要安装的位置。 3. 进入Zookeeper的conf目录,将zoo_sample.cfg文件复制一份并重命名为zoo.cfg。 4. 打开zoo.cfg文件,修改dataDir属性的值,将其设置为一个非临时目录,例如:/opt/zookeeper/data。 5. 启动Zookeeper,进入bin目录,执行以下命令: ``` ./zkServer.sh start ``` 6. 命令执行成功后,可以通过以下命令检查Zookeeper的运行状态: ``` ./zkServer.sh status ``` 如果输出“Mode: standalone”,则表示Zookeeper已经成功启动。 ## 安装Dubbo 1. 下载Dubbo,可以从官网上下载最新版本的Dubbo。 2. 解压下载的文件,将解压后的文件夹复制到你想要安装的位置。 3. 将Dubbo的jar包添加到你的项目中。 4. 在你的项目中添加Dubbo的配置文件,例如:dubbo.properties。 5. 在dubbo.properties配置文件中配置Zookeeper的地址和Dubbo的应用名称,例如: ``` dubbo.application.name=example-provider dubbo.registry.address=zookeeper://127.0.0.1:2181 ``` 6. 编写Dubbo服务提供者代码,例如: ``` public class ExampleServiceImpl implements ExampleService { public String sayHello(String name) { return "Hello, " + name; } } ``` 7. 在服务提供者代码中使用Dubbo的@Service注解标记服务实现类,例如: ``` @Service public class ExampleServiceImpl implements ExampleService { public String sayHello(String name) { return "Hello, " + name; } } ``` 8. 启动Dubbo服务提供者,运行你的项目即可。 ## 测试Dubbo服务 1. 编写Dubbo服务消费者代码,例如: ``` public class ExampleConsumer { public static void main(String[] args) { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("example-consumer"); RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("zookeeper://127.0.0.1:2181"); ReferenceConfig<ExampleService> referenceConfig = new ReferenceConfig<>(); referenceConfig.setApplication(applicationConfig); referenceConfig.setRegistry(registryConfig); referenceConfig.setInterface(ExampleService.class); ExampleService exampleService = referenceConfig.get(); String result = exampleService.sayHello("World"); System.out.println(result); } } ``` 2. 启动Dubbo服务消费者,运行你的项目即可。 以上就是dubbo和Zookeeper的安装和第一个程序的详细步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值