Spring Cloud(Finchley.RCI) (四) Spring Cloud创建服务消费者(Ribbon)

2024-06-07 16:32

本文主要是介绍Spring Cloud(Finchley.RCI) (四) Spring Cloud创建服务消费者(Ribbon),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在微服务架构中, 业务都会被拆分成一个独立的服务, 服务与服务的通讯是基于http restful的, Spring Cloud有两种服务调用方式, 一种是ribbon+restTemplate, 另一种是feign.

Ribbon简介

Ribbon是一个负载均衡客户端, 可以很好的控制http和tcp的一些行为。

准备工作

启动服务提供者, 端口号为: 8762

修改配置文件的端口号为: 8763, 启动后再Eureka中会注册两个实例, 相当于一个小集群

创建服务消费者

创建一个工程名为: hello-spring-cloud-web-admin-ribbon的服务消费者项目, pol.xml配置如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.funtl</groupId><artifactId>hello-spring-cloud-dependencies</artifactId><version>1.0.0-SNAPSHOT</version><relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath></parent><artifactId>hello-spring-cloud-web-admin-ribbon</artifactId><packaging>jar</packaging><name>hello-spring-cloud-web-admin-ribbon</name><url>http://www.funtl.com</url><inceptionYear>2018-Now</inceptionYear><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><dependency><groupId>net.sourceforge.nekohtml</groupId><artifactId>nekohtml</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><mainClass>com.funtl.hello.spring.cloud.web.admin.ribbon.WebAdminRibbonApplication</mainClass></configuration></plugin></plugins></build>
</project>

主要是增加了Ribbon的依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

Application

通过@EnableDiscoveryClient注解注册到服务中心

package com.funtl.hello.spring.cloud.web.admin.ribbon;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient
@SpringBootApplication
public class WebAdminRibbonApplication {public static void main(String[] args) {SpringApplication.run(WebAdminRibbonApplication.class, args);}

application.yml

设置程序端口号为: 8764

spring:application:name: hello-spring-cloud-web-admin-ribbonthymeleaf:cache: falsemode: LEGACYHTML5encoding: UTF-8servlet:content-type: text/html
server:port: 8764
eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/

Configuration

配置注入RestTemplate的Bean, 并通过@LoadBalanced注解表明开启负载均衡功能

package com.funtl.hello.spring.cloud.web.admin.ribbon.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;@Configuration
public class RestTemplateConfiguration {@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}
}

创建测试用的Service

package com.funtl.hello.spring.cloud.web.admin.ribbon.service;import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;@Service
public class AdminService {@Autowiredprivate RestTemplate restTemplate;public String sayHi(String message){return restTemplate.getForObject("http://hello-spring-cloud-service-admin/hi?message="+message, String.class);}
}

创建测试用的Controller

package com.funtl.hello.spring.cloud.web.admin.ribbon.controller;import com.funtl.hello.spring.cloud.web.admin.ribbon.service.AdminService;
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;@RestController
public class AdminController {@Autowiredprivate AdminService adminService;@RequestMapping(value="hi", method= RequestMethod.GET)public String sayHi(String message){return adminService.sayHi(message);}
}

测试访问

在浏览器上多次访问http://localhost:8764/hi?message=HelloRibbon

这篇关于Spring Cloud(Finchley.RCI) (四) Spring Cloud创建服务消费者(Ribbon)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1039700

相关文章

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

RabbitMQ消息总线方式刷新配置服务全过程

《RabbitMQ消息总线方式刷新配置服务全过程》SpringCloudBus通过消息总线与MQ实现微服务配置统一刷新,结合GitWebhooks自动触发更新,避免手动重启,提升效率与可靠性,适用于配... 目录前言介绍环境准备代码示例测试验证总结前言介绍在微服务架构中,为了更方便的向微服务实例广播消息,

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避

SpringBoot整合Dubbo+ZK注册失败的坑及解决

《SpringBoot整合Dubbo+ZK注册失败的坑及解决》使用Dubbo框架时,需在公共pom添加依赖,启动类加@EnableDubbo,实现类用@DubboService替代@Service,配... 目录1.先看下公共的pom(maven创建的pom工程)2.启动类上加@EnableDubbo3.实

SpringBoot整合(ES)ElasticSearch7.8实践

《SpringBoot整合(ES)ElasticSearch7.8实践》本文详细介绍了SpringBoot整合ElasticSearch7.8的教程,涵盖依赖添加、客户端初始化、索引创建与获取、批量插... 目录SpringBoot整合ElasticSearch7.8添加依赖初始化创建SpringBoot项

JAVA覆盖和重写的区别及说明

《JAVA覆盖和重写的区别及说明》非静态方法的覆盖即重写,具有多态性;静态方法无法被覆盖,但可被重写(仅通过类名调用),二者区别在于绑定时机与引用类型关联性... 目录Java覆盖和重写的区别经常听到两种话认真读完上面两份代码JAVA覆盖和重写的区别经常听到两种话1.覆盖=重写。2.静态方法可andro

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种