Fork me on GitHub

[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
posted @ 2019-07-26 07:42  IsaacZhang  阅读(881)  评论(5编辑  收藏  举报