[Spring cloud 一步步实现广告系统] 7. 中期总结回顾

在前面的过程当中,咱们建立了4个project:html

服务发现

咱们使用Eureka 做为服务发现组件,学习了Eureka Server,Eureka Client的使用。java

  • Eureka Servermysql

    1. 加依赖
    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <!--<artifactId>spring-cloud-netflix-eureka-server</artifactId>-->
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.2.7.RELEASE</version>
        </dependency>
    1. 加注解
    @SpringBootApplication
    @EnableEurekaServer
    public class DiscoveryApplication {
        public static void main(String[] args) {
            SpringApplication.run(DiscoveryApplication.class, args);
        }
    }
    1. 改配置
    eureka:
      instance:
        hostname: server1
        prefer-ip-address: false
      client:
        service-url:
          defaultZone: http://server2:8888/eureka/,http://server3:9999/eureka/

使用Sprint Boot 项目三部曲,咱们能够快速添加一个新组件,并正常使用git

  • Nacos Servergithub

    这个我没有在项目中实现,可是你们能够和Eureka同样,三部曲搞定。spring

    1. 加依赖(因SC Alibaba即将毕业影响,会从Spring-Cloud家族依赖中移动到alibaba repository下,所以,你们在学习依赖的时候,必定要注意版本信息,github传送门)
    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.9.0.RELEASE</version>
        </dependency>
    1. 加注解sql

      在早期版本中,咱们须要添加@EnableDiscoveryClient,可是在nacos 0.9以后,不须要咱们显示的添加注解了~,所以这步能够忽略。数据库

    2. 改配置api

    spring: 
      cloud:
          nacos:
            discovery:
              server-addr: localhost:8848 #前提是要启动Nacos Server
              metadata:
                version: v1
              # 指定namespace(profile)
              #namespace: 404060ce-2e6c-4f72-8083-2beb4ca921ad
              # 指定集群名称
              cluster-name: BJ

    Nacos Server ,请你们自行搜索,可参考 Nacos Github负载均衡

网关路由

  1. 加依赖(由于网关也须要注册到服务发现上,所以它也是一个client,那么须要引入spring-cloud-starter-netflix-eureka-client)
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
  1. 加注解
/**
* @SpringCloudApplication 是如下三个注解的组合注解

* @see SpringBootApplication // 标柱是Spring Boot 项目启动
* @see EnableDiscoveryClient // 标柱为服务发现 client,引入Eureka依赖以后 等同于 @EnableEurekaClient
* @see EnableCircuitBreaker // 断路器,后续咱们会讲解
*/
@SpringCloudApplication
@EnableZuulProxy //启动网关代理服务
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}
  1. 改配置
zuul:
#  ignored-services: '*' # 过滤全部请求,除了下面routes中声明过的服务
  routes:
    sponsor: #在路由中自定义服务路由名称
      path: /ad-sponsor/**
      serviceId: mscx-ad-sponsor #微服务name
      strip-prefix: false
    search: #在路由中自定义服务路由名称
      path: /ad-search/**
      serviceId: mscx-ad-search #微服务name
      strip-prefix: false
  prefix: /gateway/api
  strip-prefix: true #不对 prefix: /gateway/api 设置的路径进行截取,默认转发会截取掉配置的前缀

具体的代码,参考源代码实现。

通用代码库

这个其实你们就能够看成是本项目内的工具类就好了,没什么特殊的需求。

广告投放系统

该项目中,咱们使用到的技术有:

  1. mysql 8
  2. Eureka client
  3. 代码与数据库的交互ORM jpa
  4. flyway(数据库版本管理工具)

后续咱们要添加的技术

  1. Feign(微服务相互调用)
  2. Ribbon(调用的客户端负载均衡)
  3. hystrix(服务容错以及流控管理)

每一种技术都有一套完整的实现以及框架,想要深刻学习的同窗请自行索引,后期广告系统结束以后,我会另起一个系列来和你们一块儿讨论框架底层实现。

相关文章
相关标签/搜索