首先,开始编写服务提供者的api接口,

SampleService  接口

 1 package bhz.dubbo.sample.provider;
 2 
 3 import java.util.List;
 4 
 5 public interface SampleService {
 6 
 7     String sayHello(String name);
 8 
 9     public List getUsers();
10 
11 }

实现类

 1 package bhz.dubbo.sample.provider.impl;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import bhz.dubbo.sample.provider.SampleService;
 7 
 8 public class SampleServiceImpl implements SampleService {
 9     
10     public String sayHello(String name) {
11         return "Hello " + name;
12     }
13 
14     public List getUsers() {
15         List list = new ArrayList();
16         User u1 = new User();
17         u1.setName("jack");
18         u1.setAge(20);
19         u1.setSex("m");
20 
21         User u2 = new User();
22         u2.setName("tom");
23         u2.setAge(21);
24         u2.setSex("m");
25 
26         User u3 = new User();
27         u3.setName("rose");
28         u3.setAge(19);
29         u3.setSex("w");
30 
31         list.add(u1);
32         list.add(u2);
33         list.add(u3);
34         return list;
35     }
36 }

User 对象

 1 package bhz.dubbo.sample.provider.impl;
 2 
 3 import java.io.Serializable;
 4 
 5 public class User implements Serializable {
 6     private static final long serialVersionUID = 1L;
 7     private int age;
 8     private String name;
 9     private String sex;
10 
11     public User() { 
12         super();
13     }
14 
15     public User(int age, String name, String sex) {
16         super();
17         this.age = age;
18         this.name = name;
19         this.sex = sex;
20     }
21 
22     public int getAge() {
23         return age;
24     }
25 
26     public void setAge(int age) {
27         this.age = age;
28     }
29 
30     public String getName() {
31         return name;
32     }
33 
34     public void setName(String name) {
35         this.name = name;
36     }
37 
38     public String getSex() {
39         return sex;
40     }
41 
42     public void setSex(String sex) {
43         this.sex = sex;
44     }
45 
46 }

启动类

 1 package bhz.dubbo.sample.test;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 public class Provider {
 6 
 7     public static void main(String[] args) throws Exception {
 8         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
 9                 new String[] { "sample-provider.xml" });
10         context.start(); 
11         System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
12     }
13 }

 

看一下配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://code.alibabatech.com/schema/dubbo
 7         http://code.alibabatech.com/schema/dubbo/dubbo.xsd
 8         ">
 9 
10     <!-- 具体的实现bean -->
11     <bean id="directService" class="bhz.dubbo.direct.provider.impl.DirectServiceImpl" />
12 
13     <!-- 提供方应用信息,用于计算依赖关系  -->
14     <dubbo:application name="direct-provider"  />
15 
16     <!-- 使用zookeeper注册中心暴露服务地址 只订阅的方式:register="false"-->
17     <dubbo:registry address="zookeeper://192.168.1.111:2181?backup=192.168.1.112:2181,192.168.1.113:2181" />
18 
19     <!-- 用dubbo协议在20880端口暴露服务 -->
20     <dubbo:protocol name="dubbo" port="20880" />
21 
22     <!-- 直连服务提供者:是在消费端进行配置的,而不是在服务提供端,所以这里不需要任何配置 -->
23     <dubbo:service retries="0" interface="bhz.dubbo.direct.provider.DirectService" ref="directService" />
24 
25 </beans>

 

下面看一下消费者:

因为在两个项目中,所以接口copy一下

 1 package bhz.dubbo.sample.provider;
 2 
 3 import java.util.List;
 4 
 5 public interface SampleService {
 6 
 7     String sayHello(String name);
 8 
 9     public List getUsers();
10 
11 }

消费类

 1 package bhz.dubbo.sample.test;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import bhz.dubbo.sample.provider.SampleService;
 8 
 9 public class Consumer {
10 
11     public static void main(String[] args) throws Exception {
12         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
13                 new String[] { "sample-consumer.xml" });
14         context.start();
15             
16         SampleService sampleService = (SampleService) context.getBean("sampleService");
17         String hello = sampleService.sayHello("tom");
18         System.out.println(hello);
19         
20 //        List list = sampleService.getUsers();
21 //        if (list != null && list.size() > 0) {
22 //            for (int i = 0; i < list.size(); i++) {
23 //                System.out.println(list.get(i));
24 //            }
25 //        }
26         System.in.read();
27     }
28 
29 }

看一下配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://code.alibabatech.com/schema/dubbo
 7         http://code.alibabatech.com/schema/dubbo/dubbo.xsd
 8         ">
 9 
10     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
11     <dubbo:application name="sample-consumer" />
12 
13     <dubbo:registry address="zookeeper://192.168.2.2:2181" />
14 
15     <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 -->
16     <dubbo:reference id="sampleService" check="false"
17         interface="bhz.dubbo.sample.provider.SampleService" />
18 
19 </beans>

将项目运行,可以发现,consumer项目,可以直接调用provider中的实现类,

dubbo  提供自带的监控系统,可以对服务进行查看,可以配置路由,权重等跟多功能,

dubbo  的管控台,需要自己去部署,可以参考官方文档

http://dubbo.apache.org/books/dubbo-admin-book/install/admin-console.html

将对应的源码下载下来,进行打包,部署。

 

 访问ip加端口,如上图的管理后台。官网也提供了相应的运维手册,可以操控操作。

 

A服务依赖于B服务,A 消费者调用A服务。看一下配置

1 package bhz.dubbo.dependency.provider;
2 
3 public interface DependencyService {
4 
5     public String dependency() throws Exception;
6 }
 1 package bhz.dubbo.dependency.provider.impl;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 
 5 import bhz.dubbo.dependency.provider.DependencyService;
 6 import bhz.dubbo.sample.provider.SampleService;
 7 
 8 //@Service("dependencyServiceImpl")
 9 //@com.alibaba.dubbo.config.annotation.Service(interfaceClass = bhz.dubbo.dependency.provider.DependencyService.class, protocol = { "dubbo" }, retries = 0)
10 public class DependencyServiceImpl implements DependencyService {
11 
12     // 注入SampleService
13     @Autowired
14     private SampleService sampleService;
15 
16     public String dependency() throws Exception {
17         // 这里 我们可能需要调用SampleService,也可能不需要...
18         System.out.println(sampleService.sayHello("jack"));
19         return "dependency exec";
20     }
21 
22 }
 1 package bhz.dubbo.dependency.test;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 public class Provider {
 6 
 7     public static void main(String[] args) {
 8         try {
 9             ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
10                     new String[] { "dependency-provider.xml" });
11             context.start();
12             
13             System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
14         } catch (Exception e) {
15             e.printStackTrace();
16         }
17     }
18 }

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://code.alibabatech.com/schema/dubbo
 7         http://code.alibabatech.com/schema/dubbo/dubbo.xsd
 8         ">
 9     
10     <dubbo:annotation package="bhz" />
11     
12     <bean id="dependencyService" class="bhz.dubbo.dependency.provider.impl.DependencyServiceImpl"/>
13     
14     <!-- 提供方应用信息,用于计算依赖关系 -->
15     <dubbo:application name="dependency-provider" />
16 
17     <!-- 使用zookeeper注册中心暴露服务地址 -->
18     <dubbo:registry address="zookeeper://192.168.2.2:2181" />
19 
20     <!-- 用dubbo协议在20880端口暴露服务 -->
21     <dubbo:protocol name="dubbo" port="20890" />
22     
23     <!-- 注意这里,我们在使用DependencyService的时候,这个服务可能需要依赖某一个服务,比如SampleService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 -->
24     <dubbo:reference id="sampleService"  check="true"
25         interface="bhz.dubbo.sample.provider.SampleService" />
26         
27     <dubbo:service retries="0" interface="bhz.dubbo.dependency.provider.DependencyService" ref="dependencyService" /> 
28 
29 </beans>

这边有一个字段,“check="true"”,代表着如果sampleService不先启动,则会报错。

这边引用了sampleService,上面已经写过了,

下面看一下consumer,soncumer 还是跟helloword的一样,只关心直接调用的A服务,A服务依赖的都不关心,

1 package bhz.dubbo.dependency.provider;
2 
3 public interface DependencyService {
4 
5     public String dependency() throws Exception;
6 }
 1 package bhz.dubbo.dependency.provider.impl;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 
 5 import bhz.dubbo.dependency.provider.DependencyService;
 6 import bhz.dubbo.sample.provider.SampleService;
 7 
 8 //@Service("dependencyServiceImpl")
 9 //@com.alibaba.dubbo.config.annotation.Service(interfaceClass = bhz.dubbo.dependency.provider.DependencyService.class, protocol = { "dubbo" }, retries = 0)
10 public class DependencyServiceImpl implements DependencyService {
11 
12     // 注入SampleService
13     @Autowired
14     private SampleService sampleService;
15 
16     public String dependency() throws Exception {
17         // 这里 我们可能需要调用SampleService,也可能不需要...
18         System.out.println(sampleService.sayHello("jack"));
19         return "dependency exec";
20     }
21 
22 }

 

 1 package bhz.dubbo.dependency.test;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 public class Provider {
 6 
 7     public static void main(String[] args) {
 8         try {
 9             ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
10                     new String[] { "dependency-provider.xml" });
11             context.start();
12             
13             System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
14         } catch (Exception e) {
15             e.printStackTrace();
16         }
17     }
18 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://code.alibabatech.com/schema/dubbo
 7         http://code.alibabatech.com/schema/dubbo/dubbo.xsd
 8         ">
 9     
10     <dubbo:annotation package="bhz" />
11     
12     <bean id="dependencyService" class="bhz.dubbo.dependency.provider.impl.DependencyServiceImpl"/>
13     
14     <!-- 提供方应用信息,用于计算依赖关系 -->
15     <dubbo:application name="dependency-provider" />
16 
17     <!-- 使用zookeeper注册中心暴露服务地址 -->
18     <dubbo:registry address="zookeeper://192.168.2.2:2181" />
19 
20     <!-- 用dubbo协议在20880端口暴露服务 -->
21     <dubbo:protocol name="dubbo" port="20890" />
22     
23     <!-- 注意这里,我们在使用DependencyService的时候,这个服务可能需要依赖某一个服务,比如SampleService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 -->
24     <dubbo:reference id="sampleService"  check="true"
25         interface="bhz.dubbo.sample.provider.SampleService" />
26         
27     <dubbo:service retries="0" interface="bhz.dubbo.dependency.provider.DependencyService" ref="dependencyService" /> 
28 
29 </beans>

 

posted on 2018-06-05 08:00  小~虎  阅读(243)  评论(0编辑  收藏  举报