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)
最后
Java架构进阶面试及知识点文档笔记
这份文档共498页,其中包括Java集合,并发编程,JVM,Dubbo,Redis,Spring全家桶,MySQL,Kafka等面试解析及知识点整理
Java分布式高级面试问题解析文档
其中都是包括分布式的面试问题解析,内容有分布式消息队列,Redis缓存,分库分表,微服务架构,分布式高可用,读写分离等等!
互联网Java程序员面试必备问题解析及文档学习笔记
Java架构进阶视频解析合集
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
[外链图片转存中…(img-KGjiOtAp-1713826069879)]
[外链图片转存中…(img-UCD7TOTz-1713826069879)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Java)
[外链图片转存中…(img-k3gBiaBl-1713826069880)]
最后
Java架构进阶面试及知识点文档笔记
这份文档共498页,其中包括Java集合,并发编程,JVM,Dubbo,Redis,Spring全家桶,MySQL,Kafka等面试解析及知识点整理
[外链图片转存中…(img-cGuC7ExR-1713826069880)]
Java分布式高级面试问题解析文档
其中都是包括分布式的面试问题解析,内容有分布式消息队列,Redis缓存,分库分表,微服务架构,分布式高可用,读写分离等等!
[外链图片转存中…(img-yh3Q0IOE-1713826069880)]
互联网Java程序员面试必备问题解析及文档学习笔记
[外链图片转存中…(img-rLyKUPDZ-1713826069881)]
Java架构进阶视频解析合集
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!