springboot整合dubbo\zookeeper作註冊中心

  springboot整合dubbo發佈服務,zookeeper作註冊中心。前期的安裝zookeeper以及啓動zookeeper集羣就不說了。java

 

  dubbo-admin-2.5.4.war:dubbo服務管理項目,下載完後部署到tomcat便可查看(登陸的用戶名和密碼默認都是root)。git

pom文件引入dubbo以及zkcli包:github

<!-- 引入dubbo-spring-boot-starter以及zkclient依賴 -->
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.9</version>
        </dependency>

 

 

1.dubbo服務端(提供dubbo服務)

目錄結構以下:spring

 

 配置文件以下tomcat

 application.propertiesspringboot

############################################################ # # DUBBO相關配置 # ############################################################ #當前服務/應用名稱 spring.dubbo.application.name=provider #註冊中心的協議和地址 #spring.dubbo.registry.address=127.0.0.1:2181 #spring.dubbo.registry.protocol=zookeeper spring.dubbo.server=true spring.dubbo.registry=zookeeper://127.0.0.1:2181 #通訊規則(通訊協議和接口) spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880

 

User.javaapp

package org.springboot.dubbo.bean; import java.io.Serializable; public class User implements Serializable { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public User(String userName, String password) { super(); this.userName = userName; this.password = password; } }

 

UserService.javaide

package org.springboot.dubbo.service; import java.util.List; import org.springboot.dubbo.bean.User; public interface UserService { List<User> getAllUsers(); User getUserByUserName(String username); }

UserServiceImpl.java (註解發佈服務,注意@Service註解是dubbo提供的)spring-boot

package org.springboot.dubbo.service; import java.util.ArrayList; import java.util.List; import org.springboot.dubbo.bean.User; import org.springframework.stereotype.Component; import com.alibaba.dubbo.config.annotation.Service; @Service(version = "1.0.0") // dubbo的service註解,不具有spring的@service註解的功能
@Component public class UserServiceImpl implements UserService { public List<User> getAllUsers() { List<User> users = new ArrayList<User>(); for (int i = 0; i < 20; i++) { User user = new User("usernnamei" + i, "password" + i); users.add(user); } return users; } public User getUserByUserName(String username) { return new User(username, username); } }

 

 應用啓動類:this

package org.springboot.dubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; @SpringBootApplication @EnableDubboConfiguration public class MySpringBootApplication { public static void main(String[] args) throws InterruptedException { // 入口運行類
        SpringApplication.run(MySpringBootApplication.class, args); Thread.sleep(50 * 1000); } }

 

啓動以後查看dubbo管理界面:

 

 2.dubbo客戶端(使用dubbo提供的服務)

 目錄結構:

 

application.properties

############################################################ # # DUBBO相關配置 # ############################################################ #當前服務/應用名稱 spring.dubbo.application.name=consumer #註冊中心的協議和地址 spring.dubbo.server=true spring.dubbo.registry=zookeeper://127.0.0.1:2181 #通訊規則(通訊協議和接口) spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=org.springboot.dubbo

 

UserController.java(經過@Reference消費服務)

package org.springboot.dubbo.controller; import java.util.List; import org.springboot.dubbo.bean.User; import org.springboot.dubbo.service.UserService; import org.springframework.stereotype.Controller; import com.alibaba.dubbo.config.annotation.Reference; @Controller public class UserController { @Reference(version = "1.0.0") UserService userService; public List<User> getAllUsers() { return userService.getAllUsers(); } public User getUserByUserName(String username) { return userService.getUserByUserName(username); } }

 結果:

[org.springboot.dubbo.bean.User@31741aad, org.springboot.dubbo.bean.User@3d836d38, org.springboot.dubbo.bean.User@7f779714, org.springboot.dubbo.bean.User@45fe36d, org.springboot.dubbo.bean.User@334c0b59, org.springboot.dubbo.bean.User@457d1a5e, org.springboot.dubbo.bean.User@184e34f6, org.springboot.dubbo.bean.User@36770ef1, org.springboot.dubbo.bean.User@42760c26, org.springboot.dubbo.bean.User@5417b4e0, org.springboot.dubbo.bean.User@60640941, org.springboot.dubbo.bean.User@479bbbd7, org.springboot.dubbo.bean.User@414944fb, org.springboot.dubbo.bean.User@314fdc39, org.springboot.dubbo.bean.User@72033b79, org.springboot.dubbo.bean.User@3235bf69, org.springboot.dubbo.bean.User@66a9a03f, org.springboot.dubbo.bean.User@a57bcc1, org.springboot.dubbo.bean.User@6292835, org.springboot.dubbo.bean.User@780eb2bb]
org.springboot.dubbo.bean.User@605dfc60

 

管理界面查看以下:

 

 

 

 

 

  至此基本完成了簡單的dubbo環境搭建。

  git地址:   https://github.com/qiao-zhi/springboot

相關文章
相關標籤/搜索