您好,登录后才能下订单哦!
zookpeer搭建
下载zookeeper-3.4.6,cd ./conf/ , cp zoo_sample.cfg zoo.cfg
./bin/zkServer.sh start 启动zookpeer服务
dubboAdmain控制台搭建
下载dubbo-admin.war,解压后放到tomcat的运行目录下,修改配置WEB-INF/dubbo.properties
文件:
dubbo.registry.address=zookeeper://192.168.1.201:2181,注意改成自己的地址
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
这里,我们为一个应用发布两个提供者在不同的机器上,即使它们中有一台机器挂了,服务端也可以正常访问。
package com.service;
import java.util.List;
/**
* @描述 :
* @创建者 : guoxiaopeng
* @创建时间 : 2017-7-11 下午12:44:45
*/
public interface DemoService {
String sayHello(String name);
public List getUsers();
}
package com.service;
import java.util.ArrayList;
import java.util.List;
import com.domain.User;
/**
* @描述 :
* @创建者 : guoxiaopeng
* @创建时间 : 2017-7-11 下午12:45:12
*/
public class DemoServiceImpl implements DemoService{
public String sayHello(String name) {
return "Hello " + name;
}
public List getUsers() {
List list = new ArrayList();
User u1 = new User();
u1.setName("jack");
u1.setAge(20);
u1.setSex("m");
User u2 = new User();
u2.setName("tom");
u2.setAge(21);
u2.setSex("m");
User u3 = new User();
u3.setName("rose");
u3.setAge(19);
u3.setSex("w");
list.add(u1);
list.add(u2);
list.add(u3);
return list;
}
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @描述 :
* @创建者 : guoxiaopeng
* @创建时间 : 2017-7-11 下午1:05:57
*/
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "spring-config.xml" });
context.start();
System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
}
}
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>provider1</groupId>
<artifactId>dubbo-provider1</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provider1 Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.opensymphony.quartz</groupId>
<artifactId>quartz</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<!-- spring end -->
<!-- dubbo start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- dubbo end -->
<!-- zookeeper start -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- zookeeper end -->
</dependencies>
<build>
<finalName>dubbo-provider1</finalName>
</build>
</project>
spring-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 -->
<!-- "> -->
<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"
default-autowire="byName">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="demo_provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.1.201:2181" timeout="50" />
<!-- <dubbo:registry address="zookeeper://192.168.1.26:2181" timeout="50" /> -->
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.service.DemoService"
ref="demoService" />
<!-- 具体的实现bean,用来注入-->
<bean id="demoService" class="com.service.DemoServiceImpl" />
</beans>
spring-config-rpc.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 -->
<!-- "> -->
<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"
default-autowire="byName">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="demo_provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.1.201:2181" timeout="50" />
<!-- <dubbo:registry address="zookeeper://192.168.1.26:2181" timeout="50" /> -->
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.service.DemoService"
ref="demoService" />
<!-- 具体的实现bean,用来注入-->
<bean id="demoService" class="com.service.DemoServiceImpl" />
</beans>
生成者项目层级结构
服务端代码:
ClientTest.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.DemoService;
/**
* @描述 :
* @创建者 : guoxiaopeng
* @创建时间 : 2017-7-12 上午11:26:27
*/
public class ClientTest {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("spring-config.xml");
DemoService demoService= (DemoService) app.getBean("demoService");
String sayHello = demoService.sayHello("hello");
System.out.println(sayHello);
}
}
spring-config-rpc.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"
default-autowire="byName">
<dubbo:application name="dubbo_consumer"></dubbo:application>
<!--zookeeper注册中心 -->
<dubbo:registry address="zookeeper://192.168.1.201:2181" timeout="50" />
<!-- <dubbo:registry address="zookeeper://192.168.1.26:2181" timeout="50" /> -->
<!-- 要引用的服务 -->
<dubbo:reference interface="com.service.DemoService" id="demoService" />
</beans>
spring-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:context="http://www.springframework.org/schema/context"
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"
default-autowire="byName">
<!-- 注解传播形式 -->
<import resource="spring/spring-config-rpc.xml" />
</beans>
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>consumer</groupId>
<artifactId>dubbo-consumer</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-consumer Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.opensymphony.quartz</groupId>
<artifactId>quartz</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<!-- spring end -->
<!-- dubbo start -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- dubbo end -->
<!-- zookeeper start -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- zookeeper end -->
<!-- 提供者接口 start -->
<dependency>
<groupId>myDubbo</groupId>
<artifactId>myDubbo</artifactId>
<version>0.0.2</version>
</dependency>
<!-- 提供者接口 end -->
</dependencies>
<build>
<finalName>dubbo-consumer</finalName>
</build>
</project>
浏览器访问地址:http://127.0.0.1:8080/dubboAdmin/
提供者和服务端
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。