[SpringCloud]springCloud-netflix-Eureka服務消費者(ribbon+restTemplat)

在上一篇文章,講了服務的註冊和發現。在微服務架構中,業務都會被拆分成一個獨立的服務,服務與服務的通訊是基於http
restful的。Spring cloud有兩種服務調用方式,一種是ribbon+restTemplate,另一種是feign。在這一篇文章首先講解下基於ribbon+rest。

1.ribbon簡介

ribbon是一個負載均衡客戶端,可以很好的控制http和tcp的一些行爲。Feign默認集成了ribbon。

2.準備工作

這一篇文章基於上一篇文章的工程,啓動eureka-server
工程;啓動service-hi工程,它的端口爲8762;將service-hi的配置文件的端口改爲8763,並啓動,這時你會發現:service-hi在eureka-server註冊了2個實例,這就相當於一個小的集羣。

在IDEA下多實例啓動設置

3.建立服務消費者

重新新建一個spring-boot工程,取名爲:service-ribbon; 在它的pom.xml繼承了父pom文件,並引入了以下依賴:

  <modelVersion>4.0.0</modelVersion>

    <groupId>com.forezp</groupId>
    <artifactId>service-ribbon</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-ribbon</name>
    <description>Demo project for Spring Boot</description>


    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>sc-f-chapter2</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
    </dependencies>

在啓動類編寫:

在工程的啓動類中,通過@EnableDiscoveryClient向服務中心註冊;並且向程序的ioc注入一個bean:
restTemplate;並通過@LoadBalanced註解表明這個restRemplate開啓負載均衡的功能。

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient  //向服務中心註冊;並且向程序ioc注入一個bean
public class ServiceRibbionApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

編寫一個服務:

/**
 * @PACKAGE_NAME com.springcloud.serviceribbon
 * @PROJECT_NAME sc-f-chapter1
 * @創建人 huang
 * @創建時間 2018/11/27
 */
@Service
public class HelloService {
    @Autowired
    private RestTemplate restTemplate;

    /* 通過之前注入ioc容器的restTemplate來消費service-hi服務的“/hi”接口,
    在這裏我們直接用的程序名替代了具體的url地址,
    在ribbon中它會根據服務名來選擇具體的服務實例,
    根據服務實例在請求的時候會用具體的url替換掉服務名*/

    /**
     * 調用SERVICE-HI這個服務
     *
     * @param name
     * @return
     */
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name=" + name, String.class);
    }
}

在控制層調用這個服務:

/**
 * @PACKAGE_NAME com.springcloud.serviceribbion.controller
 * @PROJECT_NAME sc-f-chapter1
 * @創建人 huang
 * @創建時間 2018/11/27
 */
@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

    @GetMapping(value = "/hi/{name}")
    public String hi(@PathVariable("name") String name) {
        return helloService.hiService(name);
    }
}

修改全局配置文件:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
server:
  port: 9002
spring:
  application:
    name: service-ribbon

多次訪問:http://localhost:9002/hi/黃世民
頁面交替出現:

hi黃世民,i am from port:9000
hi黃世民,i am from port:8889

此時的服務列表
在這裏插入圖片描述

  • 一個服務註冊中心,eureka server,端口爲8888
  • service-hi工程跑了兩個實例,端口分別爲8889,9000,分別向服務註冊中心註冊
  • sercvice-ribbon端口爲9002,向服務註冊中心註冊
  • 當sercvice-ribbon通過restTemplate調用service-hi的hi接口時,因爲用ribbon進行了負載均衡,會輪流的調用service-hi:8889和9000兩個端口的hi接口;

4.參考資料:

https://blog.csdn.net/forezp/article/details/69788938
https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章