您好,登錄后才能下訂單哦!
首先,我們在創建投放系統之前,先看一下我們的工程結構:
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 Server (提供服務的注冊和發現)
Eureka Client
創建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文件
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/
啟動集群測試:
java -jar mscx-ad-discovery.jar --spring.profiles.active=server1
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。