springboot整合dubbo(3)-配置服務提供者


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必須啟動服務


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM