简单实践 java spring cloud 负载均衡

1 概要

1.1 实现一个最简单的微服务。远程调用+负载均衡,基本上完成了最核心的微服务框架。

远程调用:RestTemplate

注册中心:eureka

负载均衡:Ribbon

1.2 要点

1.2.1 依赖

1.2.1.1 主框架依赖
  • spring boot 依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>
  •  spring cloud 依赖
<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>
 1.2.1.2  eureka依赖
  • 服务端依赖
<groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  • 客户端依赖 
<groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

 1.2.2 配置文件

  • 服务设置
server:
  port: 10086
spring:
  application:
    name: eureka server
  •  服务注册
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/

1.2 技术关键词

  • spring-cloud-starter
    
  • spring-cloud-dependencies
  • spring-cloud-starter-netflix-eureka-server
  • spring-cloud-starter-netflix-eureka-client
  • spring-boot-starter-web
  • spring:
      application:
        name: eureka server
  • eureka:
      client:
        service-url:
          defaultZone: http://127.0.0.1:10086/eureka/
  • @SpringBootApplication
    @EnableEurekaServer
  • @Bean
    @LoadBalanced
    public RestTemplate
  • @Autowired
    RestTemplate restTemplate;
  • @RestController
  • return "函数2"+restTemplate.getForObject(url,String.class);
  • SpringApplication.run(Main.class);

 

2 代码

2.1 父工程

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xjc.springcloundtest</groupId>
        <artifactId>demo8</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>untitled</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

2.2 注册中 eureka

2.2.1 工程文件

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xjc.springcloundtest</groupId>
        <artifactId>demo8</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>untitled</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>
</project>

2.2.2 配置文件

server:
  port: 10086
spring:
  application:
    name: eureka server
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/

2.2.3 主函数

package com.xjc.springcloundtest;

import com.netflix.discovery.shared.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
        System.out.println("Hello world!");
    }
}

2.2.4 运行效果

2.3 服务工程

2.2.1 工程文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xjc.springcloundtest</groupId>
        <artifactId>demo8</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>untitled1</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

2.3.2 配置文件

spring:
  application:
    name: server1
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/

2.3.3 主函数

package com.xjc.springcloundtest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
        System.out.println("Hello world!");
    }
}

2.3.4 控制器

package com.xjc.springcloundtest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @RequestMapping("/fun")
    public String fun(){
        return "函数1";
    }
}

2.3.5 运行效果

 

2.4 消费者

2.4.1 工程文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xjc.springcloundtest</groupId>
        <artifactId>demo8</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>untitled2</artifactId>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

2.4.2 配置文件

server:
  port: 8081
spring:
  application:
    name: server2
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka/

2.4.3 主函数

package com.xjc.springcloundtest;

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

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class);
        System.out.println("Hello world!");
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder ){
        return builder.build();
    }
}

2.4.4 消费者

package com.xjc.springcloundtest;

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;

@RestController
public class TestController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/fun")
    public String fun(){
        //String url = "http://localhost:8080/fun";
        String url = "http://server1/fun";
        return "函数2"+restTemplate.getForObject(url,String.class);
    }
}

2.4.5 运行效果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/362143.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【JavaScript 漫游】【004】数据类型 object

文章简介 本文为【JavaScript 漫游】专栏的第 004 篇文章&#xff0c;记录 JS 数据类型 object 的重要知识点。 . 运算符和 [] 运算符Object.keys 方法delete 命令in 运算符for ... in ... 对象概述 JS 的对象是一组“键值对”&#xff08;key-value&#xff09;的集合&…

基于ssm的法律咨询系统(有报告)。Javaee项目,ssm项目。

演示视频&#xff1a; 基于ssm的法律咨询系统&#xff08;有报告&#xff09;。Javaee项目&#xff0c;ssm项目。 项目介绍&#xff1a; 采用M&#xff08;model&#xff09;V&#xff08;view&#xff09;C&#xff08;controller&#xff09;三层体系结构&#xff0c;通过Sp…

【百度Apollo】自动驾驶规划技术:实现安全高效的智能驾驶

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏:《linux深造日志》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! ⛳️ 推荐 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下…

springboot-前后端分离——第二篇

本篇主要介绍一个发送请求的工具—postman&#xff0c;然后对请求中的参数进行介绍&#xff0c;例如简单参数、实体参数、数组参数、集合参数、日期类型参数以及json类型参数&#xff0c;对这些参数接收进行总结。最后对响应数据进行介绍&#xff0c;使用统一响应结果返回浏览器…

MIT6.5830 实验0

前置 本次实验使用 Golang 语言实现&#xff0c;在之前的年份中&#xff0c;都是像 cs186 那样使用 Java 实现。原因&#xff1a; Golang 语言作为现代化语言&#xff0c;简单易上手但功能强大。 使参加实验的同学有同一起跑线&#xff0c;而不是像Java那样&#xff0c;有些同…

增加 CentOS 系统的交换空间/虚拟内存(swap)大小

增加 CentOS 系统的交换空间/虚拟内存&#xff08;swap&#xff09;大小 文章目录 增加 CentOS 系统的交换空间/虚拟内存&#xff08;swap&#xff09;大小 检查当前交换空间&#xff1a; 在终端中执行以下命令来查看当前的交换空间情况&#xff1a; swapon --show这将显示当…

二级域名分发全解密支持对接易支付

二级域名分发全解密支持对接易支付 先改epay里面的config.php 你的支付域名 然后再改&#xff0c;二级域名分发网站 环境&#xff1a;php74 伪静态&#xff1a; location / { try_files $uri $uri/ /index.php?$query_string; } 源代码&#xff1a;百度网盘 密码&#xff1a;1…

实现注册登录时数据的加密传输(含前后端具体代码)

前言 http/https协议提交在被抓包时请求内容是明文的, 直接传输账号密码的风险非常大&#xff0c;故这里我们要对数据加密处理&#xff0c;并生成校验码&#xff0c;防止数据篡改 Http/https传输账户密码等数据时需要加密处理的原因主要有以下几点&#xff1a; 数据保密性&a…

20240131在WIN10下配置whisper

20240131在WIN10下配置whisper 2024/1/31 18:25 首先你要有一张NVIDIA的显卡&#xff0c;比如我用的PDD拼多多的二手GTX1080显卡。【并且极其可能是矿卡&#xff01;】800&#xffe5; 2、请正确安装好NVIDIA最新的545版本的驱动程序和CUDA。 2、安装Torch 3、配置whisper http…

理解部署描述符的元素

理解部署描述符的元素 部署描述符是文件名为web.xml的XML文件&#xff0c;其包含了Web应用程序的配置信息。每个Web应用程序都有一个web.xml文件。web.xml文件的元素可用于指定servlet的初始化参数、不同文件的MIME类型、侦听器类&#xff0c;以及将URL模式映射到servlet上。一…

【SparkML系列3】特征提取器TF-IDF、Word2Vec和CountVectorizer

本节介绍了用于处理特征的算法&#xff0c;大致可以分为以下几组&#xff1a; 提取&#xff08;Extraction&#xff09;&#xff1a;从“原始”数据中提取特征。转换&#xff08;Transformation&#xff09;&#xff1a;缩放、转换或修改特征。选择&#xff08;Selection&…

【 USRP 相控阵】X波段相控阵开发平台用户指南

包装 一共三件。 1、AD9081-FMCA-EBZ AD9081 MxFE Evaluation Board, https://www.analog.com/eval-ad9081 AD9081 的全功能评估板使用 ACE 软件进行控制的 PC 软件HMC7044 的板载时钟用于管理套件和 FPGA 时钟选择切换到外部直接时钟 AD9081-FMCA-EBZ 评估板包括以各种模…

pinctrl/gpio子系统(1)-pinctrl子系统介绍及驱动源码分析

1.简介 在如今的驱动开发工作中&#xff0c;实际上已经很少去对着寄存器手册进行驱动开发了&#xff0c;一般板子拿到手&#xff0c;已经有原厂的驱动开发工程师&#xff0c;在gpio子系统、pinctrl子系统中将自家芯片的引脚适配好了。 我们直接基于设备树已配置好的寄存器值&a…

基于YOLOv8的工业油污缺陷检测,多种优化方法---自研注意力CPMS基于CBAM优化, mAP@0.5提升近五个点(二)

&#x1f4a1;&#x1f4a1;&#x1f4a1;本文主要内容:详细介绍了工业油污缺陷检测整个过程&#xff0c;从数据集到训练模型到结果可视化分析&#xff0c;以及如何优化提升检测性能。 &#x1f4a1;&#x1f4a1;&#x1f4a1;加入CPMS mAP0.5由原始的0.648提升至0.699 1.工业…

JMeter 下载、安装、启动

JMeter安装部署依赖Java环境&#xff0c;所以首先得安装JDK。 JDK下载JDK环境变量配置 ① 新建系统环境变量JAVA_HOME ② 编辑系统变量Path ③ 新建系统变量CLASSPATH变量 JMeter下载安装 Apache JMeter - Apache JMeter™ JMeter安装部署依赖Java环境&#xff0c;所以首…

如何在 Golang 中使用 crypto/ed25519 进行数字签名和验证

如何在 Golang 中使用 crypto/ed25519 进行数字签名和验证 引言crypto/ed25519 算法简介环境搭建和准备工作生成密钥对进行数字签名 验证签名实际应用场景案例总结 引言 在当今数字化时代&#xff0c;网络安全显得尤为重要。无论是在网上进行交易、签署合同&#xff0c;还是发…

结构体的学习

结构体与共用体&#xff0c;枚举 1.数据类型复习&#xff1a; 2结构体. eg&#xff1b;统计全校同学信息 需要记录的点--- 姓名&#xff0c;班级&#xff0c;性别&#xff0c;成绩&#xff0c;年龄 统计名字&#xff1a;char s[ ] [ 100 ] { "Tmo" } …

01神经网络的理论及实现

感知机的缺点就是需要设置合适的权重&#xff0c;而权重的设置都是人工操作的。 1、从感知机到神经网络 重新画出感知机的模型&#xff0c;在图上加上偏置&#xff0c;由于偏置始终为1&#xff0c;所以颜色加深。 图1-1 感知机模型 引入新函数(激活函数&#xff09;&#xff…

HTML+CSS:导航栏组件

效果演示 实现了一个导航栏的动画效果&#xff0c;当用户点击导航栏中的某个选项时&#xff0c;对应的选项卡会向左平移&#xff0c;同时一个小圆圈会出现在选项卡的中心&#xff0c;表示当前选项卡的位置。这个效果可以让用户更加清晰地了解当前页面的位置和内容。 Code <…

FFMPEG 之 DXVA2 硬解

一&#xff1a;FFMPEG 支持的硬解方式有很多&#xff1a; DXVA2、D3D11VA、CUDA、QSV、OPENCL、DRM、VAAPI、VDPAU、VIDEOTOOLBOX、MEDIACODEC。 有的支持 Windows 平台&#xff0c;有的支持 linux 平台&#xff0c;有的支持 apple ios 平台&#xff0c;…