Eureka服務治理 - 搭建簡單應用

創建Eureka註冊中心服務器

關鍵依賴:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </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>

yml配置文件: 

server:
  port: 9100  #端口號

eureka:
  instance:
    hostname: localhost #註冊中心地址
  client:
    register-with-eureka: false #是否將自己註冊到註冊中心
    fetch-registry: false #是否從註冊中心檢索服務
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

在啓動類中使用@EnableEurekaServer註解開啓註冊中心服務:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

啓動應用在瀏覽器訪問http://localhost:8100:

註冊服務

創建一個用戶服務app-user,註冊到Eureka註冊中心。

關鍵依賴:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.9.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>

<dependencies>
    <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-client</artifactId>
    </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>

yml配置文件:

server:
  port: 8010

spring:
  application:
    name: app-user  #服務別名-註冊到註冊中心之後的名稱

eureka:
  client:
    register-with-eureka: true  #註冊到註冊中心
    fetch-registry: true  #檢索其他服務
    serviceUrl:
      defaultZone: http://localhost:9100/eureka/  #註冊目標地址


在啓動類中使用@EnableEurekaClient將用戶服務註冊到Eureka註冊中心:

@SpringBootApplication
@EnableEurekaClient
public class UserServerApplication {

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

}

再次打開註冊中心頁面http://localhost:8100,就可以看到app-user服務已經被註冊進來了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章