一、创建一个公共接口项目
1.选择普通的maven创建项目
项目结构
2.pom.xml文件
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <groupId>com.itgroupId>
- <artifactId>022-interface-apiartifactId>
- <version>1.0.0version>
-
-
- project>
3.Student类(需要实现序列化)
- package com.it.entity;
-
- import java.io.Serializable;
-
- public class Student implements Serializable {
-
- private static final long serialVersionUID = -4654501682750659489L;
-
- private Integer id;
- private String name;
- private Integer age;
-
- @Override
- public String toString() {
- return "Student{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
- }
4.service包下的StudentService接口
- package com.it.service;
-
- import com.it.entity.Student;
-
- public interface StudentService {
-
- //根据id查找学生
- Student queryStudentById(Integer id);
-
- }
二、创建服务提供者
1.新建springboot项目,不需要添加任何依赖。
项目结构
2.手动导入dubbo依赖,导入zookeeper依赖时需要排除导入SLF4j依赖,以避免重复导入SLF4j依赖导致报错。
pom.xml文件
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.3version>
- <relativePath/>
- parent>
- <groupId>com.itgroupId>
- <artifactId>023-service-providerartifactId>
- <version>1.0.0version>
-
-
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
-
-
- <dependency>
- <groupId>com.itgroupId>
- <artifactId>022-interface-apiartifactId>
- <version>1.0.0version>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.dubbogroupId>
- <artifactId>dubbo-spring-boot-starterartifactId>
- <version>2.7.8version>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.dubbogroupId>
- <artifactId>dubbo-dependencies-zookeeperartifactId>
- <version>2.7.8version>
- <type>pomtype>
- <exclusions>
- <exclusion>
- <artifactId>slf4j-log4j12artifactId>
- <groupId>org.slf4jgroupId>
- exclusion>
- exclusions>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
-
- project>
3.service包下的StudentServiceImpl实现类
- package com.it.service.impl;
-
- import com.it.entity.Student;
- import com.it.service.StudentService;
- import org.apache.dubbo.config.annotation.DubboService;
- import org.springframework.stereotype.Component;
-
- /**
- * 使用dubbo中的注解暴露服务
- */
- @Component
- @DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 5000)
- public class StudentServiceImpl implements StudentService {
- @Override
- public Student queryStudentById(Integer id) {
- Student student=new Student();
- if (1001==id){
- student.setId(1001);
- student.setName("小红");
- student.setAge(24);
- }else if (1002==id){
- student.setId(1002);
- student.setName("小黄");
- student.setAge(24);
- }else if (1003==id){
- student.setId(1003);
- student.setName("小蓝");
- student.setAge(21);
- }
-
- return student;
- }
- }
4.springboot的核心配置文件application.properties
- #配置服务名称:等同于dubbo:application name="名称"
- spring.application.name=studentservice-provider
-
- #配置扫描的包,扫描的@DubboService
- dubbo.scan.base-packages=com.it.service
-
- #配置dubbo协议
- #dubbo.protocol.name=name
- #dubbo.protocol.port=20881
-
- #注册中心
- dubbo.registry.address=zookeeper://localhost:2181
5.主函数入口类ProviderApplication
- package com.it;
-
- import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- /**
- * @EnableDubbo: 启动dubbo
- */
- @SpringBootApplication
- @EnableDubbo
- public class ProviderApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(ProviderApplication.class, args);
- }
-
- }
三、创建消费者
1.新建springboot项目,选择添加web起步依赖。
项目结构
2.手动导入dubbo依赖,导入zookeeper依赖时需要排除导入SLF4j依赖,以避免重复导入SLF4j依赖导致报错。
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.3version>
- <relativePath/>
- parent>
- <groupId>com.itgroupId>
- <artifactId>024-consumerartifactId>
- <version>1.0.0version>
-
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
-
-
- <dependency>
- <groupId>com.itgroupId>
- <artifactId>022-interface-apiartifactId>
- <version>1.0.0version>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.dubbogroupId>
- <artifactId>dubbo-spring-boot-starterartifactId>
- <version>2.7.8version>
- dependency>
-
-
- <dependency>
- <groupId>org.apache.dubbogroupId>
- <artifactId>dubbo-dependencies-zookeeperartifactId>
- <version>2.7.8version>
- <type>pomtype>
- <exclusions>
- <exclusion>
- <artifactId>slf4j-log4j12artifactId>
- <groupId>org.slf4jgroupId>
- exclusion>
- exclusions>
- dependency>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
-
- project>
3.DubboController类
- package com.it.controller;
-
- import com.it.entity.Student;
- import com.it.service.StudentService;
- import org.apache.dubbo.config.annotation.DubboReference;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import javax.annotation.Resource;
-
- @RestController
- public class DubboController {
-
-
- /**
- * @DubboReference:引用远程接口,把创建好的代理对象,注入给StudentService
- */
- @DubboReference(interfaceClass = StudentService.class,version = "1.0")
- private StudentService studentService;
-
- @GetMapping("/query")
- public String queryStudentById(Integer id){
- Student student = studentService.queryStudentById(1002);
- return "调用远程接口来获取对象:"+student;
- }
-
- }
4.application.properties文件
- #指定服务名称
- spring.application.name=consumer-application
- #指定注册中心
- dubbo.registry.address=zookeeper://localhost:2181
5.ConsumerApplication类
- package com.it;
-
- import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- @EnableDubbo
- public class ConsumerApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(ConsumerApplication.class, args);
- }
-
- }
四、启动zookeeper
2.执行程序后,不能关闭程序,可以把该命令窗口最小化,才能保证 zookeeper是启动状态
3.启动服务提供者
4.启动消费者
5.在浏览器地址框中输入url进行访问