dubbo + zookeeper + spring 分布式系统(二)

public class Provider3 {

public static void main(String[] args) throws Exception {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

new String[] {“provider.xml”});

context.start();

System.in.read(); // 按任意键退出

}

}

5.2 也可以部署到tomcat 中去;那就要配置一下 web.xml 文件了


例如:看 dubbo-service1_1-project 的配置文件

里面的那些配置文件就不贴出来了 ;下载源代码看就行了 ;

最好可以打包成war 部署到tomcat 中 也是一样的效果

6.步骤5.1 或者5.2 部署成功之后的效果 ,打开dubbo-admin 查看


7. 服务注册成功了 ,那么怎么使用呢?


三 . 如何消费 提供者提供的服务----消费者

============================

刚刚发布了 service3 ,并且保持它一直在启动状态(不要把provder3 停止就行了)

可以在dubbo-admin 看到它是没有消费者的,现在我们在 Controller 层来消费它;

1. Controller 的配置


web.xml

dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:applicationContext.xml

0

spring-mvc.xml  开启自动扫描

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:context=“http://www.springframework.org/schema/context”

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” 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-3.0.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<context:component-scan base-package=“com.dubbo” />

<mvc:annotation-driven />

dubbo-config.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=“test_consumer” />

<dubbo:registry address=" " />

<dubbo:reference interface=“com.dubbo.service3.Test1Service3” id=“test1Service3” check=“false” />

配置 了需要消费的服务  service3 ;然后在Controller 中 就可以自己 通过注解 @Autowired 使用

@Autowired

private Test1Service3 test1Service3;

@RequestMapping(value = “/test3”)

@ResponseBody

public String test3(){

return test1Service3.test1Service3_1();

}

2.1 tomcat启动服务…


请求接口…

启动完了可以看到的是  服务 显示正常 表示 有提供者 又有消费者呢 ;

注意  如果注册中心 没有注册成功的话 启动会报错

2.2 本地 测试调用远程服务


comsumer.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=“service2_consumer” />

<dubbo:registry address=“zookeeper://120.25.162.6:2181” />

<dubbo:reference id=“test1Service3” interface=“com.dubbo.service3.Test1Service3” />

comsumer2.java

public class Consumer2 {

public static void main(String[] args) throws Exception {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]

{“consumer.xml”});

context.start();

Test1Service3 test1Service3 = (Test1Service3)context.getBean(“test1Service3”); // 获取远程服务代理

String s = test1Service3.test1Service3_1(); // 执行远程方法

System.out.print(“end…”+s);

}

}

String s = test1Service3.test1Service3_1();

打个断点,然后去dubbo-admin 看 可以发现 出现消费者;

放掉断点 ,消费者消失;并且 打印结果:end…test1Service3_1…

===

四. 服务之间相互调用,每个服务既是消费者 又是提供者

============================

1. 整体怎么配置


看服务  service1 ; 既是提供者 ,又是消费者 ;

Test1Service1Impl 文件

package com.dubbo.service1.impl;

import com.dubbo.service1.Test1Service1;

import com.dubbo.service1.Test2Service1;

import com.dubbo.service2.Test1Service2;

import com.dubbo.service3.Test1Service3;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

/**

  • Created by shirenchuang on 2017/3/16.

*/

@Service

public class Test1Service1Impl implements Test1Service1{

@Autowired

private Test1Service3 test1Service3;

@Autowired

private Test2Service1 test2Service1 ;

public String test1Service1_1() {

String s = test1Service3.test1Service3_1();

return s+“test1Service1_1…”;

}

public String test1Service1_2() {

return “test1Service1_2…”;

}

public String test11_21(){

String s = test2Service1.test2Service1_1();

return “Test1Service1Impl 测试本地服务 是调用本地还是调用 远程:”+s;

}

}

看代码 可以知道 它 消费了 服务 Service3  ,还有服务Service1 中的 Test2Service1Impl  (同一个服务中有多个 类 ,这里不去谈论 它是本地调用还是 远程调用的问题)

我们来看看所有的配置

we b.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns=“http://java.sun.com/xml/ns/javaee” xmlns:web=“http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”

xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”

id=“WebApp_ID” version=“2.5”>

dubbo-service1_1-project

index.html

index.htm

index.jsp

org.springframework.web.context.ContextLoaderListener

contextConfigLocation

classpath:applicationContext.xml

applicationContext.xml

dubbo-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:p=“http://www.springframework.org/schema/p”

xmlns:dubbo=“http://code.alibabatech.com/schema/dubbo”

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<dubbo:application name=“service1_provider” />

<dubbo:registry address=“zookeeper://120.25.162.6:2181” />

<dubbo:protocol name=“dubbo” port=“20886” />

<dubbo:service interface=“com.dubbo.service1.Test1Service1” ref=“test1Service1” />

<dubbo:service interface=“com.dubbo.service1.Test2Service1” ref=“test2Service1” />

dubbo-consumer.xml

配置自己所有需要消费的服务

注意下面的药注释掉

<dubbo:application name=“service2_consumer” />

<!– 使用multicast广播注册中心暴露发现服务地址 –>

<dubbo:registry address=“zookeeper://120.25.162.6:2181” />

因为在provider中已经配置了 ,不能配置两个;

<?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:reference id=“test1Service3” interface=“com.dubbo.service3.Test1Service3” />

<dubbo:reference id=“test2Service1” interface=“com.dubbo.service1.Test2Service1” />

2.配置完了 然后启动


2.1 可以 部署到tomcat 中 ;

Test1Service1Impl 的 test1Service1_1 的时候会自动装载  test1Service3 的服务(前提是 这个服务已经注册了)

public String test1Service1_1() {

String s = test1Service3.test1Service3_1();

return s+“test1Service1_1…”;

}

2.2 可以本地代码测试

可以新建一个Consumer1 的类  来本地测试  ;加载 spring容器

public static void main(String[] args) throws Exception {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

可以新建一个Consumer1 的类  来本地测试  ;加载 spring容器

public static void main(String[] args) throws Exception {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-0J4yvAR1-1715667332136)]

[外链图片转存中…(img-EOeT2Mij-1715667332137)]

[外链图片转存中…(img-MJY7cseO-1715667332137)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
Spring Boot是一个开源的Java框架,用于快速构建独立的、基于Spring的应用程序。它简化了Spring应用程序的配置和部署过程,并提供了许多嵌入式服务器,如Tomcat、Jetty等。Spring Boot还提供了自动配置的特性,可以根据类路径中的依赖自动配置Spring应用程序。 Dubbo是一款高性能的分布式服务框架,也是阿里巴巴开源的项目。它提供了服务治理、远程通讯和分布式调用等功能,帮助开发人员轻松构建分布式服务化的应用。 Zookeeper是一个开源的分布式协调服务,可以用于实现分布式应用程序的一致性和可靠性。它提供了一个类似于文件系统的层次化的命名空间,并允许开发人员注册、协调和共享各种资源,如配置信息、服务注册和发现等。 当使用Spring Boot结合DubboZookeeper时,可以构建一个高性能、可扩展和可靠的微服务架构。Spring Boot提供了便利的开发和部署方式,Dubbo提供了分布式服务框架的支持,而Zookeeper则提供了分布式协调服务。开发人员可以使用Spring Boot快速构建独立的微服务应用程序,使用Dubbo进行服务间的通信和管理,同时通过Zookeeper进行服务的注册和发现。这样的架构可以方便地实现微服务架构中的资源共享和服务治理等功能,大大简化了开发人员的负担。 综上所述,Spring Boot结合DubboZookeeper可以构建高效、可靠的微服务架构,并提供了便利的开发和部署方式,帮助开发人员构建高性能的分布式应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值