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

上一篇文章咱們介紹了eureka服務註冊中心的搭建,這篇文章介紹一下如何使用eureka服務註冊中心,搭建一個簡單的服務端註冊服務,客戶端去調用服務使用的案例。願意瞭解源碼的朋友直接求求交流分享技術:二一四七七七五六三三web

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

服務提供
咱們假設服務提供者有一個hello方法,能夠根據傳入的參數,提供輸出「hello ,this is first messge」的服務瀏覽器

一、pom包配置
建立一個springboot項目,pom.xml中添加以下配置:springboot

<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配置以下:app

spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

三、啓動類

啓動類中添加@EnableDiscoveryClient註解負載均衡

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
 
	public static void main(String[] args) {
		SpringApplication.run(ProducerApplication.class, args);
	}
}

四、controller

提供hello服務spring-boot

@RestController
public class HelloController {
	
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is first messge";
    }
}

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

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

服務調用

一、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>

二、配置文件

application.properties配置以下:

spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

三、啓動類

啓動類添加@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組合使用以支持負載均衡。

四、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中的方法名和參數需保持一致。

五、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個,測試結果同樣,請求會自動輪詢到每一個服務端來處理。

總體代碼結構以下:

相關文章
相關標籤/搜索