1. 程式人生 > >基於spring-cloud-eureka 服務中心與註冊

基於spring-cloud-eureka 服務中心與註冊

本文基於Finchley.SR2以上,Edgware.SR5的配置部分不適用
spring-boot版本為2.0.2RELEASE, 用部分設定1.5已不適用

伺服器端設定

伺服器pom檔案設定

加入依賴 spring-cloud-starter-netflix-eureka-server

<?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> <packaging>jar</packaging> <artifactId>eureka-server</artifactId> <properties> <project.build.sourceEncoding
>
UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <file.encoding>UTF8</file.encoding> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <parent> <groupId>org.springframework.boot</
groupId
>
<artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

加入@EurekaServer,開啟為註冊服務中心

package top.littlematch.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

}

配置檔案

#服務埠
server.port=8881
#名稱
spring.application.name=server-eureka
#服務名稱
eureka.instance.hostname=server-eureka
#eureka是一個高可用的元件,它沒有後端快取,每一個例項註冊之後需要向註冊中心傳送心跳(因此可以在記憶體中完成),
# 在預設情況下eureka server也是一個eureka client
# 通過eureka.client.registerWithEureka:false和fetchRegistry:false來表明自己是一個eureka server

#禁止本身註冊
eureka.client.register-with-eureka=false
#禁止本身註冊
eureka.client.fetch-registry=false
#服務中心地址
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

訪問瀏覽器 http://localhost:8881/
檢視註冊中心

客戶端設定

客戶端pom檔案設定

增加spring-cloud-starter-netflix-eureka-client,註冊為eureka客戶端;

為了測試方便加入spring-boot-starter-web與spring-boot-starter-test

<?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>
    <packaging>jar</packaging>
    <artifactId>eureka-server</artifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <file.encoding>UTF8</file.encoding>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

    <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-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</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>

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

客戶端啟動類設定

加入@EnableEurekaClient註冊為eureka客戶端,加入@RestController用於客戶端測試

package top.littlematch.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @RequestMapping(value = "hello")
    public String hello(String name){
        return "hello! " + name;
    }
}

客戶端配置文字設定

server.port=8882
spring.application.name=eureka-client
#註冊到eureka-server,埠是服務端埠
eureka.client.service-url.defaultZone=http://localhost:8881/eureka/

訪問瀏覽器
http://localhost:8882/hello?name=match

返回

hello! match

修改eureka-client的埠為8883,然後再啟動一個,然後就可以得到多個服務了,同時實現了負載均衡