溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springcloud服務(wù)提供與調(diào)用多注意點

發(fā)布時間:2021-12-02 15:50:08 來源:億速云 閱讀:153 作者:柒染 欄目:軟件技術(shù)

本篇文章為大家展示了springcloud服務(wù)提供與調(diào)用多注意點,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

下面介紹一下如何使用eureka服務(wù)注冊中心,搭建一個簡單的服務(wù)端注冊服務(wù),客戶端去調(diào)用服務(wù)使用的案例。

案例中有三個角色:服務(wù)注冊中心、服務(wù)提供者、服務(wù)消費者,其中服務(wù)注冊中心就是eureka單機版啟動既可,流程是首先啟動注冊中心,服務(wù)提供者生產(chǎn)服務(wù)并注冊到服務(wù)中心中,消費者從服務(wù)中心中獲取服務(wù)并執(zhí)行。

服務(wù)提供

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

1、pom包配置

創(chuàng)建一個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服務(wù)

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

添加@EnableDiscoveryClient注解后,項目就具有了服務(wù)注冊的功能。啟動工程后,就可以在注冊中心的頁面看到SPRING-CLOUD-PRODUCER服務(wù)。

springcloud服務(wù)提供與調(diào)用多注意點

到此服務(wù)提供者配置就完成了。

服務(wù)調(diào)用

1、pom包配置

和服務(wù)提供者一致

<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 :啟用服務(wù)注冊與發(fā)現(xiàn)

@EnableFeignClients:啟用feign進行遠程調(diào)用

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

4、feign調(diào)用實現(xiàn)

@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}

name:遠程服務(wù)名,及spring.application.name配置的名稱

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

5、web層調(diào)用遠程服務(wù)

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

@RestController
public class ConsumerController {
 
    @Autowired
    HelloRemote HelloRemote;
    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return HelloRemote.hello(name);
    }
 
}

到此,最簡單的一個服務(wù)注冊與調(diào)用的例子就完成了。

上述內(nèi)容就是springcloud服務(wù)提供與調(diào)用多注意點,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI