springcloud eureka distributed configuration registry

  A recent study springcloud, do down notes and write down the pit encountered.

1. Create maven engineering structure is very simple, a startup and a configuration file, the following structure shown in FIG.

2. Start the class code below, you need to add the registry notes: EnableEurekaServer

 1 package com.chendb.eureka;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 6 
 7 @SpringBootApplication
 8 @EnableEurekaServer
 9 public class SpringCloudEurekaApplication {
10     
11     public static void main(String[] args) {
12         SpringApplication.run(SpringCloudEurekaApplication.class, args);
13     }
14 }

3. Profiles application.yml the following code, here encountered a pit, the project can be successfully started, but access to the configuration center has reported 404, after inquiry, because of freemarker viewResolver no spring-cloud-netflix-eureka-server- in 2.1.0.RELEASE.jar found in the template, resulting in access to registration centers reported 404, it is necessary to add in the configuration file

spring:

  freemarker:
    prefer-file-system-access: false

Configuring the configuration where a total of three center can be started by replacing each arranged to form a distributed

the Spring: 
  Profiles: 
    the Active: dev1 
  # FreeMarker is not viewResolver in the Spring -cloud-Netflix-Eureka-Server-1.3.1 find the template .RELEASE.jar, leading to access the registry reported 404 
  FreeMarker: 
    the prefer -file-System-Access: false
 
--- 
the Spring: 
  the Application: 
    ### current service name 
    name: Eureka - the peer 
  Profiles: dev 

Server: 
  ## port number 
  port: 10000 

--- 
the Spring: 
  Profiles: dev1 
  the Application: 
    name: Eureka - peer2 
Server: 
  port: 10001 
  
--- 
the Spring:
   Profiles: dev2 
   the Application: 
     name: Eureka - peer3 
Server: 
  Port: 10002 

--- 
Eureka: 
  instance: 
    hostname: dev1 
  Client: 
    ## search services 
    FETCH -registry: false 
    ## whether to register itself to Eureka 
    the Register the -with-Eureka: false 
    ## clients default to this address to find the registry 
    Service - url: 
      defaultzone: HTTP: // localhost: 10000 / Eureka /, HTTP: // localhost : 10001 / Eureka /, HTTP: // localhost : 10002 / Eureka / 
  server:
    ### when the eureka start, you can not get from the cluster nodes to the instance registration information, such as how long it should 
    waitMS--in--time the when-Sync-empty: 0 
    enable -self-Preservation: to true 
    ### Eureka how long the data is updated once 
    the peer -eureka-Nodes-Update-interval The MS-: 100000

3.pom.xml file

<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>

  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>spring-cloud-eureka-server</groupId>
  <artifactId>spring-cloud-eureka-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring-cloud-eureka-server</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
  </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
</project>

4. Run the startup class, visit http: // localhost: 10001 you can, as shown below

5. Configure the client, the client code is structured as follows

6. The client initiates the following class profile

 1 package com.chendb.eureka;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 6 
 7 @SpringBootApplication
 8 @EnableEurekaClient
 9 public class SpringCloudEurekaClient {
10     public static void main(String[] args) {
11         SpringApplication.run(SpringCloudEurekaClient.class, args);
12     }
13 }
View Code
 1 spring:
 2   application:
 3     name: eureka-client
 4   # freemarker的viewResolver没有在spring-cloud-netflix-eureka-server-1.3.1.RELEASE.jar中找到模板,导致访问注册中心报404
 5   freemarker:
 6     prefer-file-system-access: false
 7     
 8 server:
 9   port: 8001
10   
11 eureka:
12   client:
13     service-url:
14       defaultZone: http://localhost:10000/eureka/,http://localhost:10001/eureka/,http://localhost:10002/eureka/
View Code

7.启动后可以看到该客户端已经被注册到服务端

 

Guess you like

Origin www.cnblogs.com/cdblogs/p/11111455.html