<h1>前言</h1>
当今微服务架构发展非常迅速,应用也比较广泛,涌现出一批非常优秀的微服务架构,比如NetflixSpring Cloud 当当网的dubbox是应用最多的两个框架,项目也倾向于SpringBoot来构建。
我们项目准备应用Dubbox作用微服务框架,选址它处于以下两点考虑
rpc性能上的提升主要是增加了序列化的处理(内部项目应用之间的调用rpc性能比较理想)。
同时也支持Rest。
<h1>spring-boot-starter-dubbox</h1>
spring-boot-starter-dubbox,将springBoot与dubbox实现集成实现形成一个dubbo、Rest服务框架。
目前所有的项目都是通过SpringBoot来创建的大大简化了之前的XML操作,当时就想能不能把dubbox形成一个组件可以插拔化用的时候只需要引入包加上启动注解@EnableDubbox即可使用,当时第一反应就上看看GitHub有木有做个这个事情,然后并没有,但是找到了阿里同事对dubbo做了这样的一个事情https://github.com/alibaba/spring-boot-starter-dubbo,所以决定在上面做扩展主要支持Rest。spring-boot-starter-dubbox代码需要的同学我会上传GitHub或者以微信形式问我306570696。
<h1>如何发布dubbox服务</h1>
How to publish dubbo
- add Dependencies:
<dependency>
<groupId>com.euond</groupId>
<artifactId>spring-boot-starter-dubbox</artifactId>
<version>3.0.1-SNAPSHOT</version>
</dependency>
- add dubbo configuration in application.properties, demo:
server:
port: 7001
spring:
http:
encoding:
charset: UTF-8
enabled: true
dubbo:
appname: spring-boot-starter-dubbo-test
registry: zookeeper://127.0.0.1:2181
version: 1.0.0
dubbox-dubbo: #dubbo服务
protocol: dubbo
port: 20801
dubbox-rest: #rest服务
server: tomcat
port: 8889
protocol: rest
dubbox-webservice: #webservice服务
protocol: webservice
port: 8080
server: jetty
- then add
@EnableDubboConfiguration
on Spring Boot Application, indicates that dubbo is enabled.(web or non-web application can use dubbo provider)
@SpringBootApplication
@EnableDubboConfiguration
public class DubboProviderLauncher {
//...
}
- code your dubbo service, add
@Service
(import com.alibaba.dubbo.config.annotation.Service) on your service class, and interfaceClass is the interface which will be published.
@Service(interfaceClass = IHelloService.class)
public class HelloServiceImpl implements IHelloService {
//...
}
@Service(interfaceClass = AnotherUserRestService.class,protocol="rest")
@Component
public class AnotherUserRestServiceImpl implements AnotherUserRestService {
@Override
public String getString(Long id) {
// TODO Auto-generated method stub
return "Holle,Word";
}
@Override
public String getTest() {
// TODO Auto-generated method stub
return "Holle,Word";
}
}
- start Spring Boot.
How to consume Dubbo
- add Dependencies:
<dependency>
<groupId>com.euond</groupId>
<artifactId>spring-boot-starter-dubbox</artifactId>
<version>3.0.1-SNAPSHOT</version>
</dependency>
- add dubbo configuration in application.properties, demo:
server:
port: 7001
spring:
http:
encoding:
charset: UTF-8
enabled: true
dubbo:
appname: spring-boot-starter-dubbo-test
registry: zookeeper://127.0.0.1:2181
version: 1.0.0
dubbox-dubbo: #dubbo服务
protocol: dubbo
- then add
@EnableDubboConfiguration
on Spring Boot Application
@SpringBootApplication
@EnableDubboConfiguration
public class DubboConsumerLauncher {
//...
}
- injection interface by the
@DubboConsumer
annotation.
@Component
public class HelloConsumer {
@DubboConsumer
private IHelloService iHelloService;
}
/**
*
* @author tan.bin
* 非dubbo的消费端调用dubbo的REST服务(non-dubbo --> dubbo)
*/
@RestController
public class DemoNoDubooRestHello {
private static final Logger logger = LoggerFactory.getLogger(DemoNoDubooRestHello.class);
@RequestMapping("/hello2.json")
public Map<String,Object> restMap(){
Map<String,Object> restMap = new HashMap<String,Object>();
Map<String,String> restParams =new HashMap<String,String>();
restParams.put("name", "tan.bin");
String url="http://localhost:8889/rest-api/service1/person";
String results="";
try {
results=HttpUtils.get(url, restParams);
logger.info("请求成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
restMap.put("results", results);
return restMap;
}
}
/**
*
* @author tan.bin
* dubbo消费端调用dubbo的REST服务 (dubbo --> dubbo)
*/
@RestController
public class DemoRestHello {
@DubboConsumer(version="1.0.0")
IHelloDubboRestService helloDubboRestService;
@RequestMapping("/hello1.json")
public Map<String,Object> restMap(){
Map<String,Object> restMap = new HashMap<String,Object>();
String results=helloDubboRestService.hello("tan.bin");
restMap.put("results", results);
return restMap;
}
}