Dubbo helloword

时间:2023-03-09 08:37:29
Dubbo  helloword

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

SampleService  接口

 package bhz.dubbo.sample.provider;

 import java.util.List;

 public interface SampleService {

     String sayHello(String name);

     public List getUsers();

 }

实现类

 package bhz.dubbo.sample.provider.impl;

 import java.util.ArrayList;
import java.util.List; import bhz.dubbo.sample.provider.SampleService; public class SampleServiceImpl implements SampleService { 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;
}
}

User 对象

 package bhz.dubbo.sample.provider.impl;

 import java.io.Serializable;

 public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int age;
private String name;
private String sex; public User() {
super();
} public User(int age, String name, String sex) {
super();
this.age = age;
this.name = name;
this.sex = sex;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} }

启动类

 package bhz.dubbo.sample.test;

 import org.springframework.context.support.ClassPathXmlApplicationContext;

 public class Provider {

     public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "sample-provider.xml" });
context.start();
System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
}
}

看一下配置文件

 <?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
"> <!-- 具体的实现bean -->
<bean id="directService" class="bhz.dubbo.direct.provider.impl.DirectServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="direct-provider" /> <!-- 使用zookeeper注册中心暴露服务地址 只订阅的方式:register="false"-->
<dubbo:registry address="zookeeper://192.168.1.111:2181?backup=192.168.1.112:2181,192.168.1.113:2181" /> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" /> <!-- 直连服务提供者:是在消费端进行配置的,而不是在服务提供端,所以这里不需要任何配置 -->
<dubbo:service retries="0" interface="bhz.dubbo.direct.provider.DirectService" ref="directService" /> </beans>

下面看一下消费者:

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

 package bhz.dubbo.sample.provider;

 import java.util.List;

 public interface SampleService {

     String sayHello(String name);

     public List getUsers();

 }

消费类

 package bhz.dubbo.sample.test;

 import java.util.List;

 import org.springframework.context.support.ClassPathXmlApplicationContext;

 import bhz.dubbo.sample.provider.SampleService;

 public class Consumer {

     public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "sample-consumer.xml" });
context.start(); SampleService sampleService = (SampleService) context.getBean("sampleService");
String hello = sampleService.sayHello("tom");
System.out.println(hello); // List list = sampleService.getUsers();
// if (list != null && list.size() > 0) {
// for (int i = 0; i < list.size(); i++) {
// System.out.println(list.get(i));
// }
// }
System.in.read();
} }

看一下配置文件

 <?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="sample-consumer" /> <dubbo:registry address="zookeeper://192.168.2.2:2181" /> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 -->
<dubbo:reference id="sampleService" check="false"
interface="bhz.dubbo.sample.provider.SampleService" /> </beans>

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

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

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

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

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

Dubbo  helloword

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

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

 package bhz.dubbo.dependency.provider;

 public interface DependencyService {

     public String dependency() throws Exception;
}
 package bhz.dubbo.dependency.provider.impl;

 import org.springframework.beans.factory.annotation.Autowired;

 import bhz.dubbo.dependency.provider.DependencyService;
import bhz.dubbo.sample.provider.SampleService; //@Service("dependencyServiceImpl")
//@com.alibaba.dubbo.config.annotation.Service(interfaceClass = bhz.dubbo.dependency.provider.DependencyService.class, protocol = { "dubbo" }, retries = 0)
public class DependencyServiceImpl implements DependencyService { // 注入SampleService
@Autowired
private SampleService sampleService; public String dependency() throws Exception {
// 这里 我们可能需要调用SampleService,也可能不需要...
System.out.println(sampleService.sayHello("jack"));
return "dependency exec";
} }
 package bhz.dubbo.dependency.test;

 import org.springframework.context.support.ClassPathXmlApplicationContext;

 public class Provider {

     public static void main(String[] args) {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "dependency-provider.xml" });
context.start(); System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
} catch (Exception e) {
e.printStackTrace();
}
}
}
 <?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:annotation package="bhz" /> <bean id="dependencyService" class="bhz.dubbo.dependency.provider.impl.DependencyServiceImpl"/> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dependency-provider" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.2.2:2181" /> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20890" /> <!-- 注意这里,我们在使用DependencyService的时候,这个服务可能需要依赖某一个服务,比如SampleService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 -->
<dubbo:reference id="sampleService" check="true"
interface="bhz.dubbo.sample.provider.SampleService" /> <dubbo:service retries="0" interface="bhz.dubbo.dependency.provider.DependencyService" ref="dependencyService" /> </beans>

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

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

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

 package bhz.dubbo.dependency.provider;

 public interface DependencyService {

     public String dependency() throws Exception;
}
 package bhz.dubbo.dependency.provider.impl;

 import org.springframework.beans.factory.annotation.Autowired;

 import bhz.dubbo.dependency.provider.DependencyService;
import bhz.dubbo.sample.provider.SampleService; //@Service("dependencyServiceImpl")
//@com.alibaba.dubbo.config.annotation.Service(interfaceClass = bhz.dubbo.dependency.provider.DependencyService.class, protocol = { "dubbo" }, retries = 0)
public class DependencyServiceImpl implements DependencyService { // 注入SampleService
@Autowired
private SampleService sampleService; public String dependency() throws Exception {
// 这里 我们可能需要调用SampleService,也可能不需要...
System.out.println(sampleService.sayHello("jack"));
return "dependency exec";
} }
 package bhz.dubbo.dependency.test;

 import org.springframework.context.support.ClassPathXmlApplicationContext;

 public class Provider {

     public static void main(String[] args) {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "dependency-provider.xml" });
context.start(); System.in.read(); // 为保证服务一直开着,利用输入流的阻塞来模拟
} catch (Exception e) {
e.printStackTrace();
}
}
}
 <?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:annotation package="bhz" /> <bean id="dependencyService" class="bhz.dubbo.dependency.provider.impl.DependencyServiceImpl"/> <!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dependency-provider" /> <!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://192.168.2.2:2181" /> <!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20890" /> <!-- 注意这里,我们在使用DependencyService的时候,这个服务可能需要依赖某一个服务,比如SampleService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 -->
<dubbo:reference id="sampleService" check="true"
interface="bhz.dubbo.sample.provider.SampleService" /> <dubbo:service retries="0" interface="bhz.dubbo.dependency.provider.DependencyService" ref="dependencyService" /> </beans>