公共实体-People.java
package com.czh.dubbo.common.entity;import java.io.Serializable;public class People implements Serializable { /** **/ private static final long serialVersionUID = 1415852192397895853L; // 人员编号 private int id; // 姓名 private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "People [id=" + id + ", name=" + name + "]"; }}公共服务接口-PeopleService.java
package com.czh.dubbo.common.service;import com.czh.dubbo.common.entity.People;public interface PeopleService { People getPeople(People people);}五、搭建服务提供者服务提供者就是被消费者所调用的,它需要实现公共服务接口,并通过dubbo把自己的服务暴露出来 。
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"> <modelVersion>4.0.0</modelVersion> <groupId>com.czh</groupId> <artifactId>dubbo-provider</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dubbo-provider</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.12.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba.boot/dubbo-spring-boot-project --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>com.czh.dubbo</groupId> <artifactId>dubbo-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>com.alibaba.boot是迁移过后出的一个最新的dubbo spring boot starter,也是目前官方推荐使用的 。值得注意的是,该starter不包含注册中心客户端,需要自己配置 。一般推荐使用zookeeper作为注册中心,所以需要手动引入zkclient 。
application.properties
#web服务端口,由于我们不需要使用web服务,所以将其设为随机端口server.port=-1#dubbo服务端口,我们无需知道dubbo服务运行在哪个端口,故也将其设为随机端口dubbo.protocol.port = -1#dubbo服务名称dubbo.application.name = dubbo-provider#dubbo服务所在包路径dubbo.scan.basePackages = com.czh.dubbo.provider.service#注册中心地址dubbo.registry.address=zookeeper://127.0.0.1:2181#设置服务的日志输出级别为debug级logging.level.com.czh.dubbo.provider.service=debug服务实现类PeopleServiceImpl
package com.czh.dubbo.provider.service;import java.util.Random;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.alibaba.dubbo.config.annotation.Service;import com.czh.dubbo.common.entity.People;import com.czh.dubbo.common.service.PeopleService;//注意,这里的service注解用的不是spring的注解,而是com.alibaba.dubbo.config.annotation.Service;@Servicepublic class PeopleServiceImpl implements PeopleService{ private static final Logger logger = LoggerFactory.getLogger(PeopleServiceImpl.class); /** * 这个方法的作用是接收传过来的People实体,将其ID赋为随机数 */ @Override public People getPeople(People people) { people.setId(new Random().nextInt(10000)); logger.debug("People:{},ID:{}",people.getName(),people.getId()); return people; }}
推荐阅读
- 在JavaScript中使用getter和setter可行吗
- 搭建nginx+tomcat+Java的负载均衡环境
- 公道杯使用技巧
- 2020关于C1驾照的使用新规,赶紧看看
- 使用手机离不开充电器,那充电器及USB接口你了解多少呢?
- EMUI:你真的会使用双卡手机吗?
- 使用苹果手机时,这些需要注意的点,你都知道吗?
- 小米刷机后使用不习惯,想要恢复原系统?可以采用这种方式来恢复
- 骨灰级果粉总结出来的使用iphone小技巧,总有你还不知道的
- 如何判断手机正在使用的充电器是不是原装的?