广州网站建设十年乐云seo,做网站用啥软件,怎样接做网站和软件的活,项目运营方案系列文章目录
第一章 Java线程池技术应用 第二章 CountDownLatch和Semaphone的应用 第三章 Spring Cloud 简介 第四章 Spring Cloud Netflix 之 Eureka 第五章 Spring Cloud Netflix 之 Ribbon 文章目录 系列文章目录[TOC](文章目录) 前言1、负载均衡1.1、服务端负载均衡1.2、…系列文章目录
第一章 Java线程池技术应用 第二章 CountDownLatch和Semaphone的应用 第三章 Spring Cloud 简介 第四章 Spring Cloud Netflix 之 Eureka 第五章 Spring Cloud Netflix 之 Ribbon 文章目录 系列文章目录[TOC](文章目录) 前言1、负载均衡1.1、服务端负载均衡1.2、客户端负载均衡 2、Ribbon实现服务间调用2.1、pom.xml配置2.2、application.yml配置2.3、bean配置类2.4、编写调用Eureka的代码2.4.1、定义用户服务接口2.4.2、编写用户服务实现类2.4.3、编写用户服务控制层代码2.4.4、统一返回结果2.4.5、统一异常处理 2.5、启动项目访问接口2.5.1、启动项目2.5.2、访问接口 总结
前言
Spring Cloud Ribbon 是一套基于 Netflix Ribbon 实现的客户端负载均衡和服务调用工具其主要功能是提供客户端的负载均衡算法和服务调用。
今天我们以电商微服务为例来讲解Eureka 、Ribbon在微服务治理方面的实战应用。
1、负载均衡
负载均衡Load Balance 简单点说就是将用户的请求平摊分配到多个服务器上运行以达到扩展服务器带宽、增强数据处理能力、增加吞吐量、提高网络的可用性和灵活性的目的。 常见的负载均衡方式有两种服务端负载均衡、客户端负载均衡
1.1、服务端负载均衡 1.2、客户端负载均衡 2、Ribbon实现服务间调用
Ribbon 可以与 RestTemplateRest 模板配合使用以实现微服务之间的调用 示例 建立C端API工程customer-api
2.1、pom.xml配置
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.hqyj/groupIdartifactIdSpringCloud/artifactIdversion0.0.1-SNAPSHOT/version/parentartifactIdcustomer-api/artifactIdnamecustomer-api/namedescriptioncustomer-api/descriptionpropertiesjava.version1.8/java.versionmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!--devtools 开发工具--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependency!--Spring Boot 测试--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!--junit 测试--dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/version/dependency!-- 修改后立即生效热部署 --dependencygroupIdorg.springframework/groupIdartifactIdspringloaded/artifactIdversion1.2.8.RELEASE/version/dependencydependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdcom.hqyj/groupIdartifactIdcommon-api/artifactIdversion0.0.1-SNAPSHOT/version/dependency/dependencies/project
2.2、application.yml配置
server:port: 80eureka:client:register-with-eureka: false #本微服务为服务消费者不需要将自己注册到服务注册中心fetch-registry: true #本微服务为服务消费者需要到服务注册中心搜索服务service-url:defaultZone: http://localhost:7001/eureka
2.3、bean配置类
配置RestTemplate、开启负载均衡
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;/**** title bean配置类* desctption 配置RestTemplate、开启负载均衡* author kelvin* create 2023/5/11 14:33**/
Configuration
public class ConfigBean {Bean //将 RestTemplate 注入到容器中LoadBalanced //在客户端使用 RestTemplate 请求服务端时开启负载均衡Ribbonpublic RestTemplate getRestTemplate() {return new RestTemplate();}
}
2.4、编写调用Eureka的代码
2.4.1、定义用户服务接口
import com.hqyj.common.model.UserInfo;
import java.util.List;/**** title 用户服务 接口* desctption 用户服务* author kelvin* create 2023/5/11 14:22**/
public interface UserConsumerService {/*** 获取用户信息列表* return*/public ListUserInfo userInfoList();}2.4.2、编写用户服务实现类
import com.hqyj.common.model.UserInfo;
import com.hqyj.customerapi.service.UserConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;import java.util.List;/**** title 用户服务 实现类* desctption 用户服务* author kelvin* create 2023/5/11 14:22**/
Service
public class UserConsumerServiceImpl implements UserConsumerService {private String REST_URL_PROVIDER_PREFIX http://USER-SERVICE;Autowiredprivate RestTemplate restTemplate;/*** 获取用户信息列表* return*/Overridepublic ListUserInfo userInfoList() {return this.restTemplate.getForObject(this.REST_URL_PROVIDER_PREFIX /user/userInfoList,List.class);}
}2.4.3、编写用户服务控制层代码
import com.hqyj.common.model.UserInfo;
import com.hqyj.customerapi.service.UserConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;/**** title UserConsumerController* desctption 用户控制层* author kelvin* create 2023/5/11 14:22**/
RestController
RequestMapping(/user)
public class UserConsumerController {Autowiredprivate UserConsumerService userConsumerService;GetMapping(/userInfoList)public ListUserInfo userInfoList(){return userConsumerService.userInfoList();}
}2.4.4、统一返回结果
在公共模块common-api里面添加DTO
import lombok.Data;/**** title 统一返回格式类* param T* desctption 统一返回格式* author kelvin* create 2023/5/11 14:28**/
Data
public class ResponseDTOT {/*** 返回编码*/private Integer code;/*** 统一返回消息*/private String message;/*** 统一返回数据体*/private T data;}2.4.5、统一异常处理
实现 ResponseBodyAdvice接口
import com.hqyj.common.dto.ResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;/**** title 统一异常处理类* desctption 统一异常处理* author kelvin* create 2023/5/11 14:33**/
RestControllerAdvice(basePackages com.hqyj.customerapi.controller)
Slf4j
public class ControllerResponseAdvice implements ResponseBodyAdviceObject {Overridepublic boolean supports(MethodParameter returnType, Class? extends HttpMessageConverter? converterType) {//true为织入通知return true;}Overridepublic Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class? extends HttpMessageConverter? selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {ResponseDTOObject objectResponseDTO new ResponseDTO();objectResponseDTO.setCode(200);objectResponseDTO.setData(body);return objectResponseDTO;}/*** 统一异常处理* param e* return*/ExceptionHandler(value Exception.class)public Object exception(Exception e){log.error(系统异常,e);ResponseDTOObject objectResponseDTO new ResponseDTO();objectResponseDTO.setCode(500);objectResponseDTO.setMessage(系统异常);return objectResponseDTO;}
}2.5、启动项目访问接口
2.5.1、启动项目
需要上一章节的2个项目先运行
2.5.2、访问接口
访问地址http://localhost/user/userInfoList 总结
在以前的分布式项目里我们使用zookeeper、redis等来存放服务注册信息在客户端调用服务时需要自己手动获取可用服务清单使用起来非常麻烦对初级开发人员特别不友好一不小心就犯错比如zookeeper依赖版本冲突、zookeeper\redis集群地址填写错误、zookeeper\redis配置项缺失等。 Ribbon的出现解决了上述部分问题而且Ribbon属于Netflix生态里的组件与Eureka可以很好的集成起来组合使用非常方便。 文章转载自: http://www.morning.rfycj.cn.gov.cn.rfycj.cn http://www.morning.kwpnx.cn.gov.cn.kwpnx.cn http://www.morning.fhtmp.cn.gov.cn.fhtmp.cn http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.kdgcx.cn.gov.cn.kdgcx.cn http://www.morning.fssmx.com.gov.cn.fssmx.com http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.qywfw.cn.gov.cn.qywfw.cn http://www.morning.qwfl.cn.gov.cn.qwfl.cn http://www.morning.wsnbg.cn.gov.cn.wsnbg.cn http://www.morning.ahscrl.com.gov.cn.ahscrl.com http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.rcjyc.cn.gov.cn.rcjyc.cn http://www.morning.tbzcl.cn.gov.cn.tbzcl.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.fqsxf.cn.gov.cn.fqsxf.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.pwsnr.cn.gov.cn.pwsnr.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.huihuangwh.cn.gov.cn.huihuangwh.cn http://www.morning.zmnyj.cn.gov.cn.zmnyj.cn http://www.morning.xnflx.cn.gov.cn.xnflx.cn http://www.morning.xdqrz.cn.gov.cn.xdqrz.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.xnpj.cn.gov.cn.xnpj.cn http://www.morning.gypcr.cn.gov.cn.gypcr.cn http://www.morning.tcxzn.cn.gov.cn.tcxzn.cn http://www.morning.jlschmy.com.gov.cn.jlschmy.com http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.mxnhq.cn.gov.cn.mxnhq.cn http://www.morning.cwtrl.cn.gov.cn.cwtrl.cn http://www.morning.xqgtd.cn.gov.cn.xqgtd.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.glncb.cn.gov.cn.glncb.cn http://www.morning.tqrxm.cn.gov.cn.tqrxm.cn http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.rkjz.cn.gov.cn.rkjz.cn http://www.morning.wwgpy.cn.gov.cn.wwgpy.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.jrqbr.cn.gov.cn.jrqbr.cn http://www.morning.kjmws.cn.gov.cn.kjmws.cn http://www.morning.pqcbx.cn.gov.cn.pqcbx.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.hryhq.cn.gov.cn.hryhq.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.ktrh.cn.gov.cn.ktrh.cn http://www.morning.gtmdq.cn.gov.cn.gtmdq.cn http://www.morning.fbzdn.cn.gov.cn.fbzdn.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.pmlgr.cn.gov.cn.pmlgr.cn http://www.morning.sbrjj.cn.gov.cn.sbrjj.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.bwxph.cn.gov.cn.bwxph.cn http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.bfcrp.cn.gov.cn.bfcrp.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.yllym.cn.gov.cn.yllym.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.ksqyj.cn.gov.cn.ksqyj.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.bqwsz.cn.gov.cn.bqwsz.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.grbgn.cn.gov.cn.grbgn.cn http://www.morning.fjlsfs.com.gov.cn.fjlsfs.com http://www.morning.zcwwb.cn.gov.cn.zcwwb.cn http://www.morning.clyhq.cn.gov.cn.clyhq.cn http://www.morning.zdgp.cn.gov.cn.zdgp.cn http://www.morning.bpmdg.cn.gov.cn.bpmdg.cn http://www.morning.gcfrt.cn.gov.cn.gcfrt.cn http://www.morning.bmmhs.cn.gov.cn.bmmhs.cn