Spring官网:https://spring.io/projects/spring-cloud-alibaba
GitHub:https://github.com/alibaba/spring-cloud-alibaba
GitHub中文文档:https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md
Spring Cloud Alibaba参考文档:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html
SpringCloud Alibaba的依赖:(已经在父工程的pom中引入了)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.2.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
官网:[https://nacos.io/zh-cn/](https://nacos.io/zh-cn/)
GitHub:https://github.com/alibaba/Nacos
在docker上安装nacos
拉取nacos镜像:
docker pull nacos/nacos-server
运行nacos:
docker run --env MODE=standalone --name nacos -d -p 8848:8848 nacos/nacos-server
在浏览器输入:http://10.211.55.17:8848/nacos/
(10.211.55.17是我linux的IP地址)
账号和密码都是nacos
。
官方文档:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html
新建模块cloudalibaba-provider-payment9001
pom
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
yml
server:
port: 9001
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: 10.211.55.17:8848 #配置的Nacos地址(本机的写localhost:8848,服务器的写IP地址)
management:
endpoints:
web:
exposure:
include: '*'
主启动类
@EnableDiscoveryClient
@SpringBootApplication
public class PaymentMain9001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain9001.class, args);
}
}
新建controller.PaymentController
@RestController
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/nacos/{id}")
public String getPayment(@PathVariable("id") Integer id){
return "nacos registry, serverPort: " + serverPort + "\t id: " + id;
}
}
新建模块cloudalibaba-consumer-nacos-order83
pom(nacos集成了ribbon,实现负载均衡)
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.angenin.springcloudgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
yml
server:
port: 83
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: 10.211.55.17:8848 #配置的Nacos地址(本机的写localhost:8848,服务器的写IP地址)
#消费者要访问的微服务名称(成功注册进nacos的服务提供者)
service-url:
nacos-user-service: http://nacos-payment-provider
主启动类
@EnableDiscoveryClient
@SpringBootApplication
public class OrderNacosMain83 {
public static void main(String[] args) {
SpringApplication.run(OrderNacosMain83.class, args);
}
}
新建config.ApplicationContextConfig
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
新建controller.OrderNacosController
测试
启动9001,9002,83
在浏览器输入:http://localhost:83/consumer/payment/nacos/1
在pom中导入
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
在主启动类上加上@EnableFeignClients
,激活feign。
注释掉config配置类的@Configuration
注解,不使用RestTemplate。
新建service.PaymentFeignService接口
@Component
@FeignClient(value = "nacos-payment-provider")
public interface PaymentFeignService {
@GetMapping("/payment/nacos/{id}")
public String getPayment(@PathVariable("id") Integer id);
}
在OrderNacosController中添加
@Resource
private PaymentFeignService paymentFeignService;
@GetMapping("/consumer/payment/feign/nacos/{id}")
public String paymentInfo2(@PathVariable("id") Long id){
return restTemplate.getForObject(serverURL + "/payment/feign/nacos/" + id, String.class);
}
测试
重启83。
浏览器输入:http://localhost:83/consumer/payment/feign/nacos/1
A:可用性
C:一致性
P:分区容错性
Nacos默认AP。
新建模块cloudalibaba-config-nacos-client3377
pom
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: 10.211.55.17:8848 #Nacos服务注册中心地址(本机的写localhost)
config:
server-addr: 10.211.55.17:8848 #Nacos作为配置中心地址(本机的写localhost)
file-extension: yml #指定yml格式配置
application.yml
spring:
profiles:
active: dev #表示开发环境
主启动类
@EnableDiscoveryClient
@SpringBootApplication
public class NacosConfigClientMain3377 {
public static void main(String[] args) {
SpringApplication.run(NacosConfigClientMain3377.class, args);
}
}
新建controller.ConfigClientController
@RefreshScope //支持Nacos的动态刷新功能
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo(){
return configInfo;
}
}
在Nacos中添加配置信息
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
#${prefix}-${spring.profile.active}.${file-extension}
# ${spring.application.name}-${spring.profile.active}.${file-extension}
# nacos-config-client-dev.yml
config:
info: test nacos config center, version = 2
修改3377的application.yml的active为test
。
新建配置nacos-config-client-info.yml(DEV_GROUP)
config:
info: nacos-config-client-info.yml DEV_GROUP
修改application.yml的active为active: info
修改bootstrap.yml的group为DEV_GROUP
https://nacos.io/zh-cn/docs/deployment.html
官网说明:https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html
nacos单机的持久化看这篇文章:在Docker上用Nacos1.3容器连接MySQL5.6和8.0.18容器进入持久化的具体操作
(步骤有点多,而且坑了我快一天的时间了,所以单独写在另一篇文章里,使用docker虽然启动时方便了,但是因为里面的配置已经被人改了,和官方文档不一样,想进行配置就变得不太方便。(个人吐槽))
同样,因为是在docker上配置的,所以和阳哥的方式不一样,写在另一篇文章里了。
在Docker上用3个Nacos1.3容器+一个MySQL5和8容器+一个Nginx容器进行集群的具体操作(Nacos集群版)
hostname -i
# server-addr: 10.211.55.17:8848 #配置的Nacos地址(本机的写localhost:8848,服务器的写IP地址)
#改为下面这个,填自己linux的IP地址
server-addr: 10.211.55.17:1111 #nginx的地址
启动9002
出现java.net.SocketException: Connection reset
异常。
2020-06-24 20:13:04.549 INFO 47941 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9002 (http) with context path ''
2020-06-24 20:13:04.740 ERROR 47941 --- [ restartedMain] c.a.c.n.registry.NacosServiceRegistry : nacos registry, nacos-payment-provider register failed...NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties{serverAddr='10.211.55.17:1111', endpoint='', namespace='', watchDelay=30000, logName='', service='nacos-payment-provider', weight=1.0, clusterName='DEFAULT', namingLoadCacheAtStart='false', metadata={preserved.register.source=SPRING_CLOUD}, registerEnabled=true, ip='192.168.0.109', networkInterface='', port=9002, secure=false, accessKey='', secretKey=''}},
java.lang.IllegalStateException: failed to req API:/nacos/v1/ns/instance after all servers([10.211.55.17:1111]) tried: failed to req API:10.211.55.17:1111/nacos/v1/ns/instance. code:500 msg: java.net.SocketException: Connection reset
at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:464) ~[nacos-client-1.1.1.jar:na]
at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:386) ~[nacos-client-1.1.1.jar:na]
at com.alibaba.nacos.client.naming.net.NamingProxy.registerService(NamingProxy.java:188) ~[nacos-client-1.1.1.jar:na]
...
重新启动多次后出现java.net.SocketTimeoutException: Read timed out
异常。
2020-06-24 20:32:01.365 ERROR 48776 --- [ restartedMain] c.a.c.n.registry.NacosServiceRegistry : nacos registry, nacos-payment-provider register failed...NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties{serverAddr='10.211.55.17:1111', endpoint='', namespace='', watchDelay=30000, logName='', service='nacos-payment-provider', weight=1.0, clusterName='DEFAULT', namingLoadCacheAtStart='false', metadata={preserved.register.source=SPRING_CLOUD}, registerEnabled=true, ip='192.168.0.109', networkInterface='', port=9002, secure=false, accessKey='', secretKey=''}},
com.alibaba.nacos.api.exception.NacosException: failed to req API:/api//nacos/v1/ns/instance after all servers([10.211.55.17:1111]) tried: java.net.SocketTimeoutException: Read timed out
at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:490) ~[nacos-client-1.2.0.jar:na]
at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:395) ~[nacos-client-1.2.0.jar:na]
在网上大部分问题都不一样,类似问题的解决办法尝试后还是解决不了,弄nacos真的把心态都搞崩了,希望也遇到这个问题并且已经解决了的兄弟,告知一下解决方法,谢谢。
下一篇笔记:SpringCloud入门学习笔记(19高级部分,熔断与限流【Sentinel】)
学习视频(p95-p110):https://www.bilibili.com/video/BV18E411x7eT?p=95