Skip to content

Commit

Permalink
Spring Boot (十三)集成Dubbo
Browse files Browse the repository at this point in the history
  • Loading branch information
1265400024 committed Nov 30, 2016
1 parent 1433faa commit e2e1fa2
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 2 deletions.
35 changes: 35 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,41 @@
<artifactId>unitils-core</artifactId>
<version>3.4.2</version>
</dependency>

<!-- Dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>

</dependencies>

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/training/SpringBootServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ImportResource;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
@ImportResource({"classpath:dubbo.xml"})
public class SpringBootServlet extends SpringBootServletInitializer {

// jar启动
public static void main(String[] args) {
SpringApplication.run(SpringBootServlet.class, args);
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(SpringBootServlet.class, args);
}

// tomcat war启动
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/training/dubbo/dto/DubboDemoDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.training.dubbo.dto;

import java.io.Serializable;

public class DubboDemoDto implements Serializable {

public DubboDemoDto() {
super();
}

public DubboDemoDto(Integer id, String name) {
super();
this.id = id;
this.name = name;
}

private Integer id;

private String name;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
13 changes: 13 additions & 0 deletions src/main/java/com/training/dubbo/service/DubboDemoService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.training.dubbo.service;

import java.util.List;

import com.training.dubbo.dto.DubboDemoDto;

public interface DubboDemoService {

public DubboDemoDto getDubboDemoDto();

public List<DubboDemoDto> findAllDubboDemoDto();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.training.dubbo.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.training.dubbo.dto.DubboDemoDto;
import com.training.dubbo.service.DubboDemoService;

public class DubboDemoServiceImpl implements DubboDemoService {

@Override
public DubboDemoDto getDubboDemoDto() {
return new DubboDemoDto(1, "张三");
}

@Override
public List<DubboDemoDto> findAllDubboDemoDto() {
List<DubboDemoDto> list = new ArrayList<DubboDemoDto>();
list.add(new DubboDemoDto(1, "张三"));
list.add(new DubboDemoDto(2, "李四"));
return list;
}

}
7 changes: 7 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@ spring:
#指定log的配置文件,以及记录Spring Boot的log级别
logging:
config: classpath:logbak.xml


#Dubbo
#ZooKeeper
dubbo:
registry:
address: 192.168.2.72:2181

24 changes: 24 additions & 0 deletions src/main/resources/dubbo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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="compute-service" />

<!-- 注册中心服务地址 -->
<dubbo:registry id="zookeeper" protocol="zookeeper" address="${dubbo.registry.address}" />

<!-- 用dubbo协议在30001 -->
<dubbo:protocol name="dubbo" port="30001" dispather="all" threadpool="cached" threads="5000"/>

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.training.dubbo.service.DubboDemoService" ref="dubboDemoService"
version="1.0" registry="zookeeper" owner="shp"/>

<!-- 具体服务接口的实现 -->
<bean id="dubboDemoService" class="com.training.dubbo.service.impl.DubboDemoServiceImpl" />

</beans>

0 comments on commit e2e1fa2

Please sign in to comment.