侧边栏壁纸
  • 累计撰写 32 篇文章
  • 累计创建 13 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

微服务随记

javalx
2022-08-18 / 0 评论 / 0 点赞 / 15 阅读 / 5130 字
微服务架构中常用的技术:
  • 服务注册与服务发现

  • 配置中心

  • 服务通信

  • 负载均衡器

  • 服务网关

  • 断路器

  • 服务监控

  • 消息队列

微服务架构中常用的技术落地方案
技术Spring 官方或第三Alibaba套件Netflix套件
服务注册与服务发现Consul、ZookeeperNacosEureka
配置中心Spring Cloud ConfigNacos
服务通信Open FeignDubboFeign
负载均衡器LoadbalancerRibbon
服务网关Spring Cloud GatewayZuul
断路器Resilience4jSentinelHystrix
链路追踪Spring Cloud Sleuth、Zipkin
分布式事务Seata
服务通信

Java中发起发起HTTP调用的常用方式:

  • Java自带的HttpUrlConnection类、HttpClient工具包
  • Spring提供的RestTemplate工具和WebClient工具
使用HttpClient处理请求

依赖:

<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
</dependency>

示例:

    /**
     * 使用HttpClient来处理http请求
     * @return
     * @throws IOException
     */
    @GetMapping("/httpClientTest")
    public String httpClientTest() throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet("http://localhost:8081/hello");
        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpClient.execute(httpGet);
            // 判断返回状态码
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                // 打印请求结果
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpClient.close();
        }
        return "请求成功";
    }
使用RestTemplate处理请求

依赖:

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

示例:

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        RestTemplate restTemplate = new RestTemplate(factory);
        // UTF-8编码设置
        restTemplate.getMessageConverters().set(1,
                new StringHttpMessageConverter(Charset.forName("UTF-8")));
        return restTemplate;

    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        // 超时时间 10秒
        factory.setReadTimeout(10 * 1000);
        // 超时时间 5秒
        factory.setConnectTimeout(5 * 1000);
        return factory;
    }
}
@Resource
private RestTemplate restTemplate;

/**
 * 使用RestTemplate来处理http请求
 * @return
 * @throws IOException
 */
@GetMapping("/restTemplateTest")
public String restTemplateTest() {
    // 打印请求结果
    System.out.println(restTemplate.getForObject(SERVICE_URL + "/hello", String.class));
    return "请求成功";
}
使用WebClient处理请求

依赖:

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

示例:

package ltd.newbee.cloud.web;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@RestController
public class ConsumerController2 {

    private final String SERVICE_URL = "http://localhost:8081";

    private WebClient webClient = WebClient.builder()
            .baseUrl(SERVICE_URL)
            .build();

    /**
     * 使用WebClient处理http请求
     * @return
     */
    @GetMapping("/webClientTest")
    public String webClientTest() {
        Mono<String> mono = webClient
                .get() // GET 请求方式
                .uri("/hello")  // 请求地址
                .retrieve() // 获取响应结果
                .bodyToMono(String.class); //响应结果转换

        // 打印请求结果
        mono.subscribe(result -> {
            System.out.println(result);
        });
        return "请求成功";
    }
}
RestTemplate是阻塞式客户端,WebClient是非阻塞客户端!
0

评论区