mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 spring cloud netflix ribbon 入门示例
This commit is contained in:
@@ -6,10 +6,6 @@ spring:
|
||||
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
loadbalancer:
|
||||
# Spring Cloud Loadbalancer 重启配置,对应 LoadBalancerRetryProperties 配置类
|
||||
retry:
|
||||
enabled: true # 是否开启重启,默认为 false 关闭重试。
|
||||
|
||||
server:
|
||||
port: 28080 # 服务器端口。默认为 8080
|
||||
@@ -17,7 +13,10 @@ server:
|
||||
ribbon:
|
||||
# okhttp:
|
||||
# enabled: true # 设置使用 OkHttp,对应 OkHttpRibbonConfiguration 配置类
|
||||
# restclient:
|
||||
# enabled: true # 设置使用 Jersey Client,对应 RestClientRibbonConfiguration 配置类
|
||||
httpclient:
|
||||
enabled: true # 设置使用 Apache HttpClient,对应 HttpClientRibbonConfiguration 配置类
|
||||
restclient:
|
||||
enabled: true # 设置使用 Jersey Client,对应 RestClientRibbonConfiguration 配置类
|
||||
# httpclient:
|
||||
# enabled: true # 设置使用 Apache HttpClient,对应 HttpClientRibbonConfiguration 配置类
|
||||
# ConnectTimeout: 1 # 请求的连接超时时间,单位:毫秒。默认为 1000
|
||||
# ReadTimeout: 1 # 请求的读取超时时间,单位:毫秒。默认为 1000
|
||||
|
||||
|
||||
71
labx-02/labx-02-scn-ribbon-demo06-consumer/pom.xml
Normal file
71
labx-02/labx-02-scn-ribbon-demo06-consumer/pom.xml
Normal file
@@ -0,0 +1,71 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>labx-01</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-02-scn-ribbon-demo06-consumer</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<!-- <version>4.3.1</version>-->
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,63 @@
|
||||
package cn.iocoder.springcloudnetflix.labx02.ribbondemo.consumer;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoConsumerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoConsumerApplication.class, args);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfiguration {
|
||||
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@RestController
|
||||
static class TestController {
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
@Autowired
|
||||
private LoadBalancerClient loadBalancerClient;
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String hello(String name) {
|
||||
// 获得服务 `demo-provider` 的一个实例
|
||||
ServiceInstance instance = loadBalancerClient.choose("demo-provider");
|
||||
// 发起调用
|
||||
String targetUrl = instance.getUri() + "/echo?name=" + name;
|
||||
String response = restTemplate.getForObject(targetUrl, String.class);
|
||||
// 返回结果
|
||||
return "consumer:" + response;
|
||||
}
|
||||
|
||||
@GetMapping("/hello02")
|
||||
public String hello02(String name) {
|
||||
// 直接使用 RestTemplate 调用服务 `demo-provider`
|
||||
String targetUrl = "http://demo-provider/echo?name=" + name;
|
||||
String response = restTemplate.getForObject(targetUrl, String.class);
|
||||
// 返回结果
|
||||
return "consumer:" + response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-consumer # Spring 应用名
|
||||
cloud:
|
||||
nacos:
|
||||
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
loadbalancer:
|
||||
# Spring Cloud Loadbalancer 重启配置,对应 LoadBalancerRetryProperties 配置类
|
||||
retry:
|
||||
enabled: true # 是否开启重启,默认为 false 关闭重试。
|
||||
|
||||
server:
|
||||
port: 28080 # 服务器端口。默认为 8080
|
||||
|
||||
ribbon:
|
||||
restclient:
|
||||
enabled: true # 设置使用 Jersey Client,对应 RestClientRibbonConfiguration 配置类
|
||||
ConnectTimeout: 1 # 请求的连接超时时间,单位:毫秒。默认为 1000
|
||||
ReadTimeout: 1 # 请求的读取超时时间,单位:毫秒。默认为 1000
|
||||
|
||||
user-provider:
|
||||
ribbon:
|
||||
OkToRetryOnAllOperations: true # 是否对所有操作都进行重试,默认为 false。
|
||||
MaxAutoRetries: 1 # 对当前服务的重试次数,默认为 0 次。
|
||||
MaxAutoRetriesNextServer: 2 # 重新选择服务的次数,默认为 1 次。注意,不包含第 1 次哈。
|
||||
@@ -22,8 +22,9 @@
|
||||
|
||||
<module>labx-02-scn-ribbon-demo04-consumer</module>
|
||||
|
||||
<!-- <module>labx-02-scn-ribbon-demo05-provider</module>-->
|
||||
<module>labx-02-scn-ribbon-demo05-consumer</module>
|
||||
|
||||
<module>labx-02-scn-ribbon-demo06-consumer</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user