大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 SpringCloud服務(wù)調(diào)用的兩種方法

SpringCloud服務(wù)調(diào)用的兩種方法

更新時間:2021-06-28 15:48:48 來源:動力節(jié)點(diǎn) 瀏覽1346次

spring-cloud調(diào)用服務(wù)有兩種方式,一種是Ribbon+RestTemplate,另外一種是Feign。

Ribbon是一個基于HTTP和TCP客戶端的負(fù)載均衡器,其實feign也使用了ribbon,只要使用 FeignClient時,ribbon就會自動使用。

1.Ribbon

新建模塊client-a

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud</artifactId>
        <groupId>com.feng</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>client-a</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
    </dependencies>

</project>

新建bootstrap.yml

server:
  port: 8910

eureka:
  client:
    serviceUrl:
          defaultZone: http://localhost:8010/eureka/

spring:
  application:
      name: client-a

ClientApplication, 這里我們需要注冊一個RestTemplate,并且使用@LoadBalanced開啟負(fù)載功能

/**
 * @author fengzp
 * @date 17/5/9
 * @email [email protected]
 * @company 廣州易站通計算機(jī)科技有限公司
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

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

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

測試用的controller

/**
 * @author fengzp
 * @date 17/5/9
 * @email [email protected]
 * @company 廣州易站通計算機(jī)科技有限公司
 */
@RestController
public class TestController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/hi")
    public String hi(@RequestParam String id){
        return restTemplate.getForObject("http://service-a/hi?id="+id, String.class);
    }
}

為了測試負(fù)載功能,這里要再新建一個模塊service-b,和上一篇的service-a的代碼基本相同,只把端口修改了就可以。

把client-a和service-b都啟動成功后,打開eureka中心應(yīng)該看到:

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

打開http://localhost:8910/hi?id=123

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

可以看到服務(wù)已經(jīng)成功調(diào)用。

然后刷新頁面

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

看到端口已經(jīng)改變,說明負(fù)載功能成功實現(xiàn)

2.feign

新建模塊client-b

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud</artifactId>
        <groupId>com.feng</groupId>
        <version>0.0.1</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>client-b</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
    </dependencies>
</project>

bootstrap.yml

server:
  port: 8911

eureka:
  client:
    serviceUrl:
          defaultZone: http://localhost:8010/eureka/

spring:
  application:
      name: client-b

ClientApplication,使用 EnableFeignClients開啟feiginClient功能

/**
 * @author fengzp
 * @date 17/5/9
 * @email [email protected]
 * @company 廣州易站通計算機(jī)科技有限公司
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ClientApplication {

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

}

這里新建一個ServiceAFeignClient來調(diào)用service-a服務(wù)

@Component
@FeignClient(value = "service-a") //這里的name對應(yīng)調(diào)用服務(wù)的spring.applicatoin.name
public interface ServiceAFeignClient {

    @RequestMapping(value = "/hi")
    String hi(@RequestParam("id") String id);

}

Controller

@RestController
public class TestController {

    @Autowired
    ServiceAFeignClient serviceAFeignClient;

    @RequestMapping("/hi")
    public String hi(@RequestParam String id){
        return serviceAFeignClient.hi(id);
    }
}

運(yùn)行后的結(jié)果應(yīng)該是和ribbon的相同。

個人感覺使用feign比較舒服,代碼比較簡潔。

以上就是動力節(jié)點(diǎn)小編介紹的"SpringCloud服務(wù)調(diào)用的兩種方法",希望對大家有幫助,想了解更多可查看SpringCloud視頻教程技術(shù)文檔,如有疑問,請在線咨詢,有專業(yè)老師隨時為您服務(wù)。

提交申請后,顧問老師會電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 91福利社在线观看 | 亚洲精品一区二区四季 | 五月天亚洲 | 中文精品北条麻妃中文 | 欧美日韩在线看 | 97在线碰碰观看免费高清 | 午夜欧美激情 | 国产精品欧美在线 | 久久中文字幕亚洲精品最新 | 2021国产在线视频 | 久久99国产精品久久99 | 日本欧美另类 | 亚洲图区欧美 | 亚洲成人在线免费观看 | 久久99精品国产一区二区三区 | 99热成人 | 亚洲免费毛片 | 欧美色v| 12一14周岁毛片免费网站 | 美日韩免费视频 | 妖精视频在线观看网站 | 9久热| 男人爱看的网站 | 四虎综合 | 日日夜夜视频 | 99久久精品无码一区二区毛片 | 亚洲日本一区二区三区在线不卡 | 青草草在线观看免费视频 | 亚洲一区中文字幕在线 | 欧美αv在线 | 久久精品亚洲99一区二区 | 欧美成人免费全网站大片 | 日韩视频久久 | 成人欧美一区二区三区视频 | 日韩 欧美 亚洲 中文字幕 | 亚洲啪啪看看 | 精产国品一二二区视 | 成人在线免费 | 亚洲国产欧美视频 | 天海翼一区二区三区高清视频 | 奇米影视久久 |