dubbo + zookeeper + spring 分布式系统(1)
return “test1Service3_1…” ;
}
}
4.配置 配置文件

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=“service3_provider” />
<dubbo:registry address=“zookeeper://120.25.162.6:2181” />
<dubbo:protocol name=“dubbo” port=“20884” />
<dubbo:service interface=“com.dubbo.service3.Test1Service3” ref=“test1Service3” />
5.1 启动spring 容器 ;java代码启动
provder3.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
- Created by shirenchuang on 2017/3/16.
*/
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…”;
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。


既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
总结
互联网大厂比较喜欢的人才特点:对技术有热情,强硬的技术基础实力;主动,善于团队协作,善于总结思考。无论是哪家公司,都很重视高并发高可用技术,重视基础,所以千万别小看任何知识。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。其实我写了这么多,只是我自己的总结,并不一定适用于所有人,相信经过一些面试,大家都会有这些感触。
**另外本人还整理收藏了2021年多家公司面试知识点以及各种技术点整理 **
下面有部分截图希望能对大家有所帮助。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
总结
互联网大厂比较喜欢的人才特点:对技术有热情,强硬的技术基础实力;主动,善于团队协作,善于总结思考。无论是哪家公司,都很重视高并发高可用技术,重视基础,所以千万别小看任何知识。面试是一个双向选择的过程,不要抱着畏惧的心态去面试,不利于自己的发挥。同时看中的应该不止薪资,还要看你是不是真的喜欢这家公司,是不是能真的得到锻炼。其实我写了这么多,只是我自己的总结,并不一定适用于所有人,相信经过一些面试,大家都会有这些感触。
**另外本人还整理收藏了2021年多家公司面试知识点以及各种技术点整理 **
下面有部分截图希望能对大家有所帮助。
[外链图片转存中…(img-97FOUXpt-1712570047416)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)