mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 spring cloud alibaba nacos 注册 - 监控端点
This commit is contained in:
@@ -123,7 +123,7 @@
|
||||
# Spring Cloud Alibaba 专栏
|
||||
|
||||
* [《芋道 Spring Cloud Alibaba 注册中心 Nacos 入门》](http://www.iocoder.cn/Spring-Cloud-Alibaba/Nacos-Discovery/?github) 对应 [labx-01](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-01)
|
||||
* 《芋道 Spring Cloud Netflix 负载均衡 Ribbon 入门》
|
||||
* [《芋道 Spring Cloud Netflix 负载均衡 Ribbon 入门》](http://www.iocoder.cn/Spring-Cloud-Netflix/Ribbon/?github) 对应 [labx-02](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-02)
|
||||
* 《芋道 Spring Cloud 声明式调用 OpenFeign 入门》
|
||||
* 《芋道 Spring Cloud Alibaba 服务调用 Dubbo 入门》
|
||||
* 《芋道 Spring Cloud Alibaba 配置中心 Nacos 入门》
|
||||
@@ -145,7 +145,7 @@
|
||||
## 服务调用
|
||||
|
||||
* 《芋道 Spring Cloud Alibaba 服务调用 Dubbo 入门》
|
||||
* 《芋道 Spring Cloud Netflix 负载均衡 Ribbon 入门》
|
||||
* [《芋道 Spring Cloud Netflix 负载均衡 Ribbon 入门》](http://www.iocoder.cn/Spring-Cloud-Netflix/Ribbon/?github) 对应 [labx-02](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-02)
|
||||
* 《芋道 Spring Cloud 声明式调用 OpenFeign 入门》
|
||||
|
||||
## 服务容错
|
||||
|
||||
64
labx-01/labx-01-sca-nacos-discovery-demo02-consumer/pom.xml
Normal file
64
labx-01/labx-01-sca-nacos-discovery-demo02-consumer/pom.xml
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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-01-sca-nacos-discovery-demo02-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>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,69 @@
|
||||
package cn.iocoder.springcloudalibaba.labx01.nacosdemo.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.discovery.DiscoveryClient;
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootApplication
|
||||
// @EnableDiscoveryClient
|
||||
public class DemoConsumerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoConsumerApplication.class, args);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfiguration {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@RestController
|
||||
static class TestController {
|
||||
|
||||
@Autowired
|
||||
private DiscoveryClient discoveryClient;
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
@Autowired
|
||||
private LoadBalancerClient loadBalancerClient;
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String hello(String name) {
|
||||
// 获得服务 `demo-provider` 的一个实例
|
||||
ServiceInstance instance;
|
||||
if (true) {
|
||||
// 获取服务 `demo-provider` 对应的实例列表
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances("demo-provider");
|
||||
// 选择第一个
|
||||
instance = instances.size() > 0 ? instances.get(0) : null;
|
||||
} else {
|
||||
instance = loadBalancerClient.choose("demo-provider");
|
||||
}
|
||||
// 发起调用
|
||||
if (instance == null) {
|
||||
throw new IllegalStateException("获取不到实例");
|
||||
}
|
||||
String targetUrl = instance.getUri() + "/echo?name=" + name;
|
||||
String response = restTemplate.getForObject(targetUrl, String.class);
|
||||
// 返回结果
|
||||
return "consumer:" + response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
namespace: 14226a0d-799f-424d-8905-162f6a8bf409 # Nacos 命名空间 dev 的编号
|
||||
@@ -0,0 +1,7 @@
|
||||
spring:
|
||||
cloud:
|
||||
nacos:
|
||||
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
namespace: bc8c8c2d-bd85-42bb-ada3-1a8f940ceb20 # Nacos 命名空间 uat 的编号
|
||||
@@ -0,0 +1,6 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-consumer # Spring 应用名
|
||||
|
||||
server:
|
||||
port: 28080 # 服务器端口。默认为 8080
|
||||
@@ -1,6 +1,6 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-provider # Spring 应用名
|
||||
spring:
|
||||
application:
|
||||
name: demo-provider # Spring 应用名
|
||||
|
||||
server:
|
||||
port: 18080 # 服务器端口。默认为 8080
|
||||
server:
|
||||
port: 18080 # 服务器端口。默认为 8080
|
||||
|
||||
70
labx-01/labx-01-sca-nacos-discovery-demo03-consumer/pom.xml
Normal file
70
labx-01/labx-01-sca-nacos-discovery-demo03-consumer/pom.xml
Normal file
@@ -0,0 +1,70 @@
|
||||
<?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-01-sca-nacos-discovery-demo03-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>
|
||||
|
||||
<!-- 实现对 Actuator 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,69 @@
|
||||
package cn.iocoder.springcloudalibaba.labx01.nacosdemo.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.discovery.DiscoveryClient;
|
||||
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;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@SpringBootApplication
|
||||
// @EnableDiscoveryClient
|
||||
public class DemoConsumerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoConsumerApplication.class, args);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfiguration {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@RestController
|
||||
static class TestController {
|
||||
|
||||
@Autowired
|
||||
private DiscoveryClient discoveryClient;
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
@Autowired
|
||||
private LoadBalancerClient loadBalancerClient;
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String hello(String name) {
|
||||
// 获得服务 `demo-provider` 的一个实例
|
||||
ServiceInstance instance;
|
||||
if (true) {
|
||||
// 获取服务 `demo-provider` 对应的实例列表
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances("demo-provider");
|
||||
// 选择第一个
|
||||
instance = instances.size() > 0 ? instances.get(0) : null;
|
||||
} else {
|
||||
instance = loadBalancerClient.choose("demo-provider");
|
||||
}
|
||||
// 发起调用
|
||||
if (instance == null) {
|
||||
throw new IllegalStateException("获取不到实例");
|
||||
}
|
||||
String targetUrl = instance.getUri() + "/echo?name=" + name;
|
||||
String response = restTemplate.getForObject(targetUrl, String.class);
|
||||
// 返回结果
|
||||
return "consumer:" + response;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
spring:
|
||||
application:
|
||||
name: demo-consumer # Spring 应用名
|
||||
cloud:
|
||||
nacos:
|
||||
# Nacos 作为注册中心的配置项,对应 NacosDiscoveryProperties 配置类
|
||||
discovery:
|
||||
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
|
||||
service: ${spring.application.name} # 注册到 Nacos 的服务名。默认值为 ${spring.application.name}。
|
||||
|
||||
server:
|
||||
port: 28080 # 服务器端口。默认为 8080
|
||||
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
|
||||
endpoint:
|
||||
# Health 端点配置项,对应 HealthProperties 配置类
|
||||
health:
|
||||
enabled: true # 是否开启。默认为 true 开启。
|
||||
show-details: ALWAYS # 何时显示完整的健康信息。默认为 NEVER 都不展示。可选 WHEN_AUTHORIZED 当经过授权的用户;可选 ALWAYS 总是展示。
|
||||
@@ -16,6 +16,9 @@
|
||||
<module>labx-01-sca-nacos-discovery-demo01-consumer</module>
|
||||
|
||||
<module>labx-01-sca-nacos-discovery-demo02-provider</module>
|
||||
<module>labx-01-sca-nacos-discovery-demo02-consumer</module>
|
||||
|
||||
<module>labx-01-sca-nacos-discovery-demo03-consumer</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user