1-新建一个空工程
2-在工程中新建一个module,使用spring initializr
取名提供者-买票
选中web
3-新建service包,TicketService接口
加入方法
public String getTicket();
4-新建TicketServiceImpl类,继承TicketService接口
返回一张电影票
package com.example.ticket.service;
public class TicketServiceImpl implements TicketService{
@Override
public String getTicket() {
return "<<厉害了,我的锅>>";
}
}
5-再新建一个module,使用spring initilaizr
选中web模块功能,finish
6-在新的module中新增service包UserService类
7-要在UserService中使用TicketService需要:
(1)将服务提供者注册到注册中心
1))引入dubbo和zkclient相关依赖
访问https://github.com/apache/dubbo,
https://github.com/apache/dubbo-spring-boot-project
在provider-ticket.pom中引入
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
在https://mvnrepository.com/中找到ZooKeeper Client ,引入zookeeper的客户端工具
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
2))配置dubbo的扫描包和注册中心地址
在provider-ticket的application.properties中
dubbo.application.name = provider-ticket
dubbo.registry.address = zookeeper://192.168.3.18:2181
dubbo.scan.base-package =com.example.ticket.service
3))使用@Service发布服务
将服务发布出去:
在provider-ticket模块中修改TicketServiceImpl中添加注解
注意service要使用dubbo的
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.stereotype.Component;
@Component
@Service
@EnableDubbo
即:
package com.example.ticket.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.stereotype.Component;
@Component
@Service
@EnableDubbo
public class TicketServiceImpl implements TicketService{
@Override
public String getTicket() {
return "<<厉害了,我的锅>>";
}
}
(2)
1))在consumer-user的pom.xml中加入
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
2))
在consumer-user的application.properties中加入
dubbo.application.name = consumer-user
dubbo.registry.address = zookeeper://192.168.3.18:2181
3))把同样的TicketService的接口类复制一份放在在consumer-user中,而且要全类名相同
取消这个√
3))UserService中
@Service为spring的
package com.example.user.service;
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.ticket.service.TicketService;
import org.springframework.stereotype.Service;
@Service
public class UserService {
//根据注册中心中的全类名进行匹配
@Reference
TicketService ticketService;
public void hello(){
String ticket = ticketService.getTicket();
System.out.println("买到票了:"+ticket);
}
}
4))consumer的测试类中编写
package com.example.user;
import com.example.user.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ConsumerUserApplicationTests {
@Autowired
UserService userService;
@Test
void contextLoads() {
userService.hello();
}
}
(3)在provider的主程序不关闭的前提下
运行consumer测试类
================================================================================================