一、项目结构
项目结构:

如下图:

 

 

项目工程简介:

changgou_auth: oauth2.0授权认证管理中心
changgou_common:公共模块
changgou_common_db:公共模块
changgou_eureka:注册中心
shangchneg_reverser:
changgou_gateway: 网关模块。
changgou_service: 微服务模块,该模块用于存放所有独立的微服务工程。
changgou_service_api: 对应工程的JavaBean、Feign、以及Hystrix配置,该工程主要对外提供依赖。
changgou_transaction_fescar: 分布式事务模块,将分布式事务抽取到该工程中,任何工程如需要使用分布式事务,只需依赖该工程即可。
changgou_web: web服务工程,对应功能模块如需要调用多个微服务,可以将他们写入到该模块中,例如网站后台、网站前台等

 

  二、工程搭建


工程搭建步骤:

步骤一:创建父工程changgou_parent,删除src文件,并配置pom.xml文件

创建父工程changgou_parent,并配置pom.xml文件

步骤二:创建二级父工程

创建changgou_gateway、changgou_service、changgou_service_api、changgou_web工程,工程全部为pom工程,并将所有工程的src文件删除。

步骤三:Eureka微服务搭建

创建changgou_eureka模块,并配置pom.xml文件,引入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

 


创建配置文件application.yml,添加如下配置

 

server:
  port: 6868
eureka:
  client:
    register-with-eureka: false #是否将自己注册到eureka中
    fetch-registry: false #是否从eureka中获取信息
    service-url:
      defaultZone: http://127.0.0.1:${server.port}/eureka/

 


创建包com.changgou.eureka ,包下创建启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class);
    }
}

 


步骤四:公共模块搭建

创建全局公共模块changgou_common,并配置pom.xml

<dependencies>
    <!--web起步依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- redis 使用-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.51</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
</dependencies>

 

 

创建com.changgou.entity包 ,包下封装相关公共实体类。

返回结果实体类:

 

package com.changgou.entity;
​
/**
 * 返回结果实体类
 */
public class Result<T> {
​
    private boolean flag;//是否成功
    private Integer code;//返回码
    private String message;//返回消息
private T data;//返回数据
public Result(boolean flag, Integer code, String message, Object data) {
        this.flag = flag;
        this.code = code;
        this.message = message;
        this.data = (T)data;
    }
​
    public Result(boolean flag, Integer code, String message) {
        this.flag = flag;
        this.code = code;
        this.message = message;
    }
​
    public Result() {
        this.flag = true;
        this.code = StatusCode.OK;
        this.message = "执行成功";
    }
​
    //getter and setter..
}
​

 


分页结果类:

 

/**
 * 分页结果类
 */
public class PageResult<T> {
​
    private Long total;//总记录数
    private List<T> rows;//记录
public PageResult(Long total, List<T> rows) {
        this.total = total;
        this.rows = rows;
    }
​
    public PageResult() {
    }
​
    //getter and setter ......
}

 

返回状态码实体类:

/**
 * 返回状态码实体类
 */
public class StatusCode {
​
    public static final int OK=20000;//成功
    public static final int ERROR =20001;//失败
    public static final int LOGINERROR =20002;//用户名或密码错误
    public static final int ACCESSERROR =20003;//权限不足
    public static final int REMOTEERROR =20004;//远程调用失败
    public static final int REPERROR =20005;//重复操作
}

 

 

步骤五:数据访问公共模块搭建

这个公共模块是连接mysql数据库的公共微服务模块,所以需要连接mysql的微服务都继承自此工程。

创建公共模块changgou_common_db,并配置pom文件,引入依赖

<dependencies>
    <dependency>
        <groupId>com.changgou</groupId>
        <artifactId>changgou_common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <!--通用mapper起步依赖-->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>2.0.4</version>
    </dependency>
    <!--MySQL数据库驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!--mybatis分页插件-->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.3</version>
    </dependency>
</dependencies>

 

 

步骤六:商品微服务搭建

1)商品微服务API工程搭建

在changgou_service_api 模块中,引入依赖 feign依赖和java实体类的依赖,在changgou_service_api 下创建changgou_service_goods_api子模块并添加common依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

 


2)商品微服务API工程搭建

在changgou_service工程下创建changgou_service_goods子模块 ,配置pom.xml文件,引入依赖

<dependencies>
    <dependency>
        <groupId>com.changgou</groupId>
        <artifactId>changgou_common_db</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.changgou</groupId>
        <artifactId>changgou_service_goods_api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

 

 

在changgou_service_goods工程中,创建配置文件application.yml

server:
  port: 9011
spring:
  application:
    name: goods
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.200.128:3306/changgou_goods?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: root
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:6868/eureka
  instance:
    prefer-ip-address: true
feign:
  hystrix:
    enabled: true
    
#hystrix 配置
hystrix:
  command:
    default:
      execution:
        timeout:
        #如果enabled设置为false,则请求超时交给ribbon控制
          enabled: true
        isolation:
          strategy: SEMAPHORE

 

 

在changgou_service_goods工程中,com.changgou.goods 包下,创建启动类

@SpringBootApplication
@EnableEurekaClient
@MapperScan(basePackages = {"com.changgou.goods.dao"})
public class GoodsApplication {
    public static void main(String[] args) {
        SpringApplication.run(GoodsApplication.class);
    }
}

 


步骤七:商品微服务测试

商品微服务测试:测试服务是否可以正常的被访问以及操作,

在changgou_service_goods工程中,创建测试controller

 1 package com.changgou.service.goods.controller;
 2  
 3 import org.springframework.web.bind.annotation.GetMapping;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RestController;
 6  
 7 @RequestMapping("/demo")
 8 @RestController  //@controller,@responseBody
 9 public class DemoController {
10  
11     @GetMapping("/test")
12     public String demo(){
13         return "demo message";
14     }
15 }

 


启动changgou_service_goods工程,查看Eureka server 管理后台,发现GOODS服务成功注册到E注册中心,端口号为9011。浏览器访问localhost:9011/demo/test,正常访问,测试通过,商品微服务搭建成功。

posted on 2024-11-21 23:52  努力--坚持  阅读(3)  评论(0编辑  收藏  举报