springcloud eureka注冊中心分布式配置


  最近在學習springcloud,做下筆記以及記下遇到的坑。

1.建立maven工程,結構很簡單,一個啟動類和一個配置文件,結構如下圖所示

2.啟動類代碼如下,需要添加注冊中心注解: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.配置文件application.yml如下代碼,在這里遇到了一個坑,項目能夠成功啟動,但是訪問配置中心一直報404,經過查詢,是因為freemarker的viewResolver沒有在spring-cloud-netflix-eureka-server-2.1.0.RELEASE.jar中找到模板,導致訪問注冊中心報404,故需要在配置文件加上

spring:

  freemarker:
    prefer-file-system-access: false

這里一共配置了三個配置中心,可通過替換配置分別啟動,形成分布式

spring:
  profiles:
    active: dev1
  # freemarker的viewResolver沒有在spring-cloud-netflix-eureka-server-1.3.1.RELEASE.jar中找到模板,導致訪問注冊中心報404
  freemarker:
    prefer-file-system-access: false

---
spring:
  application:
    ###當前服務名
    name: eureka-peer
  profiles: dev

server:
  ##端口號
  port: 10000

---
spring:
  profiles: dev1
  application:
    name: eureka-peer2
server:
  port: 10001
  
---
spring:
   profiles: dev2
   application:
     name: eureka-peer3
server:
  port: 10002

---
eureka:
  instance:
    hostname: dev1
  client:
    ##檢索服務
    fetch-registry: false
    ##是否將自己注冊到eureka
    register-with-eureka: false
    ##客戶端默認去這個地址找注冊中心
    service-url:
      defaultZone: http://localhost:10000/eureka/,http://localhost:10001/eureka/,http://localhost:10002/eureka/
  server:
    ### 當eureka啟動時,不能從集群節點中獲取到instance注冊信息,應該等多久
    wait-time-in-ms-when-sync-empty: 0
    enable-self-preservation: true
    ###eureka多長時間更新一次數據
    peer-eureka-nodes-update-interval-ms: 100000

3.pom.xml文件

<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.運行啟動類,訪問http://localhost:10001即可,如下圖

5.配置客戶端,客戶端代碼結構如下

6.客戶端啟動類的配置文件如下

 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.啟動后可以看到該客戶端已經被注冊到服務端

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM