[Spring cloud 一步步實(shí)現(xiàn)廣告系統(tǒng)] 7. 中期總結(jié)回顧

在前面的過程中,我們創(chuàng)建了4個(gè)project:

服務(wù)發(fā)現(xiàn)

我們使用Eureka 作為服務(wù)發(fā)現(xiàn)組件,學(xué)習(xí)了Eureka Server,Eureka Client的使用。

  • Eureka Server

    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 項(xiàng)目三部曲,我們可以快速添加一個(gè)新組件,并正常使用

  • Nacos Server

    這個(gè)我沒有在項(xiàng)目中實(shí)現(xiàn),但是大家可以和Eureka一樣,三部曲搞定。

    1. 加依賴(因SC Alibaba即將畢業(yè)影響,會(huì)從Spring-Cloud家族依賴中移動(dòng)到alibaba repository下,因此,大家在學(xué)習(xí)依賴的時(shí)候,一定要注意版本信息,github傳送門)
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-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 #前提是要啟動(dòng)Nacos Server
              metadata:
                version: v1
              # 指定namespace(profile)
              #namespace: 404060ce-2e6c-4f72-8083-2beb4ca921ad
              # 指定集群名稱
              cluster-name: BJ
    

    Nacos Server ,請(qǐng)大家自行搜索,可參考 Nacos Github

網(wǎng)關(guān)路由

  1. 加依賴(因?yàn)榫W(wǎng)關(guān)也需要注冊(cè)到服務(wù)發(fā)現(xiàn)上,因此它也是一個(gè)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 是以下三個(gè)注解的組合注解

* @see SpringBootApplication // 標(biāo)柱是Spring Boot 項(xiàng)目啟動(dòng)
* @see EnableDiscoveryClient // 標(biāo)柱為服務(wù)發(fā)現(xiàn) client,引入Eureka依賴之后 等同于 @EnableEurekaClient
* @see EnableCircuitBreaker // 斷路器,后續(xù)我們會(huì)講解
*/
@SpringCloudApplication
@EnableZuulProxy //啟動(dòng)網(wǎng)關(guān)代理服務(wù)
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}
  1. 改配置
zuul:
#  ignored-services: '*' # 過濾所有請(qǐng)求,除了下面routes中聲明過的服務(wù)
  routes:
    sponsor: #在路由中自定義服務(wù)路由名稱
      path: /ad-sponsor/**
      serviceId: mscx-ad-sponsor #微服務(wù)name
      strip-prefix: false
    search: #在路由中自定義服務(wù)路由名稱
      path: /ad-search/**
      serviceId: mscx-ad-search #微服務(wù)name
      strip-prefix: false
  prefix: /gateway/api
  strip-prefix: true #不對(duì) prefix: /gateway/api 設(shè)置的路徑進(jìn)行截取,默認(rèn)轉(zhuǎn)發(fā)會(huì)截取掉配置的前綴

具體的代碼,參考源代碼實(shí)現(xiàn)。

通用代碼庫(kù)

這個(gè)其實(shí)大家就可以當(dāng)作是本項(xiàng)目?jī)?nèi)的工具類就行了,沒什么特殊的需求。

廣告投放系統(tǒng)

該項(xiàng)目中,我們使用到的技術(shù)有:

  1. mysql 8
  2. Eureka client
  3. 代碼與數(shù)據(jù)庫(kù)的交互ORM jpa
  4. flyway(數(shù)據(jù)庫(kù)版本管理工具)

后續(xù)我們要添加的技術(shù)

  1. Feign(微服務(wù)相互調(diào)用)
  2. Ribbon(調(diào)用的客戶端負(fù)載均衡)
  3. hystrix(服務(wù)容錯(cuò)以及流控管理)

每一種技術(shù)都有一套完整的實(shí)現(xiàn)以及框架,想要深入學(xué)習(xí)的同學(xué)請(qǐng)自行索引,后期廣告系統(tǒng)結(jié)束之后,我會(huì)另起一個(gè)系列來和大家一起討論框架底層實(shí)現(xiàn)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容