5.1.1 导入相应的jar包
使用IDEA创建生产者工程:
生成的pom.xml配置文件:
<?xml version="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.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.marshal.dubbo</groupId>
<artifactId>dubbo-springboot-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-springboot-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.marshal.dubbo.service</groupId>
<artifactId>dubbo-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
将服务接口的jar包也导入到工程中:
5.1.2 服务接口实现类
编写服务接口实现类:
服务接口实现类源码:
package com.marshal.dubbo.dubbospringbootprovider.Service;
import com.alibaba.dubbo.config.annotation.Service;
import com.marshal.dubbo.entity.Student;
import com.marshal.dubbo.service.StudentService;
import java.util.HashMap;
/**
* @Author 王毅
* @Date 2020/9/9 14:26
*/
@Service
public class StudentServiceImpl implements StudentService {
@Override
public Student getStudentById(Long id) {
HashMap<Integer, Student> map = new HashMap<Integer, Student>();
map.put(1, new Student(1L,"唐僧", 1, "2020-09-09", 178D, 87D));
map.put(1, new Student(2L,"孙悟空", 1, "2020-10-09", 138D, 37D));
map.put(1, new Student(3L,"猪八戒", 1, "2020-11-09", 198D, 187D));
map.put(1, new Student(4L,"沙僧", 1, "2020-12-09", 278D, 187D));
return (Student) map.get(id);
}
}
5.1.3 编写“application.properties”配置
# User Config
server.port=8090
logging.level.org.springframework=INFO
## Dubbo
spring.dubbo.application.name=dubbo-springboot-provider
spring.dubbo.registry.address=zookeeper://192.168.136.129:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20886
spring.dubbo.scan=com.marshal.dubbo.dubbospringbootprovider.Service