上一篇文章我們介紹了eureka服務注冊中心的搭建,這篇文章介紹一下如何使用eureka服務注冊中心,搭建一個簡單的服務端注冊服務,客戶端去調用服務使用的案例。
案例中有三個角色:服務注冊中心、服務提供者、服務消費者,其中服務注冊中心就是我們上一篇的eureka單機版啟動既可,流程是首先啟動注冊中心,服務提供者生產服務并注冊到服務中心中,消費者從服務中心中獲取服務并執行。
# 服務提供
*****
我們假設服務提供者有一個hello方法,可以根據傳入的參數,提供輸出“hello xxx,this is first messge”的服務
## 1、pom包配置
創建一個springboot項目,pom.xml中添加如下配置:
```
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
```
## 2、配置文件
application.properties配置如下:
```
spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
```
參數在上一篇都已經解釋過,這里不多說。
## 3、啟動類
啟動類中添加@EnableDiscoveryClient注解
```
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
```
## 4、controller
提供hello服務
```
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello "+name+",this is first messge";
}
}
```
添加@EnableDiscoveryClient注解后,項目就具有了服務注冊的功能。啟動工程后,就可以在注冊中心的頁面看到SPRING-CLOUD-PRODUCER服務。

到此服務提供者配置就完成了。
# 服務調用
*****
## 1、pom包配置
和服務提供者一致
```
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
```
## 2、配置文件
application.properties配置如下:
```
spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
```
## 3、啟動類
啟動類添加@EnableDiscoveryClient和@EnableFeignClients注解。
```
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
```
* @EnableDiscoveryClient :啟用服務注冊與發現
* @EnableFeignClients:啟用feign進行遠程調用
Feign是一個聲明式Web Service客戶端。使用Feign能讓編寫Web Service客戶端更加簡單, 它的使用方法是定義一個接口,然后在上面添加注解,同時也支持JAX-RS標準的注解。Feign也支持可拔插式的編碼器和解碼器。Spring Cloud對Feign進行了封裝,使其支持了Spring MVC標準注解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支持負載均衡。
## 4、feign調用實現
```
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name") String name);
}
```
name:遠程服務名,及spring.application.name配置的名稱
此類中的方法和遠程服務中contoller中的方法名和參數需保持一致。
## 5、web層調用遠程服務
將HelloRemote注入到controller層,像普通方法一樣去調用即可。
```
@RestController
public class ConsumerController {
@Autowired
HelloRemote HelloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return HelloRemote.hello(name);
}
}
```
到此,最簡單的一個服務注冊與調用的例子就完成了。
# 測試
*****
## 簡單調用
依次啟動spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三個項目
先輸入:http://localhost:9000/hello?name=neo 檢查spring-cloud-producer服務是否正常
返回:hello neo,this is first messge
說明spring-cloud-producer正常啟動,提供的服務也正常。
瀏覽器中輸入:http://localhost:9001/hello/neo
返回:hello neo,this is first messge
說明客戶端已經成功的通過feign調用了遠程服務hello,并且將結果返回到了瀏覽器。
##
## 負載均衡
以上面spring-cloud-producer為例子修改,將其中的controller改動如下:
```
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello "+name+",this is producer 2 send first messge";
}
}
```
在配置文件中改動端口:
```
spring.application.name=spring-cloud-producer
server.port=9003
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
```
打包啟動后,在eureka就會發現兩個服務提供者,如下圖:

然后在瀏覽器再次輸入:http://localhost:9001/hello/neo 進行測試:
第一次返回結果:hello neo,this is first messge
第二次返回結果:hello neo,this is producer 2 send first messge
不斷的進行測試下去會發現兩種結果交替出現,說明兩個服務中心自動提供了服務均衡負載的功能。如果我們將服務提供者的數量在提高為N個,測試結果一樣,請求會自動輪詢到每個服務端來處理。
- JeeSpringCloud
- JeeSpringCloud-獎項
- JeeSpringCloud-企業版
- 企業開發文檔(開源不易)!
- JeeSpringCloud項目外包服
- 快速開發
- 部署初級
- 部署高級
- 部署異常處理
- JeeSpringDriver.java相關注解類報紅色
- Whitelabel Error Page用開發工具啟動報錯
- MAC電腦Whitelabel Error Page用開發工具啟動報錯
- jstl_core報錯沒有找到
- 多模塊訪問不了jsp或訪問不了jsp
- 導入sql報錯
- Error creating 'tfTicketServiceImpl'
- 運行后系統不到的報acivemq異常
- 有報數據庫相關錯誤
- 運行act表格異常
- 不用使用中文路徑
- 開發異常處理
- 代碼生成后訪問報404
- 發版日志&計劃
- 發版日志
- 發版計劃
- 企業群技術咨詢
- 開發培訓文檔
- 目錄
- 系統介紹
- 功能介紹
- 代碼生成步驟
- 代碼生成專題
- 架構代碼介紹
- 集群
- 隊列
- 高速緩存
- 多項目
- JeeSpringCloud-熱部署
- 系統配置項
- 系統配置界面
- 正式版啟用
- 驗證碼啟用
- 修改版本
- 異常郵箱
- 授權功能
- 系統配置文件
- application.yml
- bootstrap.yml
- 系統專題
- 系統模塊專題
- 云接口專題
- 代碼生成專題
- 開發專題
- 數據權限
- SpringBoot教程(免費)
- 入門篇
- web綜合開發
- SpringCloud(免費)
- 大話Spring Cloud
- 注冊中心Eureka
- 服務提供與調用
- 熔斷器Hystrix
- 項目管理(免費)
- 手把手教你做2019年計劃
- 面試(免費)
- 40K!程序員四面美團,已拿Offer!這些經驗分享給你!
- 小白程序員僅用 5 分鐘入職 BAT,他只做了這件事!