dubbo-api 接口定义
StudentDto返回数据实体
package com.frank.dto;
import java.io.Serializable;
import java.util.Arrays;
public class StudentDto implements Serializable {
private static final long serialVersionUID = -3811325927474026428L;
private String name;
private String grade;
private String[] subjects;
public StudentDto(String name, String grade, String[] subjects) {
this.name = name;
this.grade = grade;
this.subjects = subjects;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String[] getSubjects() {
return subjects;
}
public void setSubjects(String[] subjects) {
this.subjects = subjects;
}
@Override
public String toString() {
return "StudentDto{" +
"name='" + name + '\'' +
", grade='" + grade + '\'' +
", subjects=" + Arrays.toString(subjects) +
'}';
}
}
StudentService接口类
package com.frank.service;
import com.frank.dto.StudentDto;
import java.util.List;
public interface StudentService {
List<StudentDto> findAll();
}
pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.frank</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-api</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.9</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</project>
dubbo-provider 提供者实现
StudentServiceImpl实现类
package com.frank.service.impl;
import com.frank.dto.StudentDto;
import com.frank.service.StudentService;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
public List<StudentDto> findAll() {
List<StudentDto> studentDtos=new ArrayList<StudentDto>();
studentDtos.add(new StudentDto("Frank","一年级",new String[]{"语文","数学"}));
studentDtos.add(new StudentDto("Danny","六年级",new String[]{"语文","英语","数学","地理"}));
return studentDtos;
}
}
Provider测试启动类
package com.frank;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
context.start();
System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
}
}
provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 具体的实现bean -->
<bean id="studentService" class="com.frank.service.impl.StudentServiceImpl"/>
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo-provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.frank.service.StudentService" ref="studentService" />
</beans>
pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.frank</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.frank</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
启动成功后可在管理控制台看到提供者信息
image.png
dubbo-custom 消费者
Consumer 测试启动类
package com.frank;
import com.frank.dto.StudentDto;
import com.frank.service.StudentService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("custom.xml");
context.start();
StudentService studentService = (StudentService) context.getBean("studentService");
List<StudentDto> list = studentService.findAll();
for (StudentDto student:list) {
System.out.println(student.toString());
}
System.in.read();
}
}
custom.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo-custom" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:reference interface="com.frank.service.StudentService" id="studentService" />
</beans>
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.frank</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-custom</artifactId>
<dependencies>
<dependency>
<groupId>com.frank</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</project>
执行结果
StudentDto{name='Frank', grade='一年级', subjects=[语文, 数学]}
StudentDto{name='Danny', grade='六年级', subjects=[语文, 英语, 数学, 地理]}
网友评论