Feign+mybatisplus搭建项目遇到的坑

出现的错误
1.Failed to bind properties under ‘spring.datasource’ to javax.sql.DataSource:
[email protected](required=true)等

原因(简洁说明)
使用代码生成器的service层需要继承extends IService,这是一个坑,它不可以在接口工程中使用,实体类可以
如果想要使用mp的功能,就需要:
—— > 1.接口工程中创建无extends IService的接口 A
—— > 2.provider工程中放入代码生成的service层 接口B
—— > 3.provider工程的实现类需要 implements A,B
代码:
接口工程

import com.study.seckill.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;

@Service
@FeignClient(value = "seckill-provider-localhost")
public interface IUserService /*extends IService*/ {

    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public List<User> quallAll();

}

接口工程依赖

<dependencies>
    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-openfeignartifactId>
    dependency>

    <dependency>
      <groupId>com.baomidougroupId>
      <artifactId>mybatis-plus-generateartifactId>
      <version>2.2.0version>
    dependency>
    <dependency>
      <groupId>com.baomidougroupId>
      <artifactId>mybatis-plus-extensionartifactId>
      <version>3.1.1version>
    dependency>
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <optional>trueoptional>
    dependency>
  dependencies>

–注意不要有druid数据源依赖,可能会报sqlSession等异常–

consumer工程代码
controller

@RestController
public class UserConsumerController {

    @Autowired
    private IUserService iUserService;

    @GetMapping(value = "/users")
    public  String queryByName(){
        List<User> users = iUserService.quallAll();
        return users.toString();
    }
    
}

启动类

@EnableFeignClients(basePackages = {"com.study.seckill"})
@SpringBootApplication
@EnableEurekaClient
public class Seckill_Consumer_8080 {
    public static void main(String[] args) {
        SpringApplication.run(Seckill_Consumer_8080.class, args);
    }
}

consumer依赖

 <dependencies>
    <dependency>
      <groupId>com.studygroupId>
      <artifactId>seckill-apiartifactId>
      <version>0.0.1-SNAPSHOTversion>
    dependency>

    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
    dependency>
    
    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-ribbonartifactId>
      <version>1.4.6.RELEASEversion>
    dependency>

    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-eurekaartifactId>
      <version>1.4.6.RELEASEversion>
    dependency>
    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-openfeignartifactId>
    dependency>

    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-hystrixartifactId>
    dependency>
    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-hystrix-dashboardartifactId>
    dependency>
  dependencies>

consumer的application.yml

server:
  port: 8080
#配置eureka
eureka:
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: seckill-consumer-8080
    prefer-ip-address: true
spring:
  application:
    name: seckill-consumer-8080

#开启降级
feign:
#  hystrix:
#    enabled: true

#feign客户端负载均衡策略
#seckill-provider-localhost:
#  ribbon:
#
##   NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #配置规则 随机
#    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #配置规则 轮询
##   NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RetryRule #配置规则 重试
##   NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule #配置规则 响应时间权重
##   NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule #配置规则 最空闲连接策略
#    ConnectTimeout: 5000 #请求连接超时时间
#    ReadTimeout: 10000 #请求处理的超时时间
#    OkToRetryOnAllOperations: true #对所有请求都进行重试
#    MaxAutoRetriesNextServer: 2 #切换实例的重试次数
#    MaxAutoRetries: 2 #对当前实例的重试次数

借鉴:feign客户端负载均衡策略


prodvider工程
如图:UserServiceImpl 需要实现这个UserService和IUserService
service层结构

import com.baomidou.mybatisplus.extension.service.IService;
import com.study.seckill.entity.User;
public interface UserService  extends IService<User> {
}

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.seckill.entity.User;
import com.study.seckill.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.seckill.service.IUserService;
import com.study.seckill.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService, UserService {

    @Override
    public List<User> quallAll() {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        return baseMapper.selectList(null);
    }
}

provider的application.properties

mybatis-plus.type-aliases-package=com.study.seckill.entity
mybatis-plus.global.config.refresh-mapper=true
mybatis-plus.mapper-locations=classpath:/mapper/**/*.xml

provider依赖

<dependencies>
    <dependency>
      <groupId>com.studygroupId>
      <artifactId>seckill-apiartifactId>
      <version>0.0.1-SNAPSHOTversion>
    dependency>


    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-jdbcartifactId>
    dependency>

    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
    dependency>

    <dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-eurekaartifactId>
    dependency>
    <dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-actuatorartifactId>
    dependency>
    <dependency>
      <groupId>mysqlgroupId>
      <artifactId>mysql-connector-javaartifactId>
    dependency>

    <dependency>
      <groupId>com.baomidougroupId>
      <artifactId>mybatis-plus-boot-starterartifactId>
      <version>3.1.1version>
    dependency>
    <dependency>
      <groupId>com.baomidougroupId>
      <artifactId>mybatis-plusartifactId>
      <version>3.1.1version>
    dependency>
    
    <dependency>
      <groupId>com.baomidougroupId>
      <artifactId>mybatis-plus-generateartifactId>
      <version>2.2.0version>
    dependency>
    <dependency>
      <groupId>com.baomidougroupId>
      <artifactId>mybatis-plus-extensionartifactId>
      <version>3.1.1version>
    dependency>
    
    <dependency>
      <groupId>org.freemarkergroupId>
      <artifactId>freemarkerartifactId>
      <version>2.3.29version>
    dependency>

    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>druidartifactId>
      <version>1.1.12version>
    dependency>
    <dependency>
      <groupId>org.projectlombokgroupId>
      <artifactId>lombokartifactId>
      <optional>trueoptional>
    dependency>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
    dependency>
    <dependency>
      <groupId>log4jgroupId>
      <artifactId>log4jartifactId>
    dependency>
    <dependency>
      <groupId>ch.qos.logbackgroupId>
      <artifactId>logback-coreartifactId>
    dependency>

    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-testartifactId>
    dependency>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-testartifactId>
      <scope>testscope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintagegroupId>
          <artifactId>junit-vintage-engineartifactId>
        exclusion>
      exclusions>
    dependency>

  dependencies>

  <build>
    
    <resources>
      <resource>
        <directory>src/main/resourcesdirectory>
        
        <includes>
          <include>**/*.propertiesinclude>
          <include>**/*.ymlinclude>
          <include>**/**/*.xmlinclude>
        includes>
        <filtering>falsefiltering>
      resource>
    resources>
  build>
project>

provider工程需要配置druid数据源,不然会报异常,这里不再赘述。

在搭建工程上踩了两天坑,对你有帮助的话,就点个赞吧

你可能感兴趣的:(mybatisplus)