<h1>前言</h1>
當(dāng)今微服務(wù)架構(gòu)發(fā)展非常迅速,應(yīng)用也比較廣泛,涌現(xiàn)出一批非常優(yōu)秀的微服務(wù)架構(gòu),比如NetflixSpring Cloud 當(dāng)當(dāng)網(wǎng)的dubbox是應(yīng)用最多的兩個(gè)框架,項(xiàng)目也傾向于SpringBoot來(lái)構(gòu)建。
我們項(xiàng)目準(zhǔn)備應(yīng)用Dubbox作用微服務(wù)框架,選址它處于以下兩點(diǎn)考慮
rpc性能上的提升主要是增加了序列化的處理(內(nèi)部項(xiàng)目應(yīng)用之間的調(diào)用rpc性能比較理想)。
同時(shí)也支持Rest。
<h1>spring-boot-starter-dubbox</h1>
spring-boot-starter-dubbox,將springBoot與dubbox實(shí)現(xiàn)集成實(shí)現(xiàn)形成一個(gè)dubbo、Rest服務(wù)框架。
目前所有的項(xiàng)目都是通過(guò)SpringBoot來(lái)創(chuàng)建的大大簡(jiǎn)化了之前的XML操作,當(dāng)時(shí)就想能不能把dubbox形成一個(gè)組件可以插拔化用的時(shí)候只需要引入包加上啟動(dòng)注解@EnableDubbox即可使用,當(dāng)時(shí)第一反應(yīng)就上看看GitHub有木有做個(gè)這個(gè)事情,然后并沒(méi)有,但是找到了阿里同事對(duì)dubbo做了這樣的一個(gè)事情https://github.com/alibaba/spring-boot-starter-dubbo,所以決定在上面做擴(kuò)展主要支持Rest。spring-boot-starter-dubbox代碼需要的同學(xué)我會(huì)上傳GitHub或者以微信形式問(wèn)我306570696。
<h1>如何發(fā)布dubbox服務(wù)</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服務(wù)
protocol: dubbo
port: 20801
dubbox-rest: #rest服務(wù)
server: tomcat
port: 8889
protocol: rest
dubbox-webservice: #webservice服務(wù)
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服務(wù)
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的消費(fèi)端調(diào)用dubbo的REST服務(wù)(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("請(qǐng)求成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
restMap.put("results", results);
return restMap;
}
}
/**
*
* @author tan.bin
* dubbo消費(fèi)端調(diào)用dubbo的REST服務(wù) (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;
}
}