Dubbo+zookeeper搭配分布式服务的方法

发布时间:2022-04-06 10:50:26 作者:iii
来源:亿速云 阅读:114

本文小编为大家详细介绍“Dubbo+zookeeper搭配分布式服务的方法”,内容详细,步骤清晰,细节处理妥当,希望这篇“Dubbo+zookeeper搭配分布式服务的方法”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

分布式架构: 

1.当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,前端应用能更快速的响应多变的市场需求。 
2.此时,用于提高业务复用及整合的 分布式服务框架(RPC) 是关键。

Dubbo 是什么

Dubbo:

作为分布式架构比较后的框架,同时也是比较容易入手的框架,适合作为分布式的入手框架,下面是简单的搭建过程

工具:idea+tomact+zookeeper (知识点:jsp,spring,springmvc,maven)

思想:

Dubbo+zookeeper搭配分布式服务的方法

依赖:

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

工程分布:

Dubbo+zookeeper搭配分布式服务的方法

provider实现interface提供服务,constomer消费provider提供的服务

interface:

Dubbo+zookeeper搭配分布式服务的方法

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:

Dubbo+zookeeper搭配分布式服务的方法

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:

Dubbo+zookeeper搭配分布式服务的方法

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搭配分布式服务的方法”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 从头开始搭建一个dubbo+zookeeper平台
  2. vue可以搭配什么ui框架

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

dubbo zookeeper

上一篇:mysql查询结果怎么实现多列拼接查询

下一篇:docker-compose怎么配置并部署redis服务

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》