Apache Dubbo 学习二


前言

上一次学习了duboo单机版的,现在来学习一下使用zookeeper做注册中心。


一、使用步骤

1.服务提供者

首先这是服务提供者的项目结构
在这里插入图片描述
首先配置项目依赖
pom.xml

<?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>dubbo</groupId>
  <artifactId>6-multl-zk-provider</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <!--dubbo依赖-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.4</version>
    </dependency>
    <!--spring依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.10.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>dubbo</groupId>
      <artifactId>3-interface</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.apache.curator</groupId>
      <artifactId>curator-framework</artifactId>
      <version>4.1.0</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.7.25</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

这里写了两个版本的接口,可以根据不同版本调用不同的接口,后面会介绍。
SomeServiceImpl

package dubbo.service.impl;

import dubbo.model.User;
import dubbo.service.SomeService;

public class SomeServiceImpl implements SomeService {
    public String hello() {
        return "hello dubbo zookeeper";
    }

    public User queryUserById(Integer id) {
        User user = new User();
        user.setId(id);
        user.setUsername("zk-"+id);
        return user;
    }
}

SomeServiceImpl_v2

package dubbo.service.impl;

import dubbo.model.User;
import dubbo.service.SomeService;

public class SomeServiceImpl_v2 implements SomeService {
    public String hello() {
        return "hello SomeServiceImpl_v2";
    }

    public User queryUserById(Integer id) {
        User user = new User();
        user.setId(id);
        user.setUsername("zk2-"+id);
        return user;
    }
}

提供者暴露接口
dubbo-zk-provider.xml

<?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="6-multl-zk-provider" />
    <!--指定协议与端口-->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!--指定注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181" />
    <!--暴露服务
        interface:服务类名
        ref:spring接口中的标识名称
    -->
    <dubbo:service interface="dubbo.service.SomeService" ref="someServiceImpl" version="1.0"/>
    <dubbo:service interface="dubbo.service.SomeService" ref="someServiceImpl_v2" version="2.0"/>

    <!--加载接口实现类-->
    <bean id="someServiceImpl" class="dubbo.service.impl.SomeServiceImpl" />
    <bean id="someServiceImpl_v2" class="dubbo.service.impl.SomeServiceImpl_v2" />
</beans>

导入配置文件。
web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
		  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
		  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
		  version="4.0">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:dubbo-zk-provider.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

2.服务消费者

消费者项目结构。
在这里插入图片描述
对外访问的接口。
SomeController

package dubbo.web;

import dubbo.model.User;
import dubbo.service.SomeService;
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 SomeController {

    @Autowired
    private SomeService someService;

    @Autowired
    private SomeService someService_v2;

    @RequestMapping(value = "/hello")
    public String hello(Model model){
        //调用远程接口服务
        String hello = someService.hello();
        model.addAttribute("hello",hello);

        String hello_v2 = someService_v2.hello();
        model.addAttribute("hello_v2",hello_v2);
        return "hello";
    }

    @RequestMapping(value = "/user/detail")
    public User queryUserById(Model model,Integer id){
        User user = someService.queryUserById(id);
        model.addAttribute("user",user);
        return user;
    }
}

消费者配置
dubbo-zk-consumer.xml

<?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="7-multl-zk-consumer"/>

    <dubbo:registry address="zookeeper://localhost:2181" />

    <!--引用远程接口
        id:远程接口服务的代理名称
        interface:全类名
    -->
    <dubbo:reference id="someService" interface="dubbo.service.SomeService" version="1.0"/>
    <dubbo:reference id="someService_v2" interface="dubbo.service.SomeService" version="2.0"/>
</beans>

springmvc.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--扫描组件-->
    <context:component-scan base-package="dubbo.web"/>

    <!--注解驱动-->
    <mvc:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

配置网络拦截器
web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
		  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
		  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
		  version="4.0">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:dubbo-zk-consumer.xml,classpath:springmvc.xml</param-value>
	</context-param>

	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml,classpath:dubbo-zk-consumer.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

视图配置
hello.jsp

<%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2021/12/11
  Time: 12:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>${hello}</h1>
<h1>${hello_v2}</h1>
</body>
</html>

两个项目配置完成之后,就配置运行tomcat运行即可。像学习一里面那样配置一样。在这里就不多介绍了。直接看的小伙伴请回去看一。https://blog.csdn.net/qq_27756989/article/details/121878747

先运行启动者,再运行消费者就可以看到结果了。
在这里插入图片描述

总结

今日就学习到这啦~ 一步一步变强!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值