dubbo + zookeeper + spring 分布式系统
3.实现 接口
package com.dubbo.service3.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.service3.Test1Service3;
/**
Created by shirenchuang on 2017/3/16.
*/
public class Test1Service3Impl implements Test1Service3{
public String test1Service3_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" />
<bean id="test1Service3" class="com.dubbo.service3.impl.Test1Service3Impl" />
<dubbo:service interface="com.dubbo.service3.Test1Service3" ref="test1Service3" />
</beans>
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
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
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 />
</beans>
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" />
</beans>
配置 了需要消费的服务 ?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"
xml 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 ns: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="test1Service1" interface="com.dubbo.service1.Test1Service1" />
<dubbo:reference id="test2Service1" interface="com.dubbo.service1.Test2Service1" />
-->
<dubbo:reference id="test1Service3" interface="com.dubbo.service3.Test1Service3" />
</beans>
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">
<display-name>dubbo-service1_1-project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
applicationContext.xml
评论