SpringCloud+SpringBoot+Vue

一、任务要求

  1. 基于任务二 SpringBoot+Vue 的 demo 进行微服务拆分
  2. 使用 Consul 作为注册中心
  3. 使用 Zuul 作为网关

二、具体实施

  1. 构思将任务二 Demo 的后台工程,划分为几个微服务模块。
  2. 将几个微服务模块和网关模块,注册到注册中心

【依赖导入】

<!-- consul服务发现和配置 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- consul服务发现和配置结束 -->

【配置抒写】

spring:
  application:
    name: xxx
  cloud:
    consul: #使用consul做服务配置和发现
      host: localhost
      port: 8500
      discovery:
        instanceId: ${spring.application.name}

# 网关配置
zuul:
  routes:
    aaa: /a/**    # 给名为 aaa 的服务,设置映射地址 a
    bbb: /b/**    # 给名为 bbb 的服务,设置映射地址 b
  1. 使用 Feign 实现微服务之间的调用

/**
 *  调用 xxx 微服务
 */
@FeignClient("xxx") // 要访问的服务
public interface DeptMajorFeign {

    /**
     *  调用 xxx 的微服务中的接口查询院系信息!
     * @return
     */
    @GetMapping("/xxx/yyyy")
    public List<Department> deptList();

}
  1. 请求全部走向网关,由网关分发

三、总结

边学边做的任务,入门菜鸡的使用感触:微服务的拆分,可以让后端在工程的角度,进一步的解耦,便于他人的阅读、上手、开发、维护的同时,也便于团队开发的分工合作,而且更重要的是项目在 N 分为 1 后,其中一些服务挂了,并不会导致整个项目也挂掉,提高了可用性!

猜你喜欢

转载自www.cnblogs.com/xitingfeng/p/13399250.html