Dubbo项目(相比zookeeper简化代码)

Dubbo项目(相比zookeeper简化代码)

逻辑:

API在定义pojo类型的实体类,定义接口DemoDubboService,以及传入值probder中定义实现类,去实现DemoDubboService接口,重写demo方法,但返回值null,做启动器consumer中定义接口DemoService,传入值空,返回集合,定义实现类,DemoServiceImpl,实现接口DemoService,注入API的接口,调用API的方法,返回list集合,定义类controller调用自己的接口,DemoService。此时返回的实体类就会以json格式传给前端

项目依赖关系,父项目Parent1,

子模块

​ API: service中需要的实体类

​ serviceimpl:provider提供的服务内容

​ consumer:消费者,调用服务内容。

父项目管理子模块

<!--继承父类 spring-boot-->
<packaging>pom</packaging>
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
</parent>
<modules>
        <module>API</module>
        <module>consumer</module>
        <module>provider</module>
</modules>

每个子模块都要去继承父项目

   <parent>
        <artifactId>Parent1</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

consumer管理API,provider去管理API

consumer去管理API

<dependency>
            <groupId>com.evasion</groupId>
            <artifactId>API</artifactId>
            <version>2.0.0.2</version>
</dependency>

consumer,provider去注入dubbo

		<dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
		</dependency>

provider去管理API

<dependency>
            <groupId>com.evasion</groupId>
            <artifactId>API</artifactId>
            <version>2.0.0.2</version>
        </dependency>

pojo中写实体类,id,name,使用lombok实现

<dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
    </dependencies>

父类中的配置文件

<?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>
    <packaging>pom</packaging>
    <!--管理子类模块-->
    <modules>
        <module>API</module>
        <module>consumer</module>
        <module>provider</module>
    </modules>
    <!--继承父类 spring-boot-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
    </parent>
    <groupId>org.example</groupId>
    <artifactId>Parent1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

API的配置文件

<?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>Parent1</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.evasion</groupId>
    <artifactId>API</artifactId>
    <version>2.0.0.2</version>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
        </dependency>
    </dependencies>
</project>

实体类pojo-API

package com.msb.dubbo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class pojo {
 private Integer id;
 private String name;
}

provider的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>Parent1</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.evasion</groupId>
            <artifactId>API</artifactId>
            <version>2.0.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.1.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>
    </dependencies>
</project>

provider中的yml文件

dubbo:
  application:
    name: dubbo-provider
  registry:
    address: zookeeper://192.168.226.128:2181

API中定义接口方法

package com.msb.dubbo.service;

import com.msb.dubbo.pojo;

import java.util.List;

public interface DemoDubboService {
    List<pojo> demo(Integer id, String name);
}

consumer的配置文件

<?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>Parent1</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.evasion</groupId>
            <artifactId>API</artifactId>
            <version>2.0.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>
    </dependencies>
        </project>

provider,提供者的实现类,实现的API的接口,这里返回null

package com.msb.dubbo.service.impl;

import com.msb.dubbo.pojo;
import com.msb.dubbo.service.DemoDubboService;
import org.apache.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.List;

//注解是apache
@Service
public class DemoDubboServiceImpl implements DemoDubboService {
    @Override
    public List<pojo> demo(Integer id, String name) {
        System.out.println("demo方法");
        return null;
    }
}

cunsumer中的接口和实现类,实现API的方法这里返回传入参数

//接口
package com.msb.service;

import com.msb.dubbo.pojo;

import java.util.List;

public interface DemoService {

    List<pojo> consumerDemo();
}

实现类,这里调用API的方法使用 @Reference这里使用dubbo

package com.msb.service.impl;

import com.msb.dubbo.pojo;
import com.msb.dubbo.service.DemoDubboService;
import com.msb.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//注释首先consumer使用springframework注解的service
@Service
public class DemoServiceImpl implements DemoService {
    @Reference//这里使用dubbo
    private DemoDubboService demoDubboService;


    @Override
    public List<pojo> consumerDemo() {
        List<pojo> list = new ArrayList<>();
        list.add(new pojo(1,"张3"));
        list.add(new pojo(2,"张4"));
        list.add(new pojo(3,"张5"));
        return list;

    }
}

consumer中的controller配置

package com.msb.controller;

import com.msb.dubbo.pojo;
import com.msb.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DemoController {
    @Autowired
    private DemoService demoService;
    @RequestMapping("/dome")
    @ResponseBody
    public List<pojo> demo(){
        return demoService.consumerDemo();
    }
}

consumer的启动类

package com.msb.controller;

import com.msb.persion;
import com.msb.service.impl.PersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DemoController {
    @Autowired
    private PersionService persionService;
    @RequestMapping("/show")
    @ResponseBody
    public List<persion> show(){
        return persionService.show();
    }
}

provider启动器

package com.msb.dubbo.service.impl;

import com.msb.dubbo.pojo;
import com.msb.dubbo.service.DemoDubboService;
import org.apache.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.List;

//注解是apache
@Service
public class DemoDubboServiceImpl implements DemoDubboService {
    @Override
    public List<pojo> demo(Integer id, String name) {
        System.out.println("demo方法");
        return null;
    }
}

访问对应端口下的/dome即可读取到另一服务端返回的数据

posted @ 2022-07-20 16:47  爱豆技术部  阅读(29)  评论(0)    收藏  举报