[Spring Cloud]Eureka+OpenFeign
前言
在畢業之前我還沒比較系統地去了解SpringCloud生態,只用過Dubbo和Zookeeper來實現幾個簡單的服務來呼叫玩一玩,後來逐漸瞭解到SpringCloud生態,其實嚴格來講把Dubbo和Zookeeper和SpringCloud放在一起比較並不合適,因為Dubbo是一個個rpc框架,Zookeeper是註冊中心,而這兩個只是SpringCloud整套中間的兩個組成部分。在SpringCloud生態中有Eureka來當註冊中心,有OpenFeign來做服務間的呼叫,值得一提的是dubbo是RPC,OpenFeign是Http。這裡貼一張網上找到的Dubbo和SpringCloud的對比。
所以今天結合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頁面是否有該服務註冊。
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即測試完成。