@Data
public class Student implements Serializable {
private Integer id;
private String name;
private String sex;
private Integer age;
}
public interface StudentService {
//通过id查询学生
Student queryStudent(Integer id);
}
添加dubbo、注册中心、接口工程依赖
<!--接口工程-->
<dependency>
<groupId>com.why</groupId>
<artifactId>ch04-springboot-dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--Dubbo依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--注册中心依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
在application.properties文件中编写
#内嵌tomcat配置
server.port=8081
server.servlet.context-path=/
#dubbo配置
#服务提供者名称
spring.application.name=ch04-dubbo-provide
#声明当前工程是服务提供者
spring.dubbo.server=true
#注册中心 默认端口号2181
spring.dubbo.registry=zookeeper://192.168.140.129:2181
//把接口的实现类(服务)交给springboot容器管理
@Component
//@Service选择alibaba提供的那个 interfaceName接口的全限定名 也可以使用interfaceClass
//@Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000)
//暴露接口服务
@Service(interfaceName = "com.why.service.StudentService",version = "1.0.0",timeout = 15000)
public class StudentServiceImpl implements StudentService {
@Override
public Student queryStudent(Integer id) {
//这里只是通过创建对象来模拟dao层访问数据库返回的结果
//偷个小懒
Student student = new Student();
student.setName("张三");
student.setSex("男");
student.setAge(21);
return student;
}
}
在引导类上添加@EnableDubboConfiguration注解
@SpringBootApplication
@EnableDubboConfiguration//开启dubbo配置
public class Ch04SpringbootDubboProvideApplication {
public static void main(String[] args) {
SpringApplication.run(Ch04SpringbootDubboProvideApplication.class, args);
}
}
添加dubbo、注册中心、接口工程依赖
<!--接口工程-->
<dependency>
<groupId>com.why</groupId>
<artifactId>ch04-springboot-dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--Dubbo依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--注册中心依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
#内嵌tomcat端口号
server.port=8080
#上下文根
server.servlet.context-path=/
#duoob配置
#服务消费者名称
spring.application.name=ch04-springboot-dubbo-consumer
#注册中心
spring.dubbo.registry=zookeeper://192.168.140.129:2181
@Controller
public class MyController {
//引用服务提供者提供的服务
@Reference(interfaceName = "com.why.service.StudentService",version = "1.0.0",check = false)
StudentService studentService;
@RequestMapping("/query")
public @ResponseBody Object query(){
Student student = studentService.queryStudent(1);
return student.toString();
}
}
在引导类上添加@EnableDubboConfiguration注解
@SpringBootApplication
@EnableDubboConfiguration//开启dubbo配置
public class Ch04SpringbootDubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(Ch04SpringbootDubboConsumerApplication.class, args);
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/m0_60117382/article/details/121558324
内容来源于网络,如有侵权,请联系作者删除!