Dubbo+zookeeper搭配分布式服務(wù)的過(guò)程詳解
分布式架構(gòu):
1.當(dāng)垂直應(yīng)用越來(lái)越多,應(yīng)用之間交互不可避免,將核心業(yè)務(wù)抽取出來(lái),作為獨(dú)立的服務(wù),逐漸形成穩(wěn)定的服務(wù)中心,前端應(yīng)用能更快速的響應(yīng)多變的市場(chǎng)需求。
2.此時(shí),用于提高業(yè)務(wù)復(fù)用及整合的 分布式服務(wù)框架(RPC) 是關(guān)鍵。
Dubbo 是什么
- 一款分布式服務(wù)框架
- 高性能和透明化的RPC遠(yuǎn)程服務(wù)調(diào)用方案
- SOA服務(wù)治理方案
Dubbo:
作為分布式架構(gòu)比較后的框架,同時(shí)也是比較容易入手的框架,適合作為分布式的入手框架,下面是簡(jiǎn)單的搭建過(guò)程
工具:idea+tomact+zookeeper (知識(shí)點(diǎn):jsp,spring,springmvc,maven)
思想:

依賴(lài):
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!--zookeeper依賴(lài)-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.atchengdu</groupId>
<artifactId>001-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>工程分布:

provider實(shí)現(xiàn)interface提供服務(wù),constomer消費(fèi)provider提供的服務(wù)
interface:

package com.atchengdu.serviceinterface;
import com.atchengdu.pojo.User;
public interface Userservice {
//獲取user的信息
User getuserByid(Integer ie);
}
package com.atchengdu.pojo;
import java.io.Serializable;
public class User implements Serializable {
private Integer id ;
private String name;
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
public User() {
public Integer getId() {
return id;
public void setId(Integer id) {
public String getName() {
return name;
public void setName(String name) {
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';provider:

package com.atchengdu.Modulserviceimpl;
import com.atchengdu.pojo.User;
import com.atchengdu.serviceinterface.Userservice;
public class Userserviceimpl implements Userservice {
@Override
public User getuserByid(Integer ie) {
User user=new User(1,"張三");
return user;
}
}<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--聲明名稱(chēng)-->
<dubbo:application name="002-provider"></dubbo:application>
<!--設(shè)置協(xié)議和端口號(hào)-->
<dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
<!--使用注冊(cè)中心-->
<dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry>
<!--暴露服務(wù)接口-->
<dubbo:service interface="com.atchengdu.serviceinterface.Userservice" ref="userserviceimpl"></dubbo:service>
<!--加載業(yè)務(wù)實(shí)實(shí)現(xiàn)了-->
<bean id="userserviceimpl" class="com.atchengdu.Modulserviceimpl.Userserviceimpl"></bean>
</beans>constomer:

package com.atchengdu.webcontroller;
import com.atchengdu.pojo.User;
import com.atchengdu.serviceinterface.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Usercontroller {
@Autowired
private Userservice userservice;
@RequestMapping("/user")
public String user(Model model,Integer id ){
User user = userservice.getuserByid(id);
model.addAttribute("user",user);
return "user";
}
}<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task https://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.atchengdu.webcontroller">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"></property>
<property name="prefix" value="/"></property>
</bean>
</beans><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="003-constomer"></dubbo:application>
<dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry>
<dubbo:reference id="userservice" interface="com.atchengdu.serviceinterface.Userservice"></dubbo:reference>
</beans>到此這篇關(guān)于Dubbo+zookeeper搭配分布式服務(wù)的過(guò)程詳解的文章就介紹到這了,更多相關(guān)Dubbo+zookeeper分布式服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
swing中Tree與滾動(dòng)條用法實(shí)例分析
這篇文章主要介紹了swing中Tree與滾動(dòng)條用法,以實(shí)例形式分析了java基于swing實(shí)現(xiàn)圖形界面的使用技巧,需要的朋友可以參考下2015-09-09
eclipse輸出Hello World的實(shí)現(xiàn)方法
這篇文章主要介紹了eclipse輸出Hello World的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
SpringCloud GateWay動(dòng)態(tài)路由用法
網(wǎng)關(guān)作為所有項(xiàng)目的入口,不希望重啟,因此動(dòng)態(tài)路由是必須的,動(dòng)態(tài)路由主要通過(guò)RouteDefinitionRepository接口實(shí)現(xiàn),其默認(rèn)的實(shí)現(xiàn)是InMemoryRouteDefinitionRepository,即在內(nèi)存中存儲(chǔ)路由配置,可基于這個(gè)map對(duì)象操作,動(dòng)態(tài)路由的實(shí)現(xiàn)方案有兩種2024-10-10
JavaFx 中創(chuàng)建計(jì)時(shí)器的步驟詳解
本文介紹了如何在JavaFx中創(chuàng)建計(jì)時(shí)器,通過(guò)創(chuàng)建計(jì)時(shí)器界面、編寫(xiě)計(jì)時(shí)器邏輯以及關(guān)聯(lián)計(jì)時(shí)器按鈕,我們可以快速實(shí)現(xiàn)一個(gè)靈活可靠的計(jì)時(shí)器組件,本文能夠幫助讀者在 JavaFx 中成功實(shí)現(xiàn)自己的計(jì)時(shí)器功能,感興趣的朋友一起看看吧2023-11-11
Java字節(jié)碼增強(qiáng)技術(shù)知識(shí)點(diǎn)詳解
在本篇文章里小編給大家整理的是一篇關(guān)于Java字節(jié)碼增強(qiáng)技術(shù)知識(shí)點(diǎn)詳解內(nèi)容,有興趣的朋友可以跟著學(xué)習(xí)下。2021-08-08
微信公眾號(hào)測(cè)試賬號(hào)自定義菜單的實(shí)例代碼
這篇文章主要介紹了微信公眾號(hào)測(cè)試賬號(hào)自定義菜單的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
實(shí)例解決Java異常之OutOfMemoryError的問(wèn)題
在本篇文章中,我們給大家分享了關(guān)于解決Java異常之OutOfMemoryError的問(wèn)題的方法,有此需要的朋友們學(xué)習(xí)下。2019-02-02
Eclipse遠(yuǎn)程debug的步驟與注意事項(xiàng)
今天小編就為大家分享一篇關(guān)于Eclipse遠(yuǎn)程debug的步驟與注意事項(xiàng),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03

