前言:启动和执行流程

① 提供者端启动,容器(Container)负责把 Serivce  信息加载,并通过Protocol注册到注册中心

② 消费者启动,通过监听提供者列表来感知提供者信息,并在提供者发生改变时,通过注册中心         及时通知消费端

③ 消费方发起请求,通过 Proxy 模块

④ 利用 Cluster 模块,来选择真实的要发送给的提供者信息,交由 Consumer 中的 Protocol 把信       息发送给提供者

⑤ 提供者同样需要通过 Protocol 模块来处理消费者的信息

⑥ 最后由真正的服务提供者 Serivce 来进行处理

1、新建主工程 demo-base , pom 配置如下

8

8

2.7.5

org.apache.dubbo

dubbo

${dubbo.version}

org.apache.dubbo

dubbo-common

${dubbo.version}

org.apache.dubbo

dubbo-registry-zookeeper

${dubbo.version}

org.apache.dubbo

dubbo-registry-nacos

${dubbo.version}

org.apache.dubbo

dubbo-rpc-dubbo

${dubbo.version}

org.apache.dubbo

dubbo-remoting-netty4

${dubbo.version}

org.apache.dubbo

dubbo-serialization-hessian2

${dubbo.version}

log4j

log4j

1.2.17

org.slf4j

slf4j-api

1.7.5

org.slf4j

slf4j-log4j12

1.7.5

com.alibaba

fastjson

1.2.47

org.apache.maven.plugins

maven-compiler-plugin

3.1

1.8

1.8

2、新建子模块,公共api 即 service-api ,主要是接口类

public interface HelloService {

String hello(String name);

}

3、新建子模块,生产者 pom 文件如下

org.example

service-api

1.0-SNAPSHOT

org.apache.dubbo

dubbo

org.apache.dubbo

dubbo-registry-zookeeper

org.apache.dubbo

dubbo-rpc-dubbo

org.apache.dubbo

dubbo-remoting-netty4

org.apache.dubbo

dubbo-serialization-hessian2

实现类:

@Service

public class HelloServiceImpl implements HelloService {

@Override

public String hello(String name) {

return "hello:" + name;

}

}

//@Service注解不是spring的注解而是dubbo的注解

配置文件:dubbo-provider.properties

dubbo.application.name=service-provider

#下面两个值随便写

dubbo.protocol.name=dubbo

dubbo.protocol.port=20880

dubbo.application.qosEnable=true

dubbo.application.qosPort=33333

dubbo.application.qosAcceptForeignIp=false

log4j.properties

log4j.rootCategory=INFO,CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%t] %-5p %c.%M\(%F:%L\) - %m%n

启动类:

public class DubboPureMain {

public static void main(String[] args) throws IOException {

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);

context.start();

System.in.read();

}

@Configuration

@EnableDubbo(scanBasePackages = "com.lagou.impl.service")

@PropertySource("classpath:/dubbo-provider.properties")

static class ProviderConfiguration{

@Bean

public RegistryConfig registryConfig(){

RegistryConfig registryConfig = new RegistryConfig();

// timeout 要设置,不然连接时容易超过超时时间

registryConfig.setAddress("zookeeper://101.132.167.18:2181?timeout=30000");

return registryConfig;

}

}

}

 4、新建消费者 pom 文件如下

org.example

service-api

1.0-SNAPSHOT

org.apache.dubbo

dubbo

org.apache.dubbo

dubbo-registry-zookeeper

org.apache.dubbo

dubbo-rpc-dubbo

org.apache.dubbo

dubbo-remoting-netty4

org.apache.dubbo

dubbo-serialization-hessian2

客户端调用类:

@Component

public class ConsumerComponent {

@Reference

private HelloService helloService;

public String sayHello(String name){

return helloService.hello(name);

}

}

配置文件:dubbo-consumer.properties

dubbo.application.name=service-consumer

dubbo.registry.address=zookeeper://101.132.167.18:2181?timeout=30000

配置文件:log4j.properties

log4j.rootCategory=INFO,CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%t] %-5p %c.%M\(%F:%L\) - %m%n

启动类:

public class AnnoationConsumerMain {

public static void main(String[] args) throws IOException {

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);

context.start();

//获取消费者组件

ConsumerComponent service = context.getBean(ConsumerComponent.class);

while (true){

System.in.read();

String hello = service.sayHello("world");

System.out.println("result:" + hello);

}

}

@Configuration

@PropertySource("classpath:/dubbo-consumer.properties")

@ComponentScan(basePackages = "com.lagou.bean")

//为了使用 @Reference 注解

@EnableDubbo

static class ConsumerConfiguration{

}

}

参考链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: