使用SpringBoot+Dubbo搭建微服务笔记( 二 )

公共实体-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; }}


推荐阅读