1.导入依赖
<!--导入zookeeper-3.4.9.jar 同时还将curator-client.jar相关导入--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency>
2.application.properties配置
#tomcat端口号 server.port=8080 spring.datasource.url=jdbc:mysql://localhost:3306/study2020 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root mybatis.configuration.map-underscore-to-camel-case=true spring.resources.static-locations:classpath:/static/,classpath:/templates/ #mybatis 配置 # 配置映射文件加载 mybatis.mapper-locations=classpath*:mappers/*.xml # 实体类通过别名使用 #mybatis.type-aliases-package=com.example.springboot.mybatis.entity spring.devtools.restart.enabled=true #dubbo相关配置 #服务名称 dubbo.application.name=springboot-user-service #注册中心地址 dubbo.registry.address=zookeeper://127.0.0.1:2181 #协议名称 dubbo.protocol.name=dubbo dubbo.protocol.port=20880
3.实现类上添加@com.alibaba.dubbo.config.annotation.Service注解(暴露服务),相当于下面的配置:
<!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.atxuetao.service.UserService" ref="userServiceImpl" /> <!-- 和本地bean一样实现服务 --> <bean id="userServiceImpl" class="com.atxuetao.service.impl.UserServiceImpl" />
package com.xt.service.impl; import com.xt.mapper.UserMapper; import com.xt.pojo.User; import com.xt.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @Author: TestRookie * @Date: 2021/6/27 19:47 * @Description: * */ @Service @com.alibaba.dubbo.config.annotation.Service//暴露服务 public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User selectByPrimaryKey(Integer id) { return userMapper.selectByPrimaryKey(id); } }
4.在启动类上添加@EnableDubbo注解,@service注解才能扫描到
package com.xt;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.xt.mapper")
@SpringBootApplication
@EnableDubbo//启动基于dubbo注解功能
public class SpringbootUserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootUserServiceApplication.class, args);
}
}
注:启动前,zookeeper必须启动服务