Spring Cloud:第二章:eureka服務(wù)發(fā)現(xiàn)

服務(wù)注冊中心 :eureka-server

新建一個springboot項目:eureka-server,其pom.xml配置如下:

  <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
          <java.version>1.8</java.version>
      </properties>
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-eureka-server</artifactId>
         </dependency>
     </dependencies>
     <dependencyManagement>
         <dependencies>
             <dependency>
                 <groupId>org.springframework.cloud</groupId>
                 <artifactId>spring-cloud-dependencies</artifactId>
                 <version>Dalston.SR1</version>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
         </dependencies>
     </dependencyManagement>

想要實現(xiàn)一個服務(wù)注冊中心的功能非常簡單,只需要在項目的啟動類EurekaServerApplication上使用@EnableEurekaServer注解即可

1 @EnableEurekaServer
2 @SpringBootApplication
3 public class EurekaServerApplication{
4 
5     public static void main(String[] args) {
6         new SpringApplicationBuilder(EurekaServerApplication.class)
7                     .web(true).run(args);
8     }
9 }

默認(rèn)情況下,該服務(wù)注冊中心也會將自己作為客戶端來嘗試注冊它自己,所以我們需要禁用它的客戶端注冊行為,只需要在application.properties配置文件中增加如下信息:

1 spring.application.name=eureka-server
2 server.port=1001
3 eureka.instance.hostname=localhost
4 eureka.client.register-with-eureka=false
5 eureka.client.fetch-registry=false

啟動EurekaServerApplication,訪問 http://localhost:9001/可以看到Eureka的頁面,從紅框的位置可以看到?jīng)]有任務(wù)服務(wù)實例注冊到當(dāng)前的服務(wù)注冊中心
在這里插入圖片描述

服務(wù)提供方 :eureka-client

每一個實例注冊之后需要向注冊中心發(fā)送心跳,當(dāng)client向server注冊時,它會提供一些元數(shù)據(jù),例如主機和端口,URL,主頁等。Eureka server 從每個client實例接收心跳消息。 如果心跳超時,則通常將該實例從注冊server中刪除。

新建一個springboot項目:eureka-client,其pom.xml配置如下:

<properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
              <java.version>1.8</java.version>
          </properties>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-starter-eureka</artifactId>
             </dependency>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-web</artifactId>
             </dependency>
         </dependencies>
         <dependencyManagement>
             <dependencies>
                 <dependency>
                     <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                     <version>Dalston.SR1</version>
                     <type>pom</type>
                     <scope>import</scope>
                 </dependency>
             </dependencies>
         </dependencyManagement>

想要實現(xiàn)一個服務(wù)提供方也很簡單,只要在項目的啟動類EurekaClientApplication上使用@EnableEurekaClient注解即可

 1 @EnableEurekaClient
 2 @SpringBootApplication
 3 public class EurekaClientApplication {
 4 
 5      public static void main(String[] args) {
 6             new SpringApplicationBuilder(
 7                     EurekaClientApplication.class)
 8                 .web(true).run(args);
 9         }
10 }

在application.properties中進(jìn)行如下配置

spring.application.name=eureka-client
server.port=9002
eureka.client.serviceUrl.defaultZone=http://localhost:9001/eureka/

通過spring.application.name屬性,我們可以指定微服務(wù)的名稱后續(xù)在調(diào)用的時候只需要使用該名稱就可以進(jìn)行服務(wù)的訪問。

eureka.client.serviceUrl.defaultZone屬性對應(yīng)服務(wù)注冊中心的配置內(nèi)容,指定服務(wù)注冊中心的位置。

使用server.port屬性設(shè)置不同的端口。

啟動EurekaClientApplication類

刷新 http://localhost:9001/,可以看到咱們的服務(wù)提供方已經(jīng)注冊到了服務(wù)注冊中心

在這里插入圖片描述

在新建一個DiscoveryController

使用discoveryClient.getServices()獲取已經(jīng)注冊的服務(wù)名,使用@value將配置文件中的信息賦值到ip

@RestController
public class DiscoveryController {
    
    @Autowired
    private DiscoveryClient discoveryClient;
    @Value("${server.port}")
    private String ip;
    
    @GetMapping("/client")
    public String client() {
        String services = "Services: " + discoveryClient.getServices()+" ip :"+ip;
        
        System.out.println(services);
        return services;
    }
}

訪問:http://localhost:9002/client

在這里插入圖片描述










最后說明一下@EnableEurekaClient 與@EnableDiscoveryClient這兩個注解

首先這個兩個注解都可以實現(xiàn)服務(wù)發(fā)現(xiàn)的功能,在spring cloud中discovery service有許多種實現(xiàn)(eureka、consul、zookeeper等等)

@EnableEurekaClient基于spring-cloud-netflix。服務(wù)采用eureka作為注冊中心,使用場景較為單一。

@EnableDiscoveryClient基于spring-cloud-commons。服務(wù)采用其他注冊中心。