springcloud(3):服務提供與調用

轉自:微信公衆號--純潔的微笑(示例代碼:https://github.com/ityouknow/spring-cloud-starter

(本人僅學習記錄)

案例中有三個角色:服務註冊中心、服務提供者、服務消費者,其中服務註冊中心就是我們上一篇的eureka單機版啓動既可,流程是首先啓動註冊中心,服務提供者生產服務並註冊到服務中心中,消費者從服務中心中獲取服務並執行。

服務提供

我們假設服務提供者有一個hello方法,可以根據傳入的參數,提供輸出“hello xxx,this is first messge”的服務

1、pom包配置

創建一個springboot項目,pom.xml中添加如下配置:

  1. <dependencies>

  2.    <dependency>

  3.        <groupId>org.springframework.cloud</groupId>

  4.        <artifactId>spring-cloud-starter-eureka</artifactId>

  5.    </dependency>

  6.    <dependency>

  7.        <groupId>org.springframework.boot</groupId>

  8.        <artifactId>spring-boot-starter-test</artifactId>

  9.        <scope>test</scope>

  10.    </dependency>

  11. </dependencies>

2、配置文件

application.properties配置如下:

  1. spring.application.name=spring-cloud-producer

  2. server.port=9000

  3. eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

參數在上一篇都已經解釋過,這裏不多說。

3、啓動類

啓動類中添加 @EnableDiscoveryClient註解

  1. @SpringBootApplication

  2. @EnableDiscoveryClient

  3. public class ProducerApplication {

  4.    public static void main(String[] args) {

  5.        SpringApplication.run(ProducerApplication.class, args);

  6.    }

  7. }

4、controller

提供hello服務

  1. @RestController

  2. public class HelloController {

  3.    @RequestMapping("/hello")

  4.    public String index(@RequestParam String name) {

  5.        return "hello "+name+",this is first messge";

  6.    }

  7. }

添加 @EnableDiscoveryClient註解後,項目就具有了服務註冊的功能。啓動工程後,就可以在註冊中心的頁面看到SPRING-CLOUD-PRODUCER服務。


到此服務提供者配置就完成了。

服務調用

1、pom包配置

和服務提供者一致

  1. <dependencies>

  2.    <dependency>

  3.        <groupId>org.springframework.cloud</groupId>

  4.        <artifactId>spring-cloud-starter-eureka</artifactId>

  5.    </dependency>

  6.    <dependency>

  7.        <groupId>org.springframework.boot</groupId>

  8.        <artifactId>spring-boot-starter-test</artifactId>

  9.        <scope>test</scope>

  10.    </dependency>

  11. </dependencies>

2、配置文件

application.properties配置如下:

  1. spring.application.name=spring-cloud-consumer

  2. server.port=9001

  3. eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3、啓動類

啓動類添加 @EnableDiscoveryClient和 @EnableFeignClients註解。

  1. @SpringBootApplication

  2. @EnableDiscoveryClient

  3. @EnableFeignClients

  4. public class ConsumerApplication {

  5.    public static void main(String[] args) {

  6.        SpringApplication.run(ConsumerApplication.class, args);

  7.    }

  8. }

  • @EnableDiscoveryClient :啓用服務註冊與發現

  • @EnableFeignClients:啓用feign進行遠程調用

Feign是一個聲明式Web Service客戶端。使用Feign能讓編寫Web Service客戶端更加簡單, 它的使用方法是定義一個接口,然後在上面添加註解,同時也支持JAX-RS標準的註解。Feign也支持可拔插式的編碼器和解碼器。Spring Cloud對Feign進行了封裝,使其支持了Spring MVC標準註解和HttpMessageConverters。Feign可以與Eureka和Ribbon組合使用以支持負載均衡。

4、feign調用實現

  1. @FeignClient(name= "spring-cloud-producer")

  2. public interface HelloRemote {

  3.    @RequestMapping(value = "/hello")

  4.    public String hello(@RequestParam(value = "name") String name);

  5. }

  • name:遠程服務名,及spring.application.name配置的名稱

此類中的方法和遠程服務中contoller中的方法名和參數需保持一致。

5、web層調用遠程服務

將HelloRemote注入到controller層,像普通方法一樣去調用即可。

  1. @RestController

  2. public class ConsumerController {

  3.    @Autowired

  4.    HelloRemote HelloRemote;

  5.    @RequestMapping("/hello/{name}")

  6.    public String index(@PathVariable("name") String name) {

  7.        return HelloRemote.hello(name);

  8.    }

  9. }

到此,最簡單的一個服務註冊與調用的例子就完成了。

測試

簡單調用

依次啓動spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三個項目

先輸入: http://localhost:9000/hello?name=neo 檢查spring-cloud-producer服務是否正常

返回: hello neothisisfirst messge

說明spring-cloud-producer正常啓動,提供的服務也正常。

瀏覽器中輸入: http://localhost:9001/hello/neo

返回: hello neothisisfirst messge

說明客戶端已經成功的通過feign調用了遠程服務hello,並且將結果返回到了瀏覽器。

負載均衡

以上面spring-cloud-producer爲例子修改,將其中的controller改動如下:

  1. @RestController

  2. public class HelloController {

  3.    @RequestMapping("/hello")

  4.    public String index(@RequestParam String name) {

  5.        return "hello "+name+",this is producer 2  send first messge";

  6.    }

  7. }

在配置文件中改動端口:

  1. spring.application.name=spring-cloud-producer

  2. server.port=9003

  3. eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

打包啓動後,在eureka就會發現兩個服務提供者,如下圖:


然後在瀏覽器再次輸入: http://localhost:9001/hello/neo 進行測試:

第一次返回結果: hello neothisisfirst messge

第二次返回結果: hello neothisisproducer2send first messge

不斷的進行測試下去會發現兩種結果交替出現,說明兩個服務中心自動提供了服務均衡負載的功能。如果我們將服務提供者的數量在提高爲N個,測試結果一樣,請求會自動輪詢到每個服務端來處理。

示例代碼:https://github.com/ityouknow/spring-cloud-starter

發佈了14 篇原創文章 · 獲贊 7 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章