1. 程式人生 > >SpringCloud 筆記 (一)---- 簡單搭建服務註冊中心與服務,實現服務註冊中心高可用

SpringCloud 筆記 (一)---- 簡單搭建服務註冊中心與服務,實現服務註冊中心高可用

此spring cloud筆記系列都來源於翟永超的spring cloud微服務實戰一書,可自行去下載,我這裡只是記錄一下學習的筆記

此文件有關於服務註冊中心。

快速構建一個服務註冊中心專案

Pom

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version
>
<relativePath/> <!-- lookup parent from repository --> </parent> <!-- spring cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId
>
</dependency> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <!-- 此版本和springboot版本有關,可查官網,我這裡用的springboot1.5,所以用了Dalston -->
<version>Dalston.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

主類

package com.example.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/*
 * 啟動一個服務註冊中心提供給其他應用進行對話
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

預設設定下,該服務註冊中心也會將自己作為客戶端來嘗試註冊它自己
所以我們需要禁用它的客戶端註冊行為

Application.properties

server.port = 1111

eureka.instance.hostname = localhost
# don't register selt in eureka
eureka.client.register-with-eureka = false
# don't search service,only Maintain  service instance
eureka.client.fetch-registry = false
eureka.client.serviceUrl.defaultZone = http://${eureka.instance.hostname}:${server.port}/eureka/

可以發現現在註冊中心還沒有註冊任何服務,現在我們來把一個已有的springboot應用加入到eureka服務治理中。

註冊服務提供者

如果你本地有一個springboot的普通應用,這就再好不過了,改造即可。當然重新構建一個也沒問題。

新增Pom

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

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

        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <!-- 此版本和springboot版本有關,可查官網,我這裡用的springboot1.5,所以用了Dalston -->
                <version>Dalston.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

改造Controller

@RestController
@RequestMapping(value="/user")
public class UserController {
    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient client;

    @RequestMapping("/hello")
    public String greet() {
        ServiceInstance instance = client.getLocalServiceInstance();
//列印服務相關內容
        logger.info("/hello,host:"+instance.getHost()+", service_id:"+instance.getServiceId());
        return "Hello";
    }
}

主類

@EnableDiscoveryClient
@SpringBootApplication
public class ClientServiceApplication {

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

主類中加上此註解@EnableDiscoveryClient,啟用Eureka中的DiscoveryClient實現(自動化配置,建立DiscoveryClient介面針對Eureka客戶端的EnableDiscoveryClient例項),才能實現Controller中對服務資訊的輸出。

Application.properties:

# name the service
spring.application.name = hello-service
# define the register url
eureka.client.serviceUrl.defaultZone = http://localhost:1111/eureka/

高可用註冊中心

Eureka Server的高可用實際上就是將自己作為服務向其他服務註冊中心註冊自己,這樣就可以形成一組互相註冊的服務註冊中心,以實現服務清單的互相同步,達到高可用的效果。下面我們來嘗試搭建高可用的服務註冊中心叢集。

在eureka-server服務註冊中心的基礎之上進行擴充套件,構建一個雙節點的服務註冊中心叢集。

Application.properties:

# don't register selt in eureka
eureka.client.register-with-eureka = false
# don't search service,only Maintain  service instance
eureka.client.fetch-registry = false

建立application-peer1.properties與application-peer2.properties
這裡寫圖片描述

Application-peer1.properties:

spring.application.name=eureka-server
server.port=1111

# double nodes : first--peer1,that directs peer2
eureka.instance.hostname=peer1

eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/

Application-peer2.properties:

server.port = 1112
spring.application.name = eureka-server

# double nodes : second--peer2,that directs peer1
eureka.instance.hostname = peer2

eureka.client.serviceUrl.defaultZone = http://peer1:1111/eureka/

clean專案然後maven clean-maven install
target下此時jar包最新

hosts檔案

需要在本機系統中配置peer1,peer2使系統能通過他們找到ip
windows檔案位置:
這裡寫圖片描述
開啟此檔案配置如下:
127.0.0.1 peer1
127.0.0.1 peer2

我們通過jar命令與profile配置分別啟動peer1與peer2,實際中也可以兩個註冊中心專案,application.properties中分別指向彼此,啟動。
這裡寫圖片描述
這裡寫圖片描述
訪問如下即配置完成:
這裡寫圖片描述
這裡寫圖片描述

現在我們來實驗一下注冊服務,啟動之前需要把註冊地址都填上:
之前的client-service的application.properties中修改註冊地址如下:

# define the register url
eureka.client.serviceUrl.defaultZone = http://peer1:1111/eureka/,http://peer2:1112/eureka/

啟動此服務,重新訪問兩個註冊節點:
這裡寫圖片描述
這裡寫圖片描述

這樣我們兩個註冊中心彼此都有註冊,其中一個dang,另一個仍然可以工作。服務依然可訪問。

如果我們不想使用主機名來定義註冊中心的地址,也可以使用IP地址的形式,但是需要配置檔案中增加配置引數eureka.instance.prefer-ip-address=true,該預設為false。然後當應用程式向eureka註冊時,它將使用IP地址而不是主機名