03 dubbo 系列 | 基于注解的配置
前文已经介绍了基于spring xml 配置 dubbo,本文将介绍基于注解的方式配置dubbo。要想支持注解的配置,dubbo的版本必须2.6.3版本以上。
provider 的配置
- 应用共享配置
这是一个properties配置文件,用于配置dubbo的一个基本属性:如application、registry等。
#dubbo-provider.properties
dubbo.application.name=annotation-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
1
2
3
4
5
2
3
4
5
- Service注解暴露服务
package com.joyxj.dubbo.demo.annotation.impl;
import com.joyxj.dubbo.demo.api.DemoService;
import org.apache.dubbo.config.annotation.Service;
/**
* 基于注解的demo实现类
*
* @author xiaoj
* @version 1.0
* @date 2019-04-24
*/
@Service
public class AnnotationDemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "annotation: hello, " + name;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
这里需要特别的是:Service注解是dubbo提供的注解,而不是spring中的Service注解,千万不要import错误。
- 配置类
该类主要配置扫描路径
package com.joyxj.dubbo.demo.annotation.config;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* 提供者配置类
*
* @author xiaoj
* @version 1.0
* @date 2019-04-24
*/
@Configuration
@EnableDubbo(scanBasePackages = "com.joyxj.dubbo.demo.annotation.impl")
@PropertySource("classpath:/dubbo-provider.properties")
public class AnnotationProviderConfig {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- 启动类
package com.joyxj.dubbo.demo.annotation.bootstrap;
import com.joyxj.dubbo.demo.annotation.config.AnnotationProviderConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* 提供者启动类
*
* @author xiaoj
* @version 1.0
* @date 2019-04-24
*/
public class AnnotationProviderBootstrap {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(AnnotationProviderConfig.class);
context.start();
System.in.read();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
consumer 配置
与provider的配置基本上是一致的。
- 应用共享配置
#dubbo-provider.properties
dubbo.application.name=annotation-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
1
2
3
4
5
2
3
4
5
- Reference 注解引用服务
package com.joyxj.dubbo.demo.annotation.action;
import com.joyxj.dubbo.demo.api.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;
/**
* 【功能描述】
*
* @author xiaoj
* @version 1.0
* @date 2019-04-24
*/
@Component("annotationDemoAction")
public class AnnotationDemoAction {
@Reference
private DemoService demoService;
public String sayHello(String name) {
return demoService.sayHello(name);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- 配置类
定义扫描类和配置文件
package com.joyxj.dubbo.demo.annotation.config;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* 消费者配置
*
* @author xiaoj
* @version 1.0
* @date 2019-04-24
*/
@Configuration
@PropertySource("classpath:/dubbo-consumer.properties")
@ComponentScan(value = {"com.joyxj.dubbo.demo.annotation.action"})
@EnableDubbo(scanBasePackages = {"com.joyxj.dubbo.demo.annotation.action"})
public class AnnotationConsumerConfig {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
这里关键的注解是EnableDubbo。
- 启动类
package com.joyxj.dubbo.demo.annotation.bootstrap;
import com.joyxj.dubbo.demo.annotation.action.AnnotationDemoAction;
import com.joyxj.dubbo.demo.annotation.config.AnnotationConsumerConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* 【功能描述】
*
* @author xiaoj
* @version 1.0
* @date 2019-04-24
*/
public class AnnotationConsumerBootstrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext(AnnotationConsumerConfig.class);
context.start();
AnnotationDemoAction demoAction
= (AnnotationDemoAction)context.getBean("annotationDemoAction");
System.out.println(demoAction.sayHello("xiaojun"));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25