安装Zookeeper

https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.8.1/apache-zookeeper-3.8.1-bin.tar.gz

下载解压后需要先配置zoo.cfg,可以用conf/zoo_sample.cfg作为样本来进行配置,有两个关键参数:

dataDir=D:/apache-zookeeper-3.8.1-bin/data

admin.serverPort=8888(默认为8080)

然后运行zkServer即可(需要配置好java的环境变量)

安装Dubbo-admin

https://github.com/apache/dubbo-admin

到这里下载release发行版本

解压后运行startup即可

运行后可以访问http://localhost:8080/登录后台查看微服务的运行情况:

默认账号密码都为root,修改账号密码只需要到conf中修改即可。

在idea中创建微服务项目

一般需要至少创建三个工程,如下图所示:

先创建一个父工程(示例:sway-doctor),为普通的maven项目即可

引入dubbo和zookeeper依赖,这样就不需要子项目重复引用了。

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--springboot版本-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.11</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!--当前工程的版本信息-->
    <groupId>cn.com.sway.doctor</groupId>
    <artifactId>sway-doctor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sway-doctor</name>
    <description>sway-doctor</description>

    <!--设置父工程的包为pom-->
    <packaging>pom</packaging>

    <!--引入子模块-->
    <modules>
        <module>service-common</module>
        <module>service-disease</module>
        <module>service-main</module>
    </modules>

    <!--版本控制-->
    <properties>
        <java.version>1.8</java.version>
        <dubbo.version>3.2.0</dubbo.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-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 引入spring-boot-starter以及dubbo和curator的依赖 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!-- 整合dubbo与zookeeper -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
        </dependency>

        <!--引入公共工程-->
        <dependency>
            <groupId>cn.com.sway.doctor</groupId>
            <artifactId>service-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>

</project>

再创建公共工程,一般命名为common(示例:service-common),为普通的maven项目即可

主要存放公共的实体类以及接口类。

同时在父工程的pom文件中引入该依赖。

Disease代码如下:

package cn.com.sway.doctor.service.common.model;

import lombok.Data;

import java.io.Serializable;

@Data
public class Disease implements Serializable {

    private String id;

    private String name;

}

DiseaseService代码如下:

package cn.com.sway.doctor.service.common.service;

import cn.com.sway.doctor.service.common.model.Disease;

import java.util.List;

public interface DiseaseService {

    List<Disease> getList();

}

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>sway-doctor</artifactId>
        <groupId>cn.com.sway.doctor</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-common</artifactId>

</project>

接着创建微服务SpringBoot工程,一般为provider或者微服务的名称

实现公共工程的service接口,并配置dubbo和zookeeper。

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>
        <!--原来的独立springboot工程配置需要注释掉-->
        <!--<groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.11</version>
        <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->

        <!--引入父工程的pom信息-->
        <artifactId>sway-doctor</artifactId>
        <groupId>cn.com.sway.doctor</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>cn.com.sway.doctor</groupId>
    <artifactId>service-disease</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!--设置打包方式-->
    <packaging>jar</packaging>

    <name>service-disease</name>
    <description>service-disease</description>
    <properties>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- REST support dependencies -->
        <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-client -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-client</artifactId>
            <version>4.7.7.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-netty4 -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-netty4</artifactId>
            <version>4.7.7.Final</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

DiseaseServiceImpl的代码如下:

package cn.com.sway.doctor.service.disease.impl;

import cn.com.sway.doctor.service.common.model.Disease;
import cn.com.sway.doctor.service.common.service.DiseaseService;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

@DubboService(protocol = {"dubbo", "rest"})
@Path("/diseases")
public class DiseaseServiceImpl implements DiseaseService {

    @GET
    @Path("")
    @Produces({MediaType.APPLICATION_JSON})
    @Consumes({MediaType.APPLICATION_JSON})
    @Override
    public List<Disease> getList() {

        List<Disease> list = new ArrayList<Disease>();

        Disease ganmao = new Disease();
        ganmao.setId("1");
        ganmao.setName("感冒");
        list.add(ganmao);

        Disease kesou = new Disease();
        kesou.setId("2");
        kesou.setName("咳嗽");
        list.add(kesou);

        Disease fashao = new Disease();
        fashao.setId("3");
        fashao.setName("发烧");
        list.add(fashao);

        return list;
    }
}

application.yaml文件配置如下:

# -------------------- Dubbo 配置 BEGIN --------------------
dubbo:
  registry: # 单注册中心,注意属性级别:dubbo.registry,注意多注册中心格式:@Service(registry = {"registry1","registry2","registry3"})
    register: true # true(默认), false:表示服务不注册到注册中心(只订阅,直连服务正常),且dubbo-admin不显示
    address: zookeeper://127.0.0.1:2181
#  registries: # 多注册中心,注意属性级别:dubbo.registries, 不同的方法可以用@Service(registry = {"registry2"}),指定不同的注册中心
#    provider1:
#      register: false #默认true, false:表示服务不注册到注册中心
#      address: zookeeper://55.55.55.82:2181
#    provider2:
#      register: false
#      address: zookeeper://55.55.55.196:2181
#    provider3:
#      register: false
#      address: zookeeper://55.55.55.139:2181
  application:
    name: sway-doctor-service-disease
  #    qos-enable: true # 默认值 true, 是否允许在线运维, 配置和查询服务
  #    qos-port: 22222 # 默认值2222
  #    qos-accept-foreign-ip: false # 默认false , 不允许远程访问 telnet
  scan:
    base-packages: cn.com.sway.doctor.service.disease.serivce
  protocols:
    dubbo:
      name: dubbo
      port: 28080
    rest:
      name: rest
      port: 8081
      server: netty
  provider:
    retries: 0        # 服务提供者无需重试
    timeout: 6000     # 默认只有1s
# -------------------- Dubbo 配置 END --------------------

最后还需要在SpringBoot的启动类中增加@EnableDubbo注解,启动后可以在Dubbo中看见对应的服务:

最后创建消费者,一般为consumer或main

攥写RestController,以便让外部调用。

DiseaseApiControllerV1的代码如下:

package cn.com.sway.doctor.service.main.controller.api.v1;

import cn.com.sway.doctor.service.common.service.DiseaseService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/diseases")
public class DiseaseApiControllerV1 {

    @Reference
    private DiseaseService diseasetService;

    @GetMapping("")
    public Object student(){
        return this.diseasetService.getList();
    }

}

配置文件application.properties的代码如下:

server.port=8000  

dubbo.application.name=sway-doctor-service-main
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry

启动后查看dubbo-admin后可以看到对应的服务消费者信息:

测试服务调用的结果,访问http://localhost:8000/api/v1/diseases,输出结果如下图所示即为成功: