springboot整合dubbo

发布时间:2024年01月05日

1、创建三个工程:api(jar)、service(jar)、test(war)。

2、img-api:

package com.demo.service;

import java.util.List;
import com.demo.module.Img;

public interface ImgService {
	List<Img> selectAll();

}

3、img-service:

? ? ? ?

首先pom加上依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.demo</groupId>
	<artifactId>img-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<!-- springBoot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- api -->
		<dependency>
			<groupId>com.demo</groupId>
			<artifactId>img-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

		<!-- mybatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.21</version>
		</dependency>


		<!-- dubbo -->
		<dependency>
			<groupId>com.alibaba.spring.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>
	</dependencies>

</project>

? 实现层:


package com.demo.serviceImpl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.mapper.ImgMapper;
import com.demo.module.Img;
import com.demo.service.ImgService;

@Service("imgService")
public class ImgServiceImpl implements ImgService{
	
	@Autowired
	private ImgMapper imgMapper;

	@Override
	public List<Img> selectAll() {		
		return imgMapper.selectAll();
	}

}

? properties配置数据库和Mybatis:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=wtyy

mybatis.mapper-locations=classpath*:mapper/*Mapper.xml

#spring.dubbo.application.name=img-service
#spring.dubbo.registry.address=zookeeper://127.0.0.1
#spring.dubbo.registry.port=2181
#spring.dubbo.base-package=com.demo.serviceImpl
#spring.dubbo.protocol.name=dubbo
server.port=8081

? dubbo配置文件provider.xml:

<?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="img-service"/>
    <!-- 使用zookeeper做为注册中心 -->
    <dubbo:registry  protocol="zookeeper" address="zookeeper://127.0.0.1:2181"  register="false"/>
    
   
	<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
	<!-- 用dubbo协议在20880端口暴露服务 -->
	<dubbo:protocol name="dubbo" port="20881" />
    
    <!-- ref中的值要跟服务实现类中的@Server的值一致 -->
    <dubbo:service interface="com.demo.service.ImgService" ref="imgService"></dubbo:service>
</beans>

? 启动类:


package com.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;

 @SpringBootApplication
 @MapperScan("com.demo.mapper")
 @ImportResource("classpath:provider.xml")
public class ServiceStart {
	 public static void main(String[] args) {
		SpringApplication.run(ServiceStart.class, args);
	}

}

4、img-test应用:

? ??

? 添加pom依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.demo</groupId>
  <artifactId>img-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <!-- springBoot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- api -->
		<dependency>
			<groupId>com.demo</groupId>
			<artifactId>img-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

	<!-- dubbo -->
		<dependency>
			<groupId>com.alibaba.spring.boot</groupId>
			<artifactId>dubbo-spring-boot-starter</artifactId>
			<version>2.0.0</version>
		</dependency>


	</dependencies>


</project>

? properties配置文件:

#spring.dubbo.application.name=img-test
#spring.dubbo.registry.address=zookeeper://127.0.0.1
#spring.dubbo.registry.port=2181
#spring.dubbo.base-package=com.demo.service
#spring.dubbo.protocol.name=dubbo
server.port=8888

配置port是防止先启动service再启动rest出现端口占用问题,但有时候只配port会报错:

解决方法是加上数据库的配置(虽然没有用,但是不报错了):

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=wtyy

?dubbo配置文件consumer.xml:

<?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="img-test"></dubbo:application>
   

    <!-- zookeeper作为注册中心 -->
    <dubbo:registry  protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
    <!-- 生成远程服务代理 -->
    <dubbo:reference interface="com.demo.service.ImgService" id="imgService"  
    	 url="dubbo://127.0.0.1:20881"/>
</beans>

? 启动类:


package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;

 @SpringBootApplication
 @EnableDubboConfiguration
 @ImportResource("classpath:consumer.xml")
public class RestStart {
	public static void main(String[] args) {
		SpringApplication.run(RestStart.class, args);
	}

}

先启动service,再启动test,运行如下:

文章来源:https://blog.csdn.net/w_t_y_y/article/details/135403006
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。