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

相关文章

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

关于DNS域名解析服务

《关于DNS域名解析服务》:本文主要介绍关于DNS域名解析服务,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录DNS系统的作用及类型DNS使用的协议及端口号DNS系统的分布式数据结构DNS的分布式互联网解析库域名体系结构两种查询方式DNS服务器类型统计构建DNS域

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2