赞
踩
springcloud-nacos官网参考文档: https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/en-us/index.html.
可以使用翻译,或者找一个360、qq浏览器等进行网页翻译。如:
Nacos 注册中心的实现原理:
图中的流程是大家所熟悉的,不同的是在Nacos 中,服务注册时在服务端本地会通过轮询注册中心集群节点地址进行服务得注册,在注册中心上,即Nacos Server上采用了Map保存实例信息,当然配置了持久化的服务会被保存到数据库中,在服务的调用方,为了保证本地服务实例列表的动态感知,Nacos与其他注册中心不同的是,采用了 Pull/Push同时运作的方式。通过这些我们对Nacos注册中心的原理有了一定的了解
步骤介绍:
(1)新建模块:cloudalibaba-provider-payment9001
(2)父pom:
本地pom:
<dependencies> <!--alibaba-nacos-discovery--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--web两件套--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.2.2.RELEASE</version> </dependency> <dependency><!--热部署--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
(3)application.yml:
server: port: 9001 spring: application: name: nacos-payment-provider #此微服务名字 cloud: nacos: discovery: server-addr: localhost:8848 #Nacos Server 监听器的IP和端口 management: endpoints: web: exposure: include: '*' #把要监控的端点暴露出来
或者
server.port=9001
spring.application.name=nacos-payment-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*
(4)主启动类:com.fan.springcloud.NacosPayment9001
说明:@EnableDiscoveryClient 开启,说明我们提供者和消费者都是作为注册中心的一个客户端的。
package com.fan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //开启服务发现客户端的注解
public class NacosPayment9001 {
public static void main(String[] args) {
SpringApplication.run(NacosPayment9001 .class, args);
}
}
业务类controller: controller.NacosPaymentController
package com.fan.springcloud.controller; 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.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController @Slf4j public class NacosPaymentController { @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/nacos/{id}") //访问测试路径 public String getPayment(@PathVariable("id") String id){ return "nacos 注册,提供者的端口号:"+serverPort+"\t id:"+id ; } }
(6)测试:
让nacos服务后台一直运行着,然后启动我们刚才新建的模块cloudalibaba-provider-payment9001
然后访问页面看注册到nacos中的微服务:http://localhost:8848/nacos/
然后查看服务管理:
http://localhost:8848/nacos/
然后可以查看该微服务的详情,示例代码等。
第一步:要使用端点暴露,需要引入相关的依赖:spring-boot-starter-actuator和spring-boot-starter-web
启用并暴露端点
从Spring Boot 2.x开始,我们需要手动的去启用和暴露端点。默认情况下,除了/shutdown之外的所有端点都是可用的,同时只有/health和/info端点是对外暴露的。即便我们为应用程序配置了多个根上下文,所有的端点都可以通过/actuator来进行查找。
这意味着,一旦我们引入了合适的starter(spring-boot-starter-actuator和spring-boot-starter-web)到maven配置中,我们便可以通过http://localhost:8080/actuator/health和http://localhost:8080/actuator/info来进行两个端点的访问。
访问http://localhost:8080/actuator,返回可用的端点列表如下:
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},
"health":{"href":"http://localhost:8080/actuator/health","templated":false},
"info":{"href":"http://localhost:8080/actuator/info","templated":false}}}
第二步:配置 暴露所有端点:
下面通过配置来暴露除了/shutdown之外的所有端点,在application.properties中进行如下配置:
management.endpoints.web.exposure.include=*
或者yml方式:
重启,再次访问/actuator,可以看到除了/shutdown之外的其他所有端点:
测试:
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},
"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},
"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},
"health":{"href":"http://localhost:8080/actuator/health","templated":false},
"info":{"href":"http://localhost:8080/actuator/info","templated":false},
"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},
"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},
"env":{"href":"http://localhost:8080/actuator/env","templated":false},
"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},
"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},
"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},
"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},
"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},
"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}}
由于端点可能包含敏感信息,应该仔细的考虑什么时候暴露它们,下面的表格展示了内置端点的暴露情况:
步骤和上面的9001类似。此处省略。
或者拷贝9001成为一个9002:
注意:拷贝后的端口这里是9011:所以访问的时候要换成:localhost:9011/payment/nacos/1
其他非拷贝测试:这里的访问路径对应controller中的mapping方法:
网页测试9001:localhost:9001/payment/nacos/1
网页测试9002:localhost:9002/payment/nacos/1
nacos控制台查看,统一的微服务名称nacos-payment-provider下有两个实例的,我们查看详情,如下:
创建消费者微服务模块的步骤:
(1)新建模块:cloudalibaba-consumer-nacos-order83
(2)pom:同样是添加alibaba-nacos-discovery 依赖:
<dependencies> <!--alibaba-nacos-discovery--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--web两件套--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.2.2.RELEASE</version> </dependency> <dependency><!--热部署--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies>
注意,此alibaba-nacos-discovery依赖自带负载均衡:看依赖关系树
(3)application.yml:
server:
port: 83
spring:
application:
name: nacos-order-consumer #此微服务名字
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos Server 监听器的IP和端口,即将此微服务注册进nacos
#这里消费端不用暴露端点
#消费者将要去访问的微服务的名称(注册成功进nacos的微服务提供者)
service-url:
nacos-user-service: http://nacos-payment-provider
(4)主启动类:com.fan.springcloud.OrderNacosMain83
说明:@EnableDiscoveryClient 开启,说明我们提供者和消费者都是作为注册中心的一个客户端的。
package com.fan.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class OrderNacosMain83 {
public static void main(String[] args) {
SpringApplication.run(OrderNacosMain83.class,args);
}
}
负载均衡的配置类:config.ApplicationContextConfig
package com.fan.springcloud.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 ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
(5)业务类controller: controller.OrderNacosController
package com.fan.springcloud.controller; 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.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController @Slf4j public class OrderNacosController { @Resource private RestTemplate restTemplate; //注入微服务名称 @Value("${service-url.nacos-user-service}") private String serverUrl; @GetMapping(value = "/consumer/payment/nacos/{id}") //消费端访问测试地址 public String getPaymentInfo(@PathVariable("id") Long id){ //远程调用:远程服务地址(serverUrl)+远程访问路径+远程访问所需参数 return restTemplate.getForObject(serverUrl+"/payment/nacos/"+id,String.class); } }
(6)测试:localhost:83/consumer/payment/nacos/13
测试结果:
负载均衡的测试:反复刷新此访问路径:localhost:83/consumer/payment/nacos/13
发现端口号编号。自动实现了负载均衡的效果。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。