天天看點

eureka服務端和用戶端的簡單搭建

     本篇部落格簡單記錄一下,eureka 服務端和 用戶端的簡單搭建。

目标:

    1、完成單機 eureka server 和 eureka client 的搭建。

    2、完成eureka server 的添加安全認證,即不能别人知道我們的eureka server位址就可以注冊上去。

    3、測試環境下,關閉eureka的自我保護

一、eureka server 端的搭建

1、引入依賴

<dependencies>
        <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-security</artifactId>
        </dependency>

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

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

    注意:

           1、由于服務端需要保護,是以還引入了security依賴。

2、編寫配置檔案

spring:
  application:
    name: eureka-server


server:
  port: 8761
  tomcat:
    uri-encoding: utf-8
eureka:
  client:
    register-with-eureka: false # 由于eureka即可以作為服務端也可以作為用戶端,此處是作為伺服器段,是以這個參數需要設定成false: 即不作為一個用戶端注冊到服務注冊中心
    fetch-registry: false # true:表示作為一個用戶端中eureka server 服務端擷取服務注冊資訊,此處作為一個服務端是以需要設定成 false
    service-url:
      defaultZone : http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
  instance:
    hostname: localhost
  server:
    # 此處表示關閉 eureka 的自我保護
    enable-self-preservation: false
    # 清理無效節點的時間間隔,預設是60s,此處修改成10s
    eviction-interval-timer-in-ms: 10000

security:
  basic:
    enabled: true # 開啟basic認證
  user:
    name: root # 使用者名
    password: admin # 密碼      

   注意:

          1、預設情況下eureka即可以做為服務端,也可以做為用戶端,此處作為服務端,是以需要将 register-with-eureka的值改成false,即不注冊到eureka server上。

           2、fetch-registry: 服務端這個值 需要改成 false, 即不去檢索服務。

           3、security 開頭的配置是因為引入了spring security保護server端,是以 需要注意 service-url 中的 defaultZone 的值的寫法 : http://使用者名:密碼@主機:端口/eureka/

           4、enable-self-preservation的值設定成 false 表示 關閉eureka的自我保護。

                     用戶端需要修改下方2個參數的值,正式環境不建議修改。

# 用戶端與伺服器斷心跳的時間間隔,預設為 30秒
            lease-renewal-interval-in-seconds: 3
            # 租約到期時間,此值不可過小,開發環境小點沒有事,預設為 90秒
            lease-expiration-duration-in-seconds: 9      

3、編寫啟動類

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

  public static void main(String[] args) {
    SpringApplication.run(EurekaServerApplication.class, args);
  }
}      

   注意:

         1、 eureka server 的服務端上需要加上  @EnableEurekaServer 注解,表示作為服務端啟動。

 二、eureka client 端的搭建

1、引入依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

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

 2、編寫配置檔案

server:
  port: 8762


eureka:
  client:
    service-url:
      defaultZone : http://${security.user.name}:${security.user.password}@localhost:8761/eureka/   #連接配接到服務注冊中心的位址,如果服務注冊中心開啟了權限需要設定 http://username:password@ip:port/eureka/格式
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}

    # 用戶端與伺服器斷心跳的時間間隔,預設為 30秒
    lease-renewal-interval-in-seconds: 3
    # 租約到期時間,此值不可過小,開發環境小點沒有事,預設為 90秒
    lease-expiration-duration-in-seconds: 9

security:
  user:
    name: root
    password: admin
spring:
  application:
    name: eureka-client


info:
  app:
    name: "eureka-client"
    description: "eureka-client程式"
    version: "0.0.1"      

    注意:

           1、注意一下注冊到 eureka server 上 url 的編寫格式。

           2、spring.application.name 表示注冊到eureka服務上的名字,建議小寫。

 3、編寫啟動類

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

  public static void main(String[] args) {
    SpringApplication.run(EurekaClientApplication.class, args);
  }
}      

    注意:

           1、eureka client需要加上 @EnableDiscoveryClient 注解,表示這個一個用戶端。

三、運作界面

 四、完整代碼位址: