您好,登錄后才能下訂單哦!
Springboot2XConsul中怎么利用RestTemplate實現服務調用,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
服務調用有兩種方式:
A.使用RestTemplate 進行服務調用
負載均衡——通過Ribbon注解RestTemplate
B.使用Feign 進行聲明式服務調用
負載均衡——默認使用Ribbon實現
先使用RestTemplate來實現
1.服務注冊發現中心
啟動Consul
consul agent -dev
2.服務端
在spring boot2X整合Consul 的基礎上
添加服務provider,provider1
provider測試方法
package com.xyz.provider.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic 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;@RestControllerpublic class demoController { @RequestMapping("/hello") public String Hello(){ return "hello,another provider"; }}
啟動provider和provider1
瀏覽器訪問http://localhost:8500
有兩個服務提供者節點實例
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=8015spring.application.name=xyz-comsumerspring.cloud.consul.host=localhostspring.cloud.consul.port=8500spring.cloud.consul.discovery.register=falsespring.cloud.consul.discovery.health-check-url=/actuator/healthspring.cloud.consul.discovery.heartbeat.enabled=truemanagement.endpoints.web.exposure.include=*management.endpoint.health.show-details=always
(3)測試方法
獲取所有注冊的服務,從注冊的服務中選取一個,服務調用
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;@RestControllerpublic 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; }}
注:
客戶端調用的服務名,是在服務端指定的,在服務端配置里使用 spring.cloud.consul.discovery.service-name指注冊到 Consul 的服務名稱
測試
啟動Consul
啟動provider和provider1
啟動comsumer
測試地址 http://localhost:8015/services
返回結果
[ { "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
返回結果
http://hkgi-PC:8011 或 http://hkgi-PC:8011 測試地址 http://localhost:8015/hello 返回結果 hello,provider 或 hello,another provider
注:
結果交替出現的,這是因為負載均衡器是采用的是輪詢的方式
說明:
調用的過程:
A.通過LoadBalancerClient查詢服務
B.通過RestTemplate調用遠程服務
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;@SpringBootApplicationpublic class ComsumerApplication { public static void main(String[] args) { SpringApplication.run(ComsumerApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); }}
服務調用
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;@RestControllerpublic 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
關于Springboot2XConsul中怎么利用RestTemplate實現服務調用問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。