1.springcloud微服务架构搭建 之 《springboot自动装配Redis》
2.springcloud微服务架构搭建 之 《springboot集成nacos注册中心》
ribbon工作原理自己网上百度,说的都很详细
目录
1.项目引入openfeign和ribbon配置
2.新建lilock-ribbon-spring-boot-starter
3.增加restTemplate配置类
4.配置Ribbon配置类
5.配置ribbon自动装配spring.factories
6.新建一个cms业务服务,开始测试
7.配置ribbon参数
8.新建测试类进行测试
9.在user服务调用
openfeign集成了ribbon,需要单独给ribbon配置参数信息
org.springframework.cloud
spring-cloud-starter-openfeign
${spring.cloud.version}
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
${spring.cloud.version}
org.apache.httpcomponents
httpclient
${httpclient.version}
引入ribbon坐标
lilock-commons
lilock.cn
1.0-SNAPSHOT
4.0.0
1.0-SNAPSHOT
lilock-feign-spring-boot-starter
org.springframework.boot
spring-boot-autoconfigure
org.springframework.boot
spring-boot-configuration-processor
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.apache.httpcomponents
httpclient
org.projectlombok
lombok
package lilock.cn.common.ribbon.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "lilock.rest-template")
@Data
public class RestTemplateProperties {
/**
* 最大链接数
*/
private int maxTotal = 300;
/**
* 同路由最大并发数
*/
private int maxPerRoute = 200;
/**
* 读取超时时间 ms
*/
private int readTimeout = 30000;
/**
* 链接超时时间 ms
*/
private int connectTimeout = 15000;
/***
* 最大重试次数
*/
private int maxAutoRetries = 0;
/**
* 是否支持重试
*/
private boolean enableRetry = false;
}
package lilock.cn.common.ribbon.config;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
import lilock.cn.common.ribbon.properties.RestTemplateProperties;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@EnableConfigurationProperties
public class RibbonConfig {
@Autowired
private RestTemplateProperties restTemplateProperties;
@Bean
@LoadBalanced
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
RestTemplate restTemplate = new RestTemplate();
//设置resttemplate http工厂
restTemplate.setRequestFactory(factory);
return restTemplate;
}
/**
* 配置默认负载均衡策略-轮训
* @return
*/
@Bean
@Primary
public IRule ribbonRule(){
IRule rule = new RoundRobinRule();
return rule;
}
@Bean
@Primary
public ClientHttpRequestFactory httpRequestFactory(HttpClient httpclient){
//设置httpclient
return new HttpComponentsClientHttpRequestFactory(httpclient);
}
/**
* 配置httpclient 参数
* @return
*/
@Bean
public HttpClient httpClient(){
Registry registry = RegistryBuilder.create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
// 最大链接数
connectionManager.setMaxTotal(restTemplateProperties.getMaxTotal());
// 同路由并发数20
connectionManager.setDefaultMaxPerRoute(restTemplateProperties.getMaxPerRoute());
RequestConfig requestConfig = RequestConfig.custom()
// 读超时
.setSocketTimeout(restTemplateProperties.getReadTimeout())
// 链接超时
.setConnectTimeout(restTemplateProperties.getConnectTimeout())
// 链接不够用的等待时间
.setConnectionRequestTimeout(restTemplateProperties.getReadTimeout())
.build();
return HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(connectionManager)
.setRetryHandler(new DefaultHttpRequestRetryHandler(restTemplateProperties.getMaxAutoRetries(),restTemplateProperties.isEnableRetry()))
.build();
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
lilock.cn.common.ribbon.properties.RestTemplateProperties,\
lilock.cn.common.ribbon.config.RibbonConfig
lilock-modules
lilock.cn
1.0-SNAPSHOT
4.0.0
1.0-SNAPSHOT
lilock-service-cms
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
lilock.cn
lilock-feign-spring-boot-starter
${project.version}
io.springfox
springfox-swagger2
io.springfox
springfox-swagger-ui
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-maven-plugin
server.port 可以配置不同的端口,cms服务启动2次模拟负载,我这里端口分别配置的是8990,8991
lilock:
rest-template:
max-total: 300 #最大连接数
max-per-route: 200 #路由最大数
max-auto-retries: 0 #最大重试次数
enable-retry: false #是否开启重试
connect-timeout: 15000 #链接超时时间
read-timeout: 30000 #请求超时时间
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
register-enabled: true
namespace: dev
application:
name: lilock-service-cms
server:
port: 8990
package lilock.cn.cms.controller; import io.swagger.annotations.Api; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/cms") @Api(value = "cms接口管理",tags = {"CMS接口管理"}) @Slf4j public class CmsController { @Value("${server.port}") private Integer port; @Value("${spring.application.name}") private String service; @GetMapping("/hello") public String getHello(){ return "["+ service +"]点前服务端口号是:["+ port +"]返回内容 HELLO" ; } }
package lilock.cn.user.controller;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@RequestMapping(path = "/user")
@Api(value = "系统用户",tags = {"系统用户"})
@Slf4j
public class UserController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/testTemplate")
public String testTemplate(){
String value = restTemplate.getForObject("http://lilock-service-cms/api/cms/hello",String.class);
log.info("获取当前请求结果:{}",value);
return value;
}
}
可以看到8990,8991 2个端口对应的服务轮训返回,说明配置的轮训策略生效