资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Springboot2XConsul中怎么利用RestTemplate实现服务调用

Springboot2XConsul中怎么利用RestTemplate实现服务调用,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

专注于为中小企业提供做网站、网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业鹰手营子免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

服务调用有两种方式:

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)添加依赖

 1.8  Greenwich.SR4     org.springframework.boot   spring-boot-starter-web       org.springframework.cloud   spring-cloud-starter-consul-discovery          org.springframework.cloud    spring-cloud-dependencies    ${spring-cloud.version}    pom    import    

(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实现服务调用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


网站栏目:Springboot2XConsul中怎么利用RestTemplate实现服务调用
URL网址:http://cdkjz.cn/article/gsjcsj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220