[Spring Cloud]Eureka+OpenFeign

前言

在畢業之前我還沒比較系統地去了解SpringCloud生態,只用過Dubbo和Zookeeper來實現幾個簡單的服務來調用玩一玩,後來逐漸瞭解到SpringCloud生態,其實嚴格來講把Dubbo和Zookeeper和SpringCloud放在一起比較並不合適,因爲Dubbo是一個個rpc框架,Zookeeper是註冊中心,而這兩個只是SpringCloud整套中間的兩個組成部分。在SpringCloud生態中有Eureka來當註冊中心,有OpenFeign來做服務間的調用,值得一提的是dubbo是RPC,OpenFeign是Http。這裏貼一張網上找到的Dubbo和SpringCloud的對比。
avatar
所以今天結合Eureka和OpenFeign做一個簡單的消費者服務調用生產者服務的小Demo。

Eureka

跟Zookeeper不同的是,Eureka沒有安裝包,而是以集成服務中來起作用,所以在創建父工程之後單獨創建一個Eureka註冊中心模塊來提供給生產者註冊。
Eureka服務的pom文件需要加入Eureka服務端的依賴,除此之外就是一些常用的SpringBoot依賴。

 <dependencies>
        <!-- eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!--web-->
        <!--actuator-->
        <!-- lombok -->
        ......
    </dependencies>

然後就是application.yml了,這裏面規定服務端口,Eureka的基本設置等。

server:
  port: 7001

eureka:
  instance:
    hostname: localhost
  client:
  # 不向註冊中心註冊自己
    register-with-eureka: false
  # 表示自己端是註冊中心
    fetch-registry: false
    service-url:
  # 設置與Eureka Server交互的地址查詢服務和註冊服務需要依賴這個地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
  # 關閉自我保護的時間
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 2000

之後就是主啓動類加上@SpringBootApplication
和@EnableEurekaServer就行,因爲這個服務只作爲註冊中心,所以也沒有其他業務類,到這裏註冊中心就搞定了。

Provider生產者端

與上面Eureka服務端不同的是,生產者端需要加入Eureka客戶端的依賴。

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

Application.yml

這裏需要定義工程名以便消費者能夠它來進行服務調用,除此之外就是定義Eureka的配置。

server:
  port: 8001


spring:
  application:
    name: example-service

eureka:
  instance:
    instance-id: example-service-8001
    prefer-ip-address: true
    # Eureka客戶端向服務端發送心跳包的時間間隔
    lease-renewal-interval-in-seconds: 1
    # Eureka服務端在收到最後一次心跳包後等待時間上限
    lease-expiration-duration-in-seconds: 2
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

爲了簡單,生產者端不寫DAO和Service層了,只有Controller層,返回一個簡單的"Hello world",所以定義一個Controller類。

@Controller
public class OpenFeignController {

    @GetMapping("/example/openfeign")
    public String helloworld(){
        return "hello world";
    }

主啓動類加上@SpringBootApplication
和@EnableEurekaClient,隨後啓動該服務,檢查Eureka頁面是否有該服務註冊。
avatar

Consumer消費者端

消費者端主要是需要調用剛纔的/example/openfeign接口,所以除了Eureka的依賴,還需要OpenFeign的依賴。

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

在這之後,既然有了註冊中心,那麼就要告訴消費者端註冊中心的地址,自然就要加入yml的配置。

Application.yml

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://localhost:7001/eureka
......

然後就是最重要的Service層了,這裏面實現調用生產者接口。

@Component
@FeignClient(value = "EXAMPLE-SERVICE")
public interface ExampleService {
    @GetMapping("/example/openfeign")
    public String helloworld();
}

然後寫Controller層調用Service層的helloworld方法。

@RestController
public class ExampleFeignController {

    @Resource
    private ExampleService exampleService;

    @GetMapping("/example/openfeign")
    public String helloworld(){
        return exampleService.helloworld();
    }
}

最後,在這個服務的主啓動類中加入@SpringBootApplication
@EnableFeignClients註解來啓用OpenFeign。

測試消費者端

在瀏覽器輸入http://localhost/example/openfeign,返回helloworld即測試完成。
avatar

相關資料

跟着尚硅谷的視頻學的,當做入門個人覺得還不錯(不是打廣告)
我的個人小站同步博客(這個算廣告)

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