本文小編為大家詳細介紹“Dubbo+zookeeper搭配分布式服務的方法”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Dubbo+zookeeper搭配分布式服務的方法”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
1.當垂直應用越來越多,應用之間交互不可避免,將核心業務抽取出來,作為獨立的服務,逐漸形成穩定的服務中心,前端應用能更快速的響應多變的市場需求。
2.此時,用于提高業務復用及整合的 分布式服務框架(RPC) 是關鍵。
一款分布式服務框架
高性能和透明化的RPC遠程服務調用方案
SOA服務治理方案
作為分布式架構比較后的框架,同時也是比較容易入手的框架,適合作為分布式的入手框架,下面是簡單的搭建過程
工具:idea+tomact+zookeeper (知識點:jsp,spring,springmvc,maven)
<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依賴--> <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實現interface提供服務,constomer消費provider提供的服務
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"> <!--聲明名稱--> <dubbo:application name="002-provider"></dubbo:application> <!--設置協議和端口號--> <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol> <!--使用注冊中心--> <dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry> <!--暴露服務接口--> <dubbo:service interface="com.atchengdu.serviceinterface.Userservice" ref="userserviceimpl"></dubbo:service> <!--加載業務實實現了--> <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>
讀到這里,這篇“Dubbo+zookeeper搭配分布式服務的方法”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。