springboot整合zookeeper和dubbo

coding

序言

dubbo就是个rpc框架,之前都是一直在用,现在稍微总结下以备以后使用。 我就直接贴代码了,代码肯定能运行,如果运行不了那么看我之前的zookeeper配置,或者把我贴的代码重新复制下,实在不行请留言,我看到会回复的。

  1. 整体项目结构
  • 项目是父子maven结构,父maven中基础jar包都依赖好了,子maven只需继承父maven,额外依赖自己的jar包就可以,其中domain是放实体类的,interfaceapi是放提供者提供给消费者的接口的。provider是提供者consumer是消费者,不过真实项目中可能一个模块即是消费者又是提供者。所以自己需要的话直接写就行,用法都一样的。

2.主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">

<modelVersion>4.0.0</modelVersion>

<groupId>tom.heliming.dubbo</groupId>

<artifactId>dubbo-demo</artifactId>

<packaging>pom</packaging>

<version>1.0-SNAPSHOT</version>

<modules>

<module>provider</module>

<module>consumer</module>

<module>interfaceapi</module>

<module>domain</module>

</modules>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.5.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>

<maven.compiler.source>1.8</maven.compiler.source>

<maven.compiler.target>1.8</maven.compiler.target>

<java.version>1.8</java.version>

</properties>

<dependencies>

<!-- 引入spring-boot-starter以及dubbo和curator的依赖 -->

<dependency>

<groupId>com.alibaba.boot</groupId>

<artifactId>dubbo-spring-boot-starter</artifactId>

<version>0.2.0</version>

</dependency>

<!-- Spring Boot相关依赖 -->

<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>

</dependencies>

</project>

  1. domain模块

    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>tom.heliming.dubbo</groupId>

    <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>domain</artifactId>

    </project>

    User类

     package top.hh.domain;

    import java.io.Serializable;

    public class User implements Serializable {

    private Integer id;

    private Integer age;

    private String name;

    public User() {

    }

    public User(Integer age, String name) {

    this.age = age;

    this.name = name;

    }

    public Integer getAge() {

    return age;

    }

    public void setAge(Integer age) {

    this.age = age;

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    }

  2. 接口模块

  • 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>tom.heliming.dubbo</groupId>

    <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>interfaceapi</artifactId>

    <dependencies>

    <dependency>

    <groupId>tom.heliming.dubbo</groupId>

    <artifactId>domain</artifactId>

    <version>1.0-SNAPSHOT</version>

    </dependency>

    </dependencies>

    </project>

    UserServiceApi类

      package top.hh.service;

    import top.hh.domain.User;

    import java.util.List;

    public interface UserServiceApi {

    public List<User> getUserList(String userId) ;

    }

  1. 提供者

  • 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>tom.heliming.dubbo</groupId>

    <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider</artifactId>

    <dependencies>

    <dependency>

    <groupId>tom.heliming.dubbo</groupId>

    <artifactId>interfaceapi</artifactId>

    <version>1.0-SNAPSHOT</version>

    </dependency>

    </dependencies>

    <build>

    <!-- 打包设置启动springboot的main函数 -->

    <plugins>

    <plugin>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-maven-plugin</artifactId>

    </plugin>

    </plugins>

    </build>

    </project>

    application.yml

      #当前服务/应用的名字

    dubbo:

    application:

    name: user-service-provider

    #注册中心的协议和地址

    registry:

    protocol: zookeeper

    #集群用逗号隔开

    address: 127.0.0.1:2181

    #通信规则(通信协议和接口)

    protocol:

    name: dubbo

    port: 20880

    #连接监控中心

    # monitor:

    # protocol: registry

    #开启包扫描,可替代 @EnableDubbo 注解

    ##dubbo.scan.base-packages=top.hh.provider.service

    UserServiceImpl类

		package top.hh.provider.service;

import com.alibaba.dubbo.config.annotation.Service;

import top.hh.domain.User;

import top.hh.service.UserServiceApi;

import java.util.ArrayList;

import java.util.List;

/**

* 提供者

*/

@Service

@org.springframework.stereotype.Service

public class UserServiceImpl implements UserServiceApi {

@Override

public List<User> getUserList(String userId) {

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

list.add(new User(1,userId+"hello"));

return list;

}

}

ProviderApp启动类

package top.hh.provider;

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

// 开启基于注解的dubbo功能(主要是包扫描@DubboComponentScan)

// 也可以在配置文件中使用dubbo.scan.base-package来替代 @EnableDubbo

@EnableDubbo

@SpringBootApplication

public class ProviderApp {

public static void main(String[] args) {

SpringApplication.run(ProviderApp.class,args);

}

}

  1. 消费者

  • 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>tom.heliming.dubbo</groupId>

    <version>1.0-SNAPSHOT</version>

    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer</artifactId>

    <dependencies>

    <dependency>

    <groupId>tom.heliming.dubbo</groupId>

    <artifactId>interfaceapi</artifactId>

    <version>1.0-SNAPSHOT</version>

    </dependency>

    </dependencies>

    <build>

    <!-- 打包设置启动springboot的main函数 -->

    <plugins>

    <plugin>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-maven-plugin</artifactId>

    </plugin>

    </plugins>

    </build>

    </project>

    application.yml文件

      #避免和监控中心端口冲突,设为8081端口访问

    server:

    port: 8081

    dubbo:

    application:

    name: login-service-consumer

    #注册中心的协议和地址

    registry:

    protocol: zookeeper

    #集群用逗号隔开

    address: 127.0.0.1:2181

    #通信规则(通信协议和接口)

    protocol:

    name: dubbo

    port: 20881

    #连接监控中心

    # monitor:

    # protocol: registry

    #扫描包

    # packageName: top.hh.consumer.service

    LoginController类

      package top.hh.consumer.controller;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.web.bind.annotation.GetMapping;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.ResponseBody;

    import org.springframework.web.bind.annotation.RestController;

    import top.hh.consumer.service.LoginServiceImpl;

    @RestController

    @RequestMapping

    public class LoginController {

    @Autowired

    LoginServiceImpl loginService;

    @GetMapping(value = "login")

    @ResponseBody

    public String login(String userId) {

    String result = loginService.loginService(userId);

    if (result==null){

    return "fail";

    }

    return result;

    }

    }

    LoginServiceImpl类

      package top.hh.consumer.service;

    import com.alibaba.dubbo.config.annotation.Reference;

    import org.springframework.stereotype.Service;

    import top.hh.domain.User;

    import top.hh.service.UserServiceApi;

    import java.util.List;

    @Service

    public class LoginServiceImpl {

    //failfast快速失效,只发起一次调用,失败立即报错,check=false启动时检查机制(默认是true:如果提供者没启动,消费者无法启动)

    @Reference(cluster = "failfast", retries = 0, interfaceClass = UserServiceApi.class, lazy = true, check = false, timeout = 5000)

    UserServiceApi userServiceApi;

    public String loginService(String userId) {

    List<User> userList = userServiceApi.getUserList(userId);

    if (userList != null && userList.size() > 0) {

    StringBuffer sbf = new StringBuffer();

    sbf.append("success->");

    userList.stream().forEach(str -> sbf.append("name :"+str.getName()));

    return sbf.toString();

    }

    return "fail";

    }

    }

    LoginApp启动类

      package top.hh;

    import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @EnableDubbo

    @SpringBootApplication

    public class LoginApp {

    public static void main(String[] args) {

    SpringApplication.run(LoginApp.class, args);

    }

    }

启动zookeeper, 提供者,消费者 访问:localhost:8081/login/?userId=1

以上是 springboot整合zookeeper和dubbo 的全部内容, 来源链接: utcz.com/z/509327.html

回到顶部