您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。