Spring Boot中集成Dubbox

阅读更多

1、dubbox需要在本地install,地址https://github.com/dangdangdotcom/dubbox.git,使用版本为2.8.4

 

2、新建spring boot项目,版本为1.5.6.RELEASE

  •   创建maven模块

    spring-boot-dubbox-api:dubbo服务接口

    spring-boot-dubbox-provider:dubbo服务提供者

 

  •   引入dubbo配置

 

@Configuration
@ImportResource("classpath:dubbo/dubbo.xml")
public class PropertiesLoad {

}
 
   dubbo.xml具体配置如下

 

 

    
    

    

    
    
    

    
    

  • 发布服务
@Service//alibaba服务注解
@Path("user")//rest服务
//rest服务数据格式,类与方法同时指定,方法中的优先级高
@Produces({ "application/json; charset=UTF-8", "text/xml; charset=UTF-8" })
public class UserServiceImpl implements UserService {

	 private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	
	@POST
	@Path("save")
	@Produces({ ContentType.APPLICATION_JSON_UTF_8 }) //dubbox封装的类型
	public WrappResult save() {
        //解决primitive类型不被JAXB序列化的问题
		return new WrappResult((int) (Math.random() * 10));
	}
	
	@GET
	@Path("{id : \\d+}")
	public User getUserById(@PathParam("id") Long id, @Context HttpServletRequest request) {
		logger.info("Client address is:{}", request.getRemoteAddr());
		User user = new User(1L, "SpringBoog-Dubbox",20);
    	return user;
	}
	
	@GET
	@Path("/xml/{id : \\d+}")
	@Produces(ContentType.TEXT_XML_UTF_8) //使用xml格式
	public User getUserByIdXml(@PathParam("id") Long id) {
		logger.info("Client address is:{}", RpcContext.getContext().getRemoteAddressString());
		User user = new User(10L, "Xml-Entity",20);
    	return user;
	}
}

 

3、Spring Boot启动类

 

@SpringBootApplication
public class ProviderApp {
	public static void main(String[] args) throws IOException {
		SpringApplication.run(ProviderApp.class, args);
	}
// 使用jetty加载
//	@Bean
//	public EmbeddedServletContainerFactory servletContainer() {
//		JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();
//		return factory;
//	}

}

 

  • rest服务发布结果
main  INFO zookeeper.ZookeeperRegistry:  [DUBBO] Register: rest://10.*.*.*:8090/org.spring.boot.dubbox.api.UserService?anyhost=true……
main  INFO zookeeper.ZookeeperRegistry:  [DUBBO] Subscribe: provider://10*.*.*:8090/org.spring.boot.dubbox.api.UserService?anyhost=true&……
main  INFO zookeeper.ZookeeperRegistry:  [DUBBO] Notify urls for subscribe url provider://10.*.*.*:8090/org.spring.boot.dubbox.api.UserService?anyhost=true&……
  • rest服务验证  

    http://localhost:8090/user/1.xml


  20
  1
  SpringBoog-Dubbox

     http://localhost:8090/user/1.json

{  "id": 1,
  "name": "SpringBoog-Dubbox",
  "age": 20
}

 

 项目地址https://github.com/hjguang/spring-boot-dubbox

你可能感兴趣的:(spring,boot,dubbox)