最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring boot2X Consul如何通過RestTemplate實現(xiàn)服務(wù)調(diào)用

 更新時間:2019年12月02日 09:25:15   作者:慕塵  
這篇文章主要介紹了spring boot2X Consul如何通過RestTemplate實現(xiàn)服務(wù)調(diào)用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了spring boot2X Consul如何通過RestTemplate實現(xiàn)服務(wù)調(diào)用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

Consul可以用于實現(xiàn)分布式系統(tǒng)的服務(wù)發(fā)現(xiàn)與配置

服務(wù)調(diào)用有兩種方式:

A.使用RestTemplate 進行服務(wù)調(diào)用

負載均衡——通過Ribbon注解RestTemplate

B.使用Feign 進行聲明式服務(wù)調(diào)用

負載均衡——默認使用Ribbon實現(xiàn)

先使用RestTemplate來實現(xiàn)

1.服務(wù)注冊發(fā)現(xiàn)中心

啟動Consul

consul agent -dev

2.服務(wù)端

在spring boot2X整合Consul 的基礎(chǔ)上

添加服務(wù)provider,provider1

provider測試方法

package com.xyz.provider.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class demoController {
 @RequestMapping("/hello")
 public String Hello(){
  return "hello,provider";
 }
}

provider1測試方法

package com.xyz.provider1.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class demoController {
 @RequestMapping("/hello")
 public String Hello(){
  return "hello,another provider";
 }
}

啟動provider和provider1

瀏覽器訪問http://localhost:8500

有兩個服務(wù)提供者節(jié)點實例

3.客戶端

(1)添加依賴

<properties>
  <java.version>1.8</java.version>
  <spring-cloud.version>Greenwich.SR4</spring-cloud.version>
 </properties>

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

  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-consul-discovery</artifactId>
  </dependency>

</dependencies>

<dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
</dependencyManagement>

(2)添加配置

server.port=8015
spring.application.name=xyz-comsumer
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.register=false
spring.cloud.consul.discovery.health-check-url=/actuator/health
spring.cloud.consul.discovery.heartbeat.enabled=true
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

(3)測試方法

獲取所有注冊的服務(wù),從注冊的服務(wù)中選取一個,服務(wù)調(diào)用

package com.xyz.comsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {
 @Autowired
 private LoadBalancerClient loadBalancer;
 @Autowired
 private DiscoveryClient discoveryClient;
 private String serviceName = "service-provider";

 @RequestMapping("/services")
 public Object services() {
  return discoveryClient.getInstances(serviceName);
 }

 @RequestMapping("/discover")
 public Object discover() {
  return loadBalancer.choose(serviceName).getUri().toString();
 }

 @RequestMapping("/hello")
 public String hello() {
  ServiceInstance serviceInstance = loadBalancer.choose(serviceName);
  String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
  return callServiceResult;
 }
}

注:

客戶端調(diào)用的服務(wù)名,是在服務(wù)端指定的,在服務(wù)端配置里使用 spring.cloud.consul.discovery.service-name指注冊到 Consul 的服務(wù)名稱

測試

啟動Consul

啟動provider和provider1

啟動comsumer

測試地址 http://localhost:8015/services

返回結(jié)果

[
 {
  "instanceId": "provider-8010",
  "serviceId": "service-provider",
  "host": "hkgi-PC",
  "port": 8010,
  "secure": false,
  "metadata": {
   "secure": "false"
  },
  "uri": "http://hkgi-PC:8010",
  "scheme": null
 },
 {
  "instanceId": "provider-8011",
  "serviceId": "service-provider",
  "host": "hkgi-PC",
  "port": 8011,
  "secure": false,
  "metadata": {
   "secure": "false"
  },
  "uri": "http://hkgi-PC:8011",
  "scheme": null
 }
]

測試地址 http://localhost:8015/discover

返回結(jié)果

  •   http://hkgi-PC:8011 或 http://hkgi-PC:8011
  •   測試地址 http://localhost:8015/hello
  •   返回結(jié)果
  •   hello,provider 或 hello,another provider

注:

結(jié)果交替出現(xiàn)的,這是因為負載均衡器是采用的是輪詢的方式

說明:

調(diào)用的過程:

A.通過LoadBalancerClient查詢服務(wù)

B.通過RestTemplate調(diào)用遠程服務(wù)

Ribbon負載均衡策略

  •   BestAvailableRule
  •   AvailabilityFilteringRule
  •   WeightedResponseTimeRule
  •   RetryRule
  •   RoundRobinRule
  •   RandomRule
  •   ZoneAvoidanceRule

自定義Ribbon負載均衡——使用隨機訪問策略

修改啟動類

package com.xyz.comsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class ComsumerApplication {
 public static void main(String[] args) {
  SpringApplication.run(ComsumerApplication.class, args);
 }

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

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

package com.xyz.comsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class RibbonHelloController {
 @Autowired
 private RestTemplate restTemplate;
 private String serviceName = "service-provider";

 @RequestMapping("/ribbon/hello")
 public String hello() {
  String callServiceResult = restTemplate.getForObject("http://"+serviceName+"/hello", String.class);
  return callServiceResult;
 }
}

配置添加

service-provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule

重新啟動 comsumer

測試地址 http://localhost:8015/ribbon/hello

輸出的結(jié)果不再是交替出現(xiàn),改為隨機的了

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java實現(xiàn)石頭剪刀布游戲

    Java實現(xiàn)石頭剪刀布游戲

    這篇文章主要為大家詳細介紹了Java實現(xiàn)石頭剪刀布游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Java 基于tcp協(xié)議實現(xiàn)文件上傳

    Java 基于tcp協(xié)議實現(xiàn)文件上傳

    這篇文章主要介紹了Java 基于tcp協(xié)議實現(xiàn)文件上傳,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-11-11
  • Java遞歸方法求5!的實現(xiàn)代碼

    Java遞歸方法求5!的實現(xiàn)代碼

    這篇文章主要介紹了Java遞歸方法求5!的實現(xiàn)代碼,需要的朋友可以參考下
    2017-02-02
  • Java之如何關(guān)閉流

    Java之如何關(guān)閉流

    這篇文章主要介紹了Java之如何關(guān)閉流問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 輕量級聲明式的Http庫——Feign的獨立使用

    輕量級聲明式的Http庫——Feign的獨立使用

    這篇文章主要介紹了輕量級聲明式的Http庫——Feign的使用教程,幫助大家更好的理解和學習使用feign,感興趣的朋友可以了解下
    2021-04-04
  • Java Iterator迭代器_動力節(jié)點Java學院整理

    Java Iterator迭代器_動力節(jié)點Java學院整理

    迭代器是一種模式,它可以使得對于序列類型的數(shù)據(jù)結(jié)構(gòu)的遍歷行為與被遍歷的對象分離,接下來通過本文給大家分享Java Iterator迭代器_動力節(jié)點Java學院整理,需要的朋友參考下吧
    2017-05-05
  • Java Kafka分區(qū)發(fā)送及消費實戰(zhàn)

    Java Kafka分區(qū)發(fā)送及消費實戰(zhàn)

    本文主要介紹了Kafka分區(qū)發(fā)送及消費實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • SpringBoot整合Shiro和Redis的示例代碼

    SpringBoot整合Shiro和Redis的示例代碼

    這篇文章主要介紹了SpringBoot整合Shiro和Redis的示例代碼,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • java獲取redis日志信息與動態(tài)監(jiān)控信息的方法

    java獲取redis日志信息與動態(tài)監(jiān)控信息的方法

    這篇文章主要給大家介紹了關(guān)于java如何獲取redis日志信息與動態(tài)監(jiān)控信息的方法,文中介紹的非常詳細,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-04-04
  • SpringBoot整合ip2region獲取客戶端IP地理位置信息

    SpringBoot整合ip2region獲取客戶端IP地理位置信息

    在我們?nèi)粘EB開發(fā)工作中,經(jīng)常會有需要獲取客戶端地理位置的需求,本文主要介紹了SpringBoot整合ip2region獲取客戶端IP地理位置信息,具有一定的參考價值,感興趣的可以了解一下
    2024-08-08

最新評論

隆化县| 沾益县| 青河县| 乌拉特前旗| 玛多县| 玉林市| 藁城市| 汤原县| 太仓市| 东海县| 刚察县| 吕梁市| 县级市| 靖宇县| 武胜县| 辽阳市| 云安县| 内乡县| 尉犁县| 永川市| 北流市| 黄冈市| 女性| 清流县| 方山县| 秭归县| 吕梁市| 宕昌县| 贺州市| 陇西县| 增城市| 蓬莱市| 太保市| 宜春市| 安化县| 达孜县| 龙泉市| 施甸县| 裕民县| 大英县| 永兴县|