[step by step implementation of advertising system in Spring cloud] configuration project structure & Implementation of Eureka service

Keywords: Java Spring Maven Apache xml

Parent project management

First, before creating the delivery system, let's take a look at our engineering structure:

Mscx ad sponsor is our advertising system. For the above structure, we need to create a parent project mscx ad first

To write the pom of the parent project to manage our unified dependency information.

<?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>Distributed advertising system</name>
    <description>Be based on Spring Cloud Alibaba Distributed advertising system implemented</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 Monitoring endpoint management dependencies-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
        <!--Definition Spring Cloud Main version-->
    <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>

    <!--Define remote maven Warehouse-->
    <repositories>
          <!-- Spring Central repository -->
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <!-- Ali Central repository -->
        <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>
        <!--Project compilation plug-in-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Service discovery

Eureka

  • Eureka Server (provides service registration and discovery)
  • Eureka Client

    • Service provider (the service provider registers itself on the server so that Eureka Server can save the metadata of the provider and other service consumers can find the current service)
    • Service consumer (service consumer, obtain the list of registered services from Eureka Server to consume services)

Create project mscx ad discovery, then use the spring boot project Trilogy (add dependency, add annotation, change configuration)

Write POM, focus on relying on 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>Service discovery component</name>
    <description>First use eureka Implementation, to be used later nacos Replace</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>
Add annotation (@ EnableEurekaServer)
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryApplication {
    public static void main(String[] args) {
        SpringApplication.run(DiscoveryApplication.class, args);
    }
}
Modified configuration
Single point
spring:
  application:
    name: ad-discovery-server
server:
  port: 8888
eureka:
  instance:
    hostname: localhost #Single version
  client:
    fetch-registry: false #Get registration information from eureka server or not
    register-with-eureka: false #Register yourself to eureka
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
colony

When testing, you need to modify the hosts file

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

Then modify 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/

Start cluster test:

  • Configure startup profile / java -jar mscx-ad-discovery.jar --spring.profiles.active=server1
  • Effect display

Be a good person.

Posted by r2ks on Tue, 15 Oct 2019 09:06:19 -0700