服務註冊與發現Eureka

Java EE 目錄:https://blog.csdn.net/dkbnull/article/details/87932809
Spring Cloud 專欄:https://blog.csdn.net/dkbnull/column/info/36820

在上一篇文章 Spring Boot整合Spring Cloud 中,我們已經創建了一個服務提供者和服務消費者,但是在消費者調用提供者接口的時候,我們把提供者的地址硬編碼在了消費者代碼裏,這樣我們的代碼極其不優雅,也不利於維護。現在,我們使用Eureka來優雅地實現消費者調用提供者。


0. 開發環境

  • IDE:IntelliJ IDEA 2017.1 x64

  • jdk:1.8.0_91

  • Spring Boot:2.1.1.RELEASE

  • Spring Cloud:Finchley.RELEASE


1. 新建Eureka服務註冊中心

1.1 新建Eureka服務註冊中心

在這裏插入圖片描述

1.2 引入依賴

<?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>

    <artifactId>spring-cloud-eureka</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>cn.wbnull</groupId>
        <artifactId>spring-cloud-demo</artifactId>
        <version>1.0.0</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

1.3 新建application.yml

server:
  port: 8090
  servlet:
    context-path: /springcloudeureka

spring:
  application:
    name: spring-cloud-eureka

eureka:
  instance:
    hostname: localhost
  client:
    #此項目不作爲客戶端註冊
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:${server.port}/springcloudeureka/eureka/

1.4 新建Spring Boot入口類

這裏要注意,入口類增加@EnableEurekaServer註解,表示此項目是Eureka服務端

package cn.wbnull.springcloudeureka;

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

@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {

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

1.5 啓動服務

啓動Eureka服務,瀏覽器訪問 http://127.0.0.1:8090/springcloudeureka/ 可以打開Eureka界面,這裏因爲我們未註冊服務,所以Application是空的。
在這裏插入圖片描述

2. 新建Spring Boot服務提供者

這裏我們直接修改上篇中創建的服務提供者,使其成爲Eureka客戶端

2.1 引入依賴

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2.2 修改application.yml

application.yml 增加如下配置

eureka:
  client:
    #將此項目註冊到Eureka服務
    register-with-eureka: true
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:8090/springcloudeureka/eureka/

2.3 修改Spring Boot入口類

SpringBootProviderApplication 增加註解@EnableEurekaClient,表示此項目是Eureka客戶端

@SpringBootApplication
@EnableEurekaClient
public class SpringBootProviderApplication {

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

2.4 啓動服務

我們先啓動Eureka服務,再啓動該服務提供者,然後再次瀏覽器訪問 http://127.0.0.1:8090/springcloudeureka/ ,服務註冊成功。
在這裏插入圖片描述

3. 再新建一個Spring Boot服務提供者

這裏,我們再創建一個新的Spring Boot服務提供者,可以直接複製上個服務提供者。

複製創建完成後,我們修改一些配置以區別兩個服務提供者

3.1 pom.xml

兩個服務提供者的artifactId不能相同,這裏我們修改第二個服務提供者artifactIdspring-boot-provider-v2

    <artifactId>spring-boot-provider-v2</artifactId>
    <packaging>jar</packaging>

3.2 application.yml

兩個服務提供者的端口不能衝突,這裏我麼修改第二個服務提供者的 server.port=8083,但是spring.application.name=spring-boot-provider,兩個服務提供者名字相同

server:
  port: 8083
  servlet:
    context-path: /springbootprovider

spring:
  application:
    name: spring-boot-provider

3.3 GatewayController

區別兩個服務提供者的返回信息,這裏我們修改第二個服務提供者GatewayController接口返回信息

@RestController
@Scope("prototype")
public class GatewayController {

    @GetMapping(value = "/gateway")
    public String gateway() throws Exception {
        return "hello world,this is spring-boot-provider-v2";
    }
}

3.4 啓動服務

啓動新建的服務提供者,然後再次瀏覽器訪問 http://127.0.0.1:8090/springcloudeureka/ ,如下所示,有兩個spring-boot-provider服務提供者,端口分別是8081,8083
在這裏插入圖片描述

4. 新建Spring Boot服務消費者

回到我們開篇說的,我們上篇創建的服務消費者在調用服務提供者接口時把提供者的地址硬編碼在了消費者代碼裏,現在我們來解決這個問題。

這裏我們也直接修改上篇中創建的服務消費者,使其成爲Eureka客戶端

4.1 引入依賴

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

4.2 修改application.yml

eureka:
  client:
    #將此項目註冊到Eureka服務
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8090/springcloudeureka/eureka/

4.3 修改Spring Boot入口類

SpringBootConsumerApplication增加兩個註解,@EnableEurekaClient表示此項目是Eureka客戶端@LoadBalanced表示開啓負載均衡

package cn.wbnull.springbootconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class SpringBootConsumerApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

4.4 修改GatewayController

將原來硬編碼寫死的服務提供者的地址 http://localhost:8081/springbootprovider/gateway 改成 http://spring-boot-provider/springbootprovider/gateway

spring-boot-provider 就是我們剛纔創建的服務提供者的 spring.application.name,也就是註冊到Eureka中的服務的id。

在Eureka中,我們可以不使用 ip:port 而是直接使用 服務id 去訪問服務。但是這裏要注意,服務id 替換的只是 ip:port,server.servlet.context-path 還是要保留的。

@RestController
@Scope("prototype")
public class GatewayController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping(value = "/gateway")
    public String gateway() throws Exception {
        return restTemplate.getForObject("http://spring-boot-provider/springbootprovider/gateway", String.class);
    }
}

4.5 測試

我們把四個服務依次啓動起來,然後瀏覽器訪問 http://127.0.0.1:8082/springbootconsumer/gateway

不斷刷新幾次,可以看到,接口有時會返回hello world,this is spring-boot-provider,有時會返回hello world,this is spring-boot-provider-v2。這是因爲我們使用**@LoadBalanced**註解開啓了負載均衡。
在這裏插入圖片描述
在這裏插入圖片描述



GitHub:https://github.com/dkbnull/SpringCloudDemo
微信:https://mp.weixin.qq.com/s/L4WVLGh7d6ULl9bP6IdxVg




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章