搭建简单的SpringCloud项目一:注册中心和公共层

2021-06-04 00:02

阅读:724

标签:app   reg   ruid   exp   work   width   depend   public   registry   

注:笔者在搭建途中其实遇见不少问题,统一放在后面的文章说明,现在的搭建是测试OK的。

GitHub:https://github.com/ownzyuan/test-cloud

 

 

数据库

创建一个非常简单的user表

技术图片

父工程

新建Maven项目(不直接创建SpringBoot项目):

技术图片

删掉父工程的src,引入SprinCloud、SprinBoot、Eureka、tkMybatis、Lombok等的依赖

dependencyManagement>
        dependencies>
?
            dependency>
                groupId>org.springframework.cloudgroupId>
                artifactId>spring-cloud-dependenciesartifactId>
                version>Greenwich.SR1version>
                type>pomtype>
                scope>importscope>
            dependency>
?
            dependency>
                groupId>org.springframework.bootgroupId>
                artifactId>spring-boot-dependenciesartifactId>
                version>2.1.4.RELEASEversion>
                type>pomtype>
                scope>importscope>
            dependency>
?
            dependency>
                groupId>org.springframework.cloudgroupId>
                artifactId>spring-cloud-starter-eureka-serverartifactId>
                version>1.4.0.RELEASEversion>
            dependency>
?
            dependency>
                groupId>org.springframework.cloudgroupId>
                artifactId>spring-cloud-starter-eurekaartifactId>
                version>1.4.0.RELEASEversion>
            dependency>
?
            dependency>
                groupId>org.springframework.cloudgroupId>
                artifactId>spring-cloud-openfeign-coreartifactId>
                version>2.0.1.RELEASEversion>
            dependency>
?
            dependency>
                groupId>org.springframework.cloudgroupId>
                artifactId>spring-cloud-starter-openfeignartifactId>
                version>2.0.1.RELEASEversion>
            dependency>
?
            dependency>
                groupId>mysqlgroupId>
                artifactId>mysql-connector-javaartifactId>
                version>5.1.47version>
            dependency>
?
            dependency>
                groupId>com.github.drtranggroupId>
                artifactId>druid-spring-boot2-starterartifactId>
                version>1.1.10version>
            dependency>
?
            dependency>
                groupId>tk.mybatisgroupId>
                artifactId>mapper-spring-boot-starterartifactId>
                version>1.1.2version>
            dependency>
?
            dependency>
                groupId>tk.mybatisgroupId>
                artifactId>mapperartifactId>
                version>3.4.1version>
            dependency>
?
            dependency>
                groupId>org.projectlombokgroupId>
                artifactId>lombokartifactId>
                version>1.16.20version>
            dependency>
?
        dependencies>
dependencyManagement>

搭建注册中心

新建Moudle

技术图片

eureka项目中pom依赖:

dependencies>
    dependency>
        groupId>org.springframework.cloudgroupId>
        artifactId>spring-cloud-starter-eureka-serverartifactId>
    dependency>
    dependency>
        groupId>org.springframework.cloudgroupId>
        artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
    dependency>
    dependency>
        groupId>org.springframework.bootgroupId>
        artifactId>spring-boot-devtoolsartifactId>
    dependency>
dependencies>

application文件配置

server:
  port: 8081
?
eureka:
  instance:
    hostname: 127.0.0.1
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

最后创建启动类,注意加入@EnableEurekaServer

 技术图片

启动项目并访问http://localhost:8081/

 技术图片

注册中心完成

 

加入common

注:非必要加入的一层,仅为了方便统一管理。

common-base层

这一层主要是将一些基础功能做好封装,在需要的时候直接继承过去就行。

引入Lombok、tkMybatis

dependencies>
    dependency>
        groupId>org.projectlombokgroupId>
        artifactId>lombokartifactId>
    dependency>
    dependency>
        groupId>tk.mybatisgroupId>
        artifactId>mapper-spring-boot-starterartifactId>
    dependency>
    dependency>
        groupId>tk.mybatisgroupId>
        artifactId>mapperartifactId>
    dependency>
dependencies>

首先是基础DTO对象

技术图片

然后是IBaseService接口

技术图片

对应实现类(注意有些方法不实现,故使用抽象类):

技术图片

common层

依赖与common-base一样,同时将common-base层也引进来

dependencies>  
        dependency>
            groupId>org.projectlombokgroupId>
            artifactId>lombokartifactId>
        dependency>
        dependency>
            groupId>tk.mybatisgroupId>
            artifactId>mapper-spring-boot-starterartifactId>
        dependency>
        dependency>
            groupId>tk.mybatisgroupId>
            artifactId>mapperartifactId>
        dependency>
        dependency>
            groupId>com.zygroupId>
            artifactId>cloud-common-baseartifactId>
            version>1.0-SNAPSHOTversion>
        dependency>
dependencies>

创建User实体

import lombok.AllArgsConstructor;
import lombok.Data;
?
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
?
@Data
@AllArgsConstructor
@Table(name = "user")
public class User {
?
    @Id
    private Long id;
?
    @Column(name = "name")
    private String name;
?
    @Column(name = "status")
    private Integer status;
}

创建UserDTO:

import com.zy.entity.dto.BaseDTO;
import lombok.Data;
import lombok.experimental.Accessors;
?
import javax.persistence.Column;
import javax.persistence.Table;
?
@Data
@Accessors(chain = true)
@Table(name = "user")
public class UserDTO extends BaseDTO {
?
    @Column(name = "name")
    private String name;
?
}

 

搭建简单的SpringCloud项目一:注册中心和公共层

标签:app   reg   ruid   exp   work   width   depend   public   registry   

原文地址:https://www.cnblogs.com/kzyuan/p/14666140.html


评论


亲,登录后才可以留言!