项目需求

客户端:针对普通用户,用户登录、用户退出、菜品订购、我的订单。

后台管理系统:针对管理员,管理员登录、管理员退出、添加菜品、查询菜品、修改菜品、删除菜品、订单处理、添加用户、查询用户、删除用户。
image.png

拆分:

  • account 提供账户服务:用户和管理的登录退出。
  • menu 提供菜品服务:添加菜品、删除菜品、修改菜品、查询菜品。
  • order 提供订单服务:添加订单、查询订单、删除订单、处理订单。
  • user 提供用户服务:添加用户、查询用户、删除用户。
  • 分离出一个服务消费者,调用以上四个服务提供者,服务消费者包含了客户端的前端页面和后台接口、后台管理系统的前端页面和后台接口。用户 / 管理员直接访问的资源都保存在服务消费者中,服务消费者根据具体的需求调用四个服务提供者的业务逻辑,通过 Feign 实现负载均衡。

四个服务提供者和一个服务消费者都需要在注册中心进行注册,同时可以使用配置中心来对配置文件进行统一集中管理。
image.png

  • 创建父工程,pom.xml
  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.7.RELEASE</version>
  5. </parent>
  6. <dependencies>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-web</artifactId>
  10. </dependency>
  11. <!-- JDK 9 缺失jar -->
  12. <dependency>
  13. <groupId>javax.xml.bind</groupId>
  14. <artifactId>jaxb-api</artifactId>
  15. <version>2.3.0</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.sun.xml.bind</groupId>
  19. <artifactId>jaxb-impl</artifactId>
  20. <version>2.3.0</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>com.sun.xml.bind</groupId>
  24. <artifactId>jaxb-core</artifactId>
  25. <version>2.3.0</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>javax.activation</groupId>
  29. <artifactId>activation</artifactId>
  30. <version>1.1.1</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.projectlombok</groupId>
  34. <artifactId>lombok</artifactId>
  35. <optional>true</optional>
  36. </dependency>
  37. </dependencies>
  38. <dependencyManagement>
  39. <dependencies>
  40. <dependency>
  41. <groupId>org.springframework.cloud</groupId>
  42. <artifactId>spring-cloud-dependencies</artifactId>
  43. <version>Finchley.SR2</version>
  44. <type>pom</type>
  45. <scope>import</scope>
  46. </dependency>
  47. </dependencies>
  48. </dependencyManagement>

注册中心

  • pom.xml
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    <version>2.0.2.RELEASE</version>
  </dependency>
</dependencies>
  • application.yml
server:
  port: 8761  #当前服务端口
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  #注册中心的访问地址
    register-with-eureka: false #是否将当前的服务作为客户端进行注册--->不注册自己
    fetch-registry: false  #是否要同步其他的注册中心的数据---->这里选择不同步
  • 启动类
package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer   //说明是一个Eureka注册中心
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

配置中心

  • pom.xml
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>2.0.2.RELEASE</version>
  </dependency>
</dependencies>
  • application.yml
server:
  port: 8762
spring:
  application:
    name: configserver
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared
  • 在 shared 路径下创建各个微服务对应的配置文件

client-dev.yml

server:
  port: 8030
spring:
  application:
    name: client
  thymeleaf:
    prefix: classpath:/static/
    suffix: .html
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

menu-dev.yml

server:
  port: 8020
spring:
  application:
    name: menu
  datasource:
    name: orderingsystem
    url: jdbc:mysql://localhost:3306/orderingsystem?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
mybatis:
  mapper-locations: classpath:/mapping/*.xml
  type-aliases-package: com.southwind.entity
  • 启动类
package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer //说明这个是配置中心
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class,args);
    }
}

服务提供者 order

  • pom.xml
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.0.2.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.0.2.RELEASE</version>
  </dependency>
</dependencies>

1 作为服务提供者在注册中心进行注册
2 需要读取配置中心读取文件

  • bootstrap.yml
spring:
  application:
    name: order  #在注册中心的名字
  profiles:
    active: dev  #根据order和dev整合起来 用一个横线去拼接
  cloud:
    config:
      uri: http://localhost:8762  #本地Config Server的访问路径
      fail-fast: true #设置客户端优先判断Config Server获取是否正常
  • Handler
package com.southwind.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderHandler {

    @Value("${server.port}")
    private String port;

    @GetMapping("/index")
    public String index(){
        return "order的端口:"+this.port;
    }
}
  • 启动类
package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

数据库如下
image.png

服务提供者 menu

  • pom.xml
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.0.2.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
  </dependency>

  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.11</version>
  </dependency>

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.0.2.RELEASE</version>
  </dependency>
</dependencies>
  • bootstrap.yml
spring:
  application:
    name: menu #在注册中心的名字
  profiles:
    active: dev #根据menu和dev整合起来 用一个横线去拼接
  cloud:
    config:
      uri: http://localhost:8762
      fail-fast: true #设置客户端优先判断Config Server获取是否正常
  • Handler
package com.southwind.controller;

import com.southwind.entity.Menu;
import com.southwind.repository.MenuRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuHandler {
    @Value("${server.port}")
    private String port;

    @Autowired
    private MenuRepository menuRepository;

    @GetMapping("/index")
    public String index(){
        return this.port;
    }

    @GetMapping("/findAll/{index}/{limit}")
    public List<Menu> findAll(@PathVariable("index") int index,@PathVariable("limit") int limit){
        return menuRepository.findAll(index, limit);
    }
}
  • 启动类
package com.southwind;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.southwind.repository")
public class MenuApplication {
    public static void main(String[] args) {
        SpringApplication.run(MenuApplication.class,args);
    }
}
  • Menu 实体类
package com.southwind.entity;

import lombok.Data;

@Data
public class Menu {
    private long id;
    private String name;
    private double price;
    private String flavor;
}
  • 创建 MenuRepository 接口
package com.southwind.repository;

import com.southwind.entity.Menu;

import java.util.List;

public interface MenuRepository {
    public List<Menu> findAll(int index,int limit);
    public int count();
    public Menu findById(long id);
    public void save(Menu menu);
    public void update(Menu menu);
    public void deleteById(long id);
}
  • resources 路径下创建 mapping 文件夹,存放 Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.southwind.repository.MenuRepository">

    <select id="findAll" resultType="Menu">
        select * from t_menu limit #{param1},#{param2}
    </select>

    <select id="count" resultType="int">
        select count(id) from t_menu
    </select>

    <select id="findById" parameterType="long" resultType="Menu">
        select * from t_menu where id = #{id}
    </select>

    <insert id="save" parameterType="Menu">
        insert into t_menu(name,price,flavor) values(#{name},#{price},#{flavor})
    </insert>

    <update id="update" parameterType="Menu">
        update t_menu set name = #{name},price = #{price},flavor = #{flavor} where id = #{id}
    </update>

    <delete id="deleteById" parameterType="long">
        delete from t_menu where id = #{id}
    </delete>
</mapper>

服务消费者 client

  • pom.xml
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.0.2.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.0.2.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>2.0.2.RELEASE</version>
  </dependency>
</dependencies>
  • bootstrap.yml
spring:
  application:
    name: client
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8762
      fail-fast: true
  • 启动类
package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class,args);
    }
}