1. 程式人生 > >springboot整合zookeeper和dubbo

springboot整合zookeeper和dubbo

序言

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>
    
      </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](https://my.oschina.net/service)
      @org.springframework.stereotype.Service
      public class UserServiceImpl implements UserServiceApi {
    
      	[@Override](https://my.oschina.net/u/1162528)
      	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>
    
      </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快速失效,只發起一次呼叫,失敗立即報錯
      	@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