spring cloud 2.x版本 Eureka Client服務提供者教程

本文采用Spring cloud本文爲2.1.8RELEASE,version=Greenwich.SR3 java

1 建立eureka client

1.1 新建Srping boot工程:eureka-client

1.2 pom.xml所須要依賴的jar包

<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>
複製代碼

1.3 EurekaClientApplication添加註解@EnableEurekaClient

package spring.cloud.demoo.eurekaclient;

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

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {

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

}
複製代碼

@EnableEurekaClient 向eureka服務中心註冊服務(推薦使用),若是是其餘服務註冊中心,例如:consul、zookeeper,建議使用@EnableDiscoveryClientgit

1.4 添加application.yml配置內容、

spring:
 application:
 name: eureka-client
server:
 port: 8801

eureka:
 instance:
 hostname: localhost
    # 表示eureka client間隔多久去拉取服務註冊信息,默認爲30秒,若是要迅速獲取服務註冊狀態,能夠縮小該值
 lease-renewal-interval-in-seconds: 5
    # 表示eureka server至上一次收到client的心跳以後,等待下一次心跳的超時時間,在這個時間內若沒收到下一次心跳,則將移除該instance。
    # 默認爲90秒
    # 若是該值太大,則極可能將流量轉發過去的時候,該instance已經不存活了。
    # 若是該值設置過小了,則instance則極可能由於臨時的網絡抖動而被摘除掉。
    # 該值至少應該大於 leaseRenewalIntervalInSeconds
 lease-expiration-duration-in-seconds: 10
 client:
 service-url:
 defaultZone: http://localhost:8701/eureka/
複製代碼

配置中http://localhost:8701/eureka/ 調用eureka-server,能夠參考: eureka-server註冊中心搭建github

1.5 建立測試controller:EurekaClientController

package spring.cloud.demoo.eurekaclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/** * @auther: maomao * @DateT: 2019-09-17 */
@RestController
public class EurekaClientController {

    @Value("${server.port}")
    private String port;

    @RequestMapping("/info")
    public String syaHello(HttpServletRequest request) {
        String message = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getServletPath();
        return message;
    }

}
複製代碼

1.6 啓動eureka-client服務

訪問http://localhost:8801/info,並返回結果,以下圖所示。 web

這時在訪問服務註冊中心,若是下入所示:spring

能夠看到eureka-client已經註冊到服務註冊中心。

至此,一個簡單的單機eureka client服務提供者就搭建完成。服務的提供者提供一個Restful服務。bash

彩蛋

eureka client集羣的搭建

1.1 配置本地host文件

127.0.0.1		eureka1.client.com
127.0.0.1		eureka2.client.com
127.0.0.1		eureka3.client.com
複製代碼

1.2 增長application.yml配置文件

增長application-clinet1.yml和application-clinet2.yml文件,修改原application.yml文件。網絡

  • application.yml
spring:
 application:
 name: eureka-client
server:
 port: 8801

eureka:
 instance:
 hostname: eureka1.client.com
 lease-renewal-interval-in-seconds: 5
 lease-expiration-duration-in-seconds: 10
 client:
 service-url:
 defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
複製代碼
  • application-client1.yml
spring:
 application:
 name: eureka-client
server:
 port: 8802

eureka:
 instance:
 hostname: eureka2.client.com
 lease-renewal-interval-in-seconds: 5
 lease-expiration-duration-in-seconds: 10
 client:
 service-url:
 defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
複製代碼
  • application-client2.yml
spring:
 application:
 name: eureka-client
server:
 port: 8803

eureka:
 instance:
 hostname: eureka3.client.com
 lease-renewal-interval-in-seconds: 5
 lease-expiration-duration-in-seconds: 10
 client:
 service-url:
 defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
複製代碼

1.3 分別啓動eureka-client三個服務

分別訪問http://eureka1.client.com:8801/info、eureka2.client.com:8802/info、 eureka3.client.com:8803/info 顯示結果以下:app

此時打開服務註冊中心,顯示

截圖中紅框中表明三個client已經註冊成功。分佈式

總結

本文簡單實現了服務提供者單機和集羣的搭建,後續繼續更新其餘關於spring cloud其餘內容。spring-boot

代碼地址

gitHub地址


《Srping Cloud 2.X小白教程》目錄

轉載請註明出處,

  • 聯繫方式:4272231@163.com
相關文章
相關標籤/搜索