1. 程式人生 > >[Spring cloud 一步步實現廣告系統] 2. 配置專案結構 & 實現Eureka服務

[Spring cloud 一步步實現廣告系統] 2. 配置專案結構 & 實現Eureka服務

父專案管理

首先,我們在建立投放系統之前,先看一下我們的工程結構:

UTOOLS1563929285002.png

mscx-ad-sponsor就是我們的廣告投放系統。如上結構,我們需要首先建立一個Parent Project mscx-ad

來編寫父專案的pom,來管理我們的統一依賴資訊。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <modules>
        <module>mscx-ad-discovery</module>
        <module>mscx-ad-zuul</module>
        <module>mscx-ad-gateway</module>
        <module>mscx-ad-discovery-nacos</module>
        <module>mscx-ad-common</module>
        <module>mscx-ad-db</module>
        <module>mscx-ad-sponsor</module>
        <module>mscx-ad-search</module>
        <module>mscx-ad-feign-sdk</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.sxzhongf</groupId>
    <artifactId>mscx-ad</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>分散式廣告系統</name>
    <description>基於Spring Cloud Alibaba 實現的分散式廣告系統</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
       <!--Spring cloud 監控端點管理依賴-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
		<!--定義Spring Cloud 主版本-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--定義遠端maven倉庫-->
    <repositories>
      	<!-- Spring 中央倉庫 -->
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <!-- Ali 中央倉庫 -->
        <repository>
            <id>alibaba-milestones</id>
            <name>ali Milestones</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
		<!--專案編譯外掛-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

服務發現

Eureka

  • Eureka Server (提供服務的註冊和發現)

  • Eureka Client

    • Service provider (服務提供方,將自身註冊到server上,從而讓Eureka Server儲存provider的元資料,讓其他的服務消費者可以找到當前服務)
    • Service Consumer(服務消費方,從Eureka Server上獲取註冊的服務列表,從而消費服務)

    UTOOLS1563949690779.png

建立project mscx-ad-discovery, 然後使用SpringBoot專案的三部曲(加依賴,加註解,改配置)

編寫POM,重點關注依賴spring-cloud-starter-eureka-server
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>mscx-ad</artifactId>
        <groupId>com.sxzhongf</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>jar</packaging>

    <groupId>com.sxzhongf</groupId>
    <artifactId>mscx-ad-discovery</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>服務發現元件</name>
    <description>先使用eureka實現,後續會使用nacos替換掉</description>

    <dependencies>
        <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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
添加註解(@EnableEurekaServer)
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryApplication {
    public static void main(String[] args) {
        SpringApplication.run(DiscoveryApplication.class, args);
    }
}
改配置
單點
spring:
  application:
    name: ad-discovery-server
server:
  port: 8888
eureka:
  instance:
    hostname: localhost #單機版
  client:
    fetch-registry: false #是否從eureka server獲取註冊資訊
    register-with-eureka: false #註冊自己到eureka
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
叢集

測試的時候,需要修改hosts檔案

zhangpandeMacBook-Pro:etc zhangpan$ cat hosts
##
...
##
127.0.0.1	localhost
127.0.0.1	server1
127.0.0.1	server2
127.0.0.1	server3
::1             localhost

然後修改application.yml

spring:
  application:
    name: ad-discovery
  profiles: server1
server:
  port: 7777
eureka:
  instance:
    hostname: server1
    prefer-ip-address: false
  client:
    service-url:
      defaultZone: http://server2:8888/eureka/,http://server3:9999/eureka/

---
spring:
  application:
    name: ad-discovery
  profiles: server2
server:
  port: 8888
eureka:
  instance:
    hostname: server2
    prefer-ip-address: false
  client:
    service-url:
      defaultZone: http://server1:7777/eureka/,http://server3:9999/eureka/

---
spring:
  application:
    name: ad-discovery
  profiles: server3
server:
  port: 9999
eureka:
  instance:
    hostname: server3
    prefer-ip-address: false
  client:
    service-url:
      defaultZone: http://server2:8888/eureka/,http://server1:7777/eureka/

啟動叢集測試:

  • 配置啟動profile / java -jar mscx-ad-discovery.jar --spring.profiles.active=server1 UTOOLS1563934587785.png
  • 效果展示 UTOOLS1563934714568.png