[Spring cloud 一步步实现广告系统] 2. 配置项目结构 & 实现Eureka服务

发布时间:2020-06-15 11:42:04 作者:zhangpan0614
来源:网络 阅读:694

父项目管理

首先,我们在创建投放系统之前,先看一下我们的工程结构:

[Spring cloud 一步步实现广告系统] 2. 配置项目结构 & 实现Eureka服务

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

创建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/

启动集群测试:

推荐阅读:
  1. Spring Cloud切换Ribbon负载均衡模式的方法
  2. 如何在C#与.NET框中利用StringBuilder类操作字符串

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring cloud eureka 服务发现

上一篇:Java RMI是什么

下一篇:window下批处理操作:结束进程kill_S_CRT

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》