SpringCloud(二):服務提供與調(diào)用【Finchley版】

上一篇我們介紹了服務注冊與發(fā)現(xiàn),這一篇我們來講講服務提供與調(diào)用,服務的提供者像注冊中心注冊服務,調(diào)用方從注冊中心拉去服務進行使用,邏輯非常的簡單,Spring cloud有兩種服務調(diào)用方式,一種是 ribbon + restTemplate,另一種是 feign,今天我們就來簡單的學習一下這兩種調(diào)用方法。
在該demo中將有4個服務,注冊中心、服務提供方、基于ribbon + restTemplate的服務調(diào)用方、基于feign的服務調(diào)用方,在后面的demo中,都將使用nacos作為服務注冊中心并且默認啟動。

服務提供方

服務提供方功能非常簡單,提供一個hello方法,輸出hello spring cloud+機器端口。創(chuàng)建一個名為 hello-provider-service 的 springboot 項目,對項目進行下面一系列改造。

1、pom.xml

在 pom.xml 里引入 nacos 依賴和 springboot web 依賴。

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        # naocs服務注冊依賴
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
2、引導類

在引導類上添加@EnableDiscoveryClient注解

@SpringBootApplication
@EnableDiscoveryClient
public class HelloProviderServiceApplication {

    public static void main(String[] args) {
        
        SpringApplication.run(HelloProviderServiceApplication.class, args);
    }

}
3、服務提供類

建立一個HelloController類,編寫一個hello方法。

@RestController
public class HelloController {
    // 服務器端口
    @Value("${server.port}")
    String port;

    @GetMapping(path = "/hello")
    public String hello() {
        return "hello spring cloud ,i am port :" + port;
    }
}
4、application.yml
server:
  port: 8085
spring:
  application:
    name: hello-provider-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

修改好之后,我們將服務提供方項目打包,執(zhí)行mvn install -Dmaven.test.skip=true命令,就可以在target目錄下找到jar包,我們需要將服務端部署兩份,可以使用服務調(diào)用方的負載均衡功能。進入到target目錄,執(zhí)行下面兩條命令,來啟動兩個提供方服務
java -jar hello-provider-service-0.0.1-SNAPSHOT.jar --server.port=8085
java -jar hello-provider-service-0.0.1-SNAPSHOT.jar --server.port=8086
經(jīng)過上述操作之后,我們已經(jīng)將服務提供方部署好了。

Ribbon + RestTemplate 服務調(diào)用方

Ribbon 是一個客戶端負載均衡器,可以讓您對HTTP和TCP客戶端的行為進行大量控制。

RestTemplate 是 spring 框架提供的可用于在應用中調(diào)用 rest 服務的工具類,它簡化了與 http 服務的通信方式,統(tǒng)一了 RESTful 的標準,封裝了 http 鏈接, 我們只需要傳入url及返回值類型即可。相較于之前常用的 HttpClient,RestTemplate 是一種更優(yōu)雅的調(diào)用 RESTful 服務的方式,在 springboot 中有默認的 RestTemplate bean。

創(chuàng)建一個名為 ribbon-customer-service 的 springboot 項目,該項目也需要像注冊中心注冊,創(chuàng)建完項目之后,對項目進行下面改造。

1、pom.xml

在 pom.xml 里引入ribbon、nacos依賴包

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
    </dependencies>
2、引導類

在引導類上添加@EnableDiscoveryClient注解和實例化一個具有負載均衡功能的 RestTemplate bean ,通過添加 @LoadBalanced注解來表明這個restRemplate開啟負載均衡的功能

@SpringBootApplication
@EnableDiscoveryClient
public class RibbonCustomerServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(RibbonCustomerServiceApplication.class, args);
    }

    @Bean
    @LoadBalanced // 開啟負載均衡
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
3、業(yè)務層
service 層

創(chuàng)建HelloService類并且添加@Service注解

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    public String hello(){
        // 根據(jù)服務名調(diào)用
        return restTemplate.getForObject("http://hello-provider-service/hello",String.class);
    }
}
controller 層
@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping(path = "/hello")
    public String hello(){
        return helloService.hello();
    }
}
4、application.yml 配置文件
server:
  port: 8087
spring:
  application:
    name: ribbon-customer-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

啟動 ribbon-customer-service 項目,在瀏覽器里訪問http://127.0.0.1:8087/hello,你將交替看到

hello spring cloud ,i am port :8085
hello spring cloud ,i am port :8086

基于 Feign 的服務調(diào)用方

Feign 是一種聲明式、模板化的 HTTP 客戶端,是一種基于接口注解的編程模式,在使用 Feign 做服務調(diào)用的時候,它能夠讓你像調(diào)用本地方法一樣,完全感覺不到這是一個通過 http 請求調(diào)用遠程方法。Feign 主要有如下特性:

  • 可插拔的注解支持,包括Feign注解和JAX-RS注解
  • 支持可插拔的HTTP編碼器和解碼器
  • 支持Hystrix和它的Fallback
  • 支持Ribbon的負載均衡
  • 更多介紹請訪問 https://github.com/openfeign/feign
    上面我們簡單的介紹了 Feign ,接下來我們創(chuàng)建一個名為 feign-customer-service 的 springboot 項目,按照套路對項目進行以下幾步改造。
1、pom.xml

在 pom.xml 中添加nacos、feign的依賴

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>0.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
2、引導類

在引導類跟上添加@EnableFeignClients注解來啟動feign功能

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignCustomerServiceApplication {

    public static void main(String[] args) {
        
        SpringApplication.run(FeignCustomerServiceApplication.class, args);
    }

}
3、業(yè)務層
service 層

創(chuàng)建一個HelloService 接口,通過@FeignClient來指定調(diào)用那個服務

@FeignClient(value = "hello-provider-service")
public interface HelloService {

    @GetMapping(path = "/hello")
    String hello();
}
controller 層

創(chuàng)建一個HelloController類,對外提供一個hello方法即可

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping(path = "/hello")
    public String hello(){
        return helloService.hello();
    }
}

5、application 配置
server:
  port: 8088
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: feign-customer-service

啟動feign-customer-service項目,在瀏覽器里訪問http://127.0.0.1:8088/hello,我們能夠看到跟上面一樣的效果。瀏覽器里面交替顯示:

hello spring cloud ,i am port :8085
hello spring cloud ,i am port :8086

本文demo下載 :https://github.com/BinaryBall/springcloud-learn/tree/master/spring-cloud-learn-02

掃碼關注公眾號(搜索公眾號:平頭哥的技術博文)一起交流學習唄
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容