您的位置:首页 > 编程语言 > Java开发

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

2019-07-29 23:25 1416 查看

在前面的过程中,我们创建了4个project:

服务发现

我们使用Eureka 作为服务发现组件,学习了

Eureka Server
,
Eureka Client
的使用。

  • Eureka Server

      加依赖
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <!--<artifactId>
    5b4
    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 项目三部曲,我们可以快速添加一个新组件,并正常使用

  • Nacos Server

    这个我没有在项目中实现,但是大家可以和Eureka一样,三部曲搞定。

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

      在早期版本中,我们需要添加

      @EnableDiscoveryClient
      ,但是在nacos 0.9之后,不需要我们显示的添加注解了~,因此这步可以忽略。

    2. 改配置
    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(服务容错以及流控管理)

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐