springcloud入门学习--服务治理篇

一、背景

作为一个Java后端爱好者,不可能学点Java并发编程、JVM以及springcloud微服务的。虽然这些我都还没学,但是在后期有空就会进行学习了解的。今天开始了简单springcloud学习,在此记录一下。

参考文章
C语言编程网学习springcloud
RestTemplate的使用

二、具体步骤

  1. 什么是springcloud?

Spring Cloud 是一系列框架的有序集合。它利用 Spring Boot 的开发便利性,巧妙地简化了分布式系统基础设施的开发,如服务注册、服务发现、配置中心、消息总线、负载均衡、断路器、数据监控等,这些都可以用 Spring Boot 的开发风格做到一键启动和部署。

  1. 模块介绍

Spring Cloud 模块介绍
Spring Cloud 模块的相关介绍如下:
Eureka:服务注册中心,用于服务管理。
Ribbon:基于客户端的负载均衡组件。
Hystrix:容错框架,能够防止服务的雪崩效应。
Feign:Web 服务客户端,能够简化 HTTP 接口的调用。
Zuul:API 网关,提供路由转发、请求过滤等功能。
Config:分布式配置管理。
Sleuth:服务跟踪。
Stream:构建消息驱动的微服务应用程序的框架。
Bus:消息代理的集群消息总线。

  1. 开始使用

创建一个maven工程。
在这里插入图片描述
分别使用springboot创建一个注册中心(Eureka Server)、消费者和服务者若干(都为client,客户端),可以类比交易过程,支付宝平台或银行为中心平台,而人们为若干生产者和消费者。(区块链,比特币,则是去中心化。。。)
在这里插入图片描述
配置信息

服务注册中心

spring.application.name=helpton-server

# port
server.port=8761
# 不能自己注册自己
eureka.client.register-with-eureka=false
# 不需要检索服务
eureka.client.fetch-registry=false

注解

@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {

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

}

启动测试,可以看到自带的管理网站,访问相应端口。

配置和注解消费者和生产者

spring.application.name=provider
server.port=8082

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
# 采用IP注册
eureka.instance.preferIpAddress=true
# 定义实例ID格式
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

生产者写个controller一个服务。

@RestController
public class TestController {
    @GetMapping("/hello")
    String test(){return "hello";}

}

消费者来调用该服务


@RestController
public class CallOtherController {

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/call/hello")
    String callHello(){
       return restTemplate.getForObject("http://localhost:8082/hello",String.class);
    }



}

restTemplate的简单使用(具体参考RestTemplate的使用

package com.example.demo.rest;


import net.minidev.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @AUTHOR LYF
 * @DATE 2021/4/14
 * @VERSION 1.0
 * @DESC
 * https://www.cnblogs.com/54chensongxia/p/11414923.html
 */
@SpringBootTest
public class RestTemplateTest {
    @Autowired
    RestTemplate restTemplate;
    @Test
    public void test(){

        // 1.普通接口

        // (1) GET方法
        Map<String,String> vars = new HashMap<>();
        vars.put("param","1");
        // getForObject  (url,
        String rs = restTemplate.getForObject("http://www.baidu.com",String.class);//,vars
        //JSONObject jsonRs = restTemplate.getForObject("", JSONObject.class);

        //

        //(2) POST方法
         JSONObject params = new JSONObject();
         ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity("",params,JSONObject.class);
         responseEntity.getStatusCode();//getStatus,Code,Body,Header

        // 通过request请求

        RequestEntity requestEntity = RequestEntity.get("").header("").build();
        restTemplate.exchange(requestEntity,JSONObject.class);

       // restTemplate.execute("","POST");


        System.out.println(rs);

    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值