创建Maven项目:ch14-interface-api
<groupId>com.suyvgroupId>
<artifactId>ch14-interface-apiartifactId>
<version>1.0.0version>
package com.suyv.model;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 1318357199475675242L;
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;
}
}
package com.suyv.service;
import com.suyv.model.Student;
public interface StudentService {
Student queryStudent(Integer id);
}
创建SpringBoot项目:ch15-service-provider
<dependencies>
<dependency>
<groupId>com.suyvgroupId>
<artifactId>ch14-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>
#服务名称
spring.application.name=studentservice-provider
#zookeeper 注册中心
dubbo.registry.address=zookeeper://localhost:2181
#dubbo 注解所在的包名
dubbo.scan.base-packages=com.suyv.service
# 配置Dubbo协议
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
package com.suyv.service.impl;
import com.suyv.model.Student;
import com.suyv.service.StudentService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
/**
* 使用Dubbo中的注解暴露服务
*/
@Component
@DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 500)
public class StudentServiceImpl implements StudentService {
@Override
public Student queryStudent(Integer id) {
Student student = new Student();
if( 1001 == id){
student.setId(1001);
student.setName("1001-张三");
student.setAge(20);
}else if( 1002 == id){
student.setId(1002);
student.setName("1002-李四");
student.setAge(25);
}
return student;
}
}
package com.suyv;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启用Dubbo
*/
@SpringBootApplication
@EnableDubbo
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
创建SpringBoot项目:ch16-service-consumer
<dependencies>
<dependency>
<groupId>com.suyvgroupId>
<artifactId>ch14-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-starter-webartifactId>
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>
#服务名称
spring.application.name=studentservice-consumer
#zookeeper注册中心
dubbo.registry.address=zookeeper://localhost:2181
package cm.suyv.controller;
import com.suyv.model.Student;
import com.suyv.service.StudentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DubboController {
@DubboReference(interfaceClass = StudentService.class,version = "1.0")
private StudentService studentService;
@GetMapping("/query")
public String queryStudent(){
Student student = studentService.queryStudent(1001);
return "调用远程接口:获取对象:" + student;
}
}
package cm.suyv;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}