天天看點

springcloud(三) 服務提供與調用

目錄

    • 服務提供
            • --1 pom包配置
            • --2 配置檔案
            • --3 啟動類
            • --4 controller
    • 服務調用
            • --4 feign 調用實作
            • --5 web層調用遠端服務
    • 測試
    • 負載均衡

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

–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";         }     }           

添加

@EnableDisconveryClient

注解之後, 項目就具有了服務注冊的功能,啟動工程後,就可以再注冊中心的頁面看到SPRING-CLOUD-PRODUCER服務

springcloud(三) 服務提供與調用

到此服務提供者就完成了

和服務提供者一緻

<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>           
application.properties配置如下:     spring.application.name=spring-cloud-consumer     server.port=9001     eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/           

啟動類添加

@EnableDiscoveryClient

@EnbleFeignClients

@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就會發現兩個服務提供者,如下圖:

springcloud(三) 服務提供與調用

然後在浏覽器再次輸入:

http://localhost:9001/hello/neo

進行測試:

第一次傳回結果:

hello neo,this is first messge

第二次傳回結果:

hello neo,this is producer 2 send first messge

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