1. 程式人生 > >SpringCloud(1)服務註冊與發現Eureka

SpringCloud(1)服務註冊與發現Eureka

1.建立1個空白的工程

2.建立2個model工程

一個model(即SpringBoot)工程作為服務註冊中心,即Eureka Server,另一個作為Eureka Client。

Eureka Server建立完後的工程 pom.xml 檔案如下:

<?xml version="1.0" encoding="UTF-8"?>
<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>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR1</spring-cloud.version>
    </properties>

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

</project>

3.啟動服務註冊中心

eureka是一個高可用的元件,它沒有後端快取,每一個例項註冊之後需要向註冊中心傳送心跳(因此可以在記憶體中完成),在預設情況下erureka server也是一個eureka client,必須要指定一個 server。在啟動之前,首先對eureka server配置application.yml檔案.

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

通過 eureka.client.registerWithEureka=false 和 fetchRegistry=false 來表明自己是一個eureka server。

然後再把註解 @EnableEurekaServer 加在springboot工程的啟動application類上

@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaserverApplication.class, args);
    }
}

Eureka Server 是有介面的,啟動工程,開啟瀏覽器訪問 http://localhost:8761即可檢視。

5.建立1個服務提供者

當client向server註冊時,它會提供一些元資料,例如主機和埠,URL,主頁等。Eureka server 從每個client例項接收心跳訊息。 如果心跳超時,則通常將該例項從註冊server中刪除。

建立過程同server類似,建立完pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<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>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.SR1</spring-cloud.version>
    </properties>

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

</project>

通過註解@EnableEurekaClient 表明自己是一個eurekaclient.

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

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

    @Value("${server.port}")
    String port;
    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "hi "+name+",i am from port:" +port;
    }

}

僅僅@EnableEurekaClient是不夠的,還需要在配置檔案中註明自己的服務註冊中心的地址,application.yml配置檔案如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8763
spring:
  application:
    name: service-hi

需要指明spring.application.name,這個很重要,這在以後的服務與服務之間相互呼叫一般都是根據這個name 。
啟動工程,開啟http://localhost:8761 ,即eureka server 的網址,你會發現一個服務已經註冊在服務中了,服務名為SERVICE-HI,埠為7862。

這時開啟 http://localhost:8763/hi?name=forezp ,你會在瀏覽器上看到 :

hi forezp,i am from port:8763

6.構建高可用的Eureka Server叢集

更改 eureka-server 的配置檔案,再分別以 spring.profiles.active=peer1 和 peer2 啟動兩次eureka-serve工程。

spring:
  profiles:
    active: peer1 #peer2

---

spring:
  profiles: peer1
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8762/eureka/

---

spring:
  profiles: peer2
server:
  port: 8762
eureka:
  instance:
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

再啟動 eureka-client 工程。

訪問 http://localhost:8761/ 可以發現,eureka-client 已經向8761埠的 eureka-server 註冊了,並且在DS Replicas選項中顯示了節點127.0.0.1,介面這裡不展示。

這時 eureka-client 工程的配置檔案並沒有向埠為8762的 eureka-server 註冊。訪問 http://127.0.0.1:8762/ 可以發現,eureka-client 也已經向 127.0.0.1:8762 註冊了,可見 localhost:8761 的註冊資訊已經同步到了 127.0.0.1:8762 節點。

也就是說,埠為8761和8762的兩臺 Eureka-server 相互感應,當有服務註冊時,兩個Eureka-eserver是對等的,它們都存有相同的資訊,這就是通過伺服器的冗餘來增加可靠性,當有一臺伺服器宕機了,服務並不會終止,因為另一臺服務存有相同的資料。

參考方誌朋《深入理解Spring Cloud與微服務構建》