首先,咱們在建立投放系統以前,先看一下咱們的工程結構:java
mscx-ad-sponsor
就是咱們的廣告投放系統。如上結構,咱們須要首先建立一個Parent Project mscx-ad
spring
來編寫父項目的pom,來管理咱們的統一依賴信息。apache
<?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 Server (提供服務的註冊和發現)app
Eureka Clientmaven
建立project mscx-ad-discovery
, 而後使用SpringBoot項目的三部曲(加依賴,加註解,改配置)分佈式
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文件ide
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-boot
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/
啓動集羣測試:測試
java -jar mscx-ad-discovery.jar --spring.profiles.active=server1