restTemplate实现跨服务API调用

同时使用ribbon,实现接口调用的负载均衡。

1 注册中心

略…

2 消费端

2.1 pom
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
2.2 application.properties
spring.application.name=ribbon-consumer
server.port=9000
# 本地启动注册中心
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
2.3 启动类
package com.example.lei;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(RibbonConsumerApplication.class, args);
    }

}
2.4 消费
package com.example.lei.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: leimin
 * @Description: new class
 * @Date: 2020/6/8 16:04
 * @Version: 1.0
 */
@RestController
public class ConsumerController {

    /**
     * xx
     */
    @Autowired
    RestTemplate restTemplate;

    /**
     * yyy
     * @return rr
     */
    @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
    private String helloConsumer(){

        return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();
    }
}

3 服务端

3.1 pom
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

3.2 application.properties
spring.application.name=hello-service
# 服务注册到两台注册中心
eureka.client.service-url.defaultZone=http://peer1:1111/eureka/,http://peer2:1112/eureka
3.3 启动类
package com.didispace.springboothello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * 能够被发现为客户端
 */
@EnableDiscoveryClient
@SpringBootApplication
public class SpringBootHelloApplication {

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

}
3.4 服务
package com.didispace.springboothello.controller;

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

import java.util.logging.Logger;

/**
 * @Author: leimin
 * @Description: new class
 * @Date: 2020/6/8 9:08
 * @Version: 1.0
 */
@RestController
public class HelloController {

    /**
     * xx
     */
    private final Logger logger = Logger.getLogger(String.valueOf(getClass()));

    /**
     * xx
     */
    @Autowired
    private DiscoveryClient client;

    /**
     * xx
     * @return xx
     */
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String index(){

        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("/hello,host:" + instance.getHost()+", service_id:" +instance.getServiceId());

        return "hello World !";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RestTemplate是Spring Framework提供的一个用于进行HTTP请求的客户端对象。通过RestTemplate可以方便地完成与服务端的交互,包括发送HTTP请求、接收响应等操作。 使用RestTemplate进行服务调用的一般步骤如下: 1. 创建RestTemplate对象:可以通过直接实例化RestTemplate类或者通过依赖注入的方式获取RestTemplate对象。 2. 设置请求参数:可以设置请求的URL、请求方法、请求头、请求体等参数。 3. 发送请求:调用RestTemplate的方法发送HTTP请求,如getForObject()、postForObject()等。 4. 处理响应:根据返回的响应进行相应的处理,如获取响应状态码、获取响应头、获取响应体等。 以下是一个使用RestTemplate进行GET请求的示例代码: ```java RestTemplate restTemplate = new RestTemplate(); String url = "http://example.com/api/resource"; ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); int statusCode = response.getStatusCodeValue(); HttpHeaders headers = response.getHeaders(); String responseBody = response.getBody(); ``` 以上代码中,首先创建了一个RestTemplate对象,然后通过getForEntity()方法发送GET请求,并将响应结果封装到ResponseEntity对象中。最后可以通过ResponseEntity对象获取响应状态码、响应头和响应体等信息。 当然,RestTemplate还支持其他类型的HTTP请求,如POST、PUT、DELETE等,具体使用方法可以根据实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值