(一)springboot 2.4.4 整合 springCloud 、nacos
最近学习一下alibaba的微服务,找视频资料学习整合nacos作注册服务与配置中心,写了项目的一部分,与视频资料学习时的版本有些不一样,特此记录一下,也希望可以帮到一些正在学习的伙伴。
一、搭建项目工程
1、新建项目父模块,命名为springboot-nacos-demo,父模块不需要src包,删除,并在pom文件中声明为父工程
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot-nacos-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version> # 注意版本号
<relativePath/>
</parent>
</project>
2、在父模块中新建子父模块springboot-nacos-service,用于管理pom依赖,添加依赖
<?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>springboot-nacos-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<artifactId>springboot-nacos-serivce</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
2、在子父模块中新建两个子模块,分别为spring-boot-producer(服务提供者),spring-boot-customer(服务消费者)。目录结构如下
2.1、spring-boot-producer(服务提供者)
2.1.1、pom文件
<?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>springboot-nacos-serivce</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>服务提供者</description>
<artifactId>spring-boot-producer</artifactId>
</project>
2.1.2、添加配置文件 application.yml
# 服务提供者服务端口
server:
port: 7100
spring:
application:
name: nacos-producer # 服务名称
2.1.3、添加启动类
/**
* 服务提供者启动类
* @Author WEN
* @Date 2021/6/26 10:19
*/
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class,args);
}
}
2.1.4、新建一个controller包,类名与方法如下
/**
* @Author WEN
* @Date 2021/6/26 10:32
*/
@RestController
@RequestMapping("/producer")
public class ProducerController {
@GetMapping("/testProd/{id}")
public String testProd(@PathVariable("id") String id){
System.out.println("id:"+id);
return "成功调用:"+id;
}
}
成功访问:
2.1.5、目录结构如下
2.2、spring-boot-customer(服务消费者)
2.2.1、pom文件
<?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>springboot-nacos-serivce</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>服务消费者</description>
<artifactId>spring-boot-customer</artifactId>
</project>
2.2.2、添加配置文件 application.yml
# 服务消费者端口
server:
port: 7200
spring:
application:
name: nacos-customer # 服务名称
2.1.3、添加启动类
/**
* 服务消费者启动类
* @Author WEN
* @Date 2021/6/26 10:52
*/
@SpringBootApplication
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
}
2.1.4、新建一个controller包,类名与方法如下
/**
* @Author WEN
* @Date 2021/6/26 10:54
*/
@RestController
@RequestMapping("/customer")
public class TestController {
@GetMapping("/testCustomer/{id}")
public String testCustomer(@PathVariable("id") String id){
return "服务消费者——成功调用:"+id;
}
}
访问成功:
目录结构如下
二、nacos
1、因为nacos不像Eureka,nacos是一个单独的服务,因此需要去下载配置去官网下载nocas,我项目中使用的是nacos1.4.2版本的 官网下载
2、下载解压,在bin目录下双击startup.cmd启动(shutdown.cmd停止)
启动后出现报错,此错误原因是以集群的方式启动的,我本地没有配置集群,是以单机方式启动。
"nacos is starting with cluster"
Error occurred during initialization of VM
Could not reserve enough space for 2097152KB object heap
解决方案
方案一:在doc命令窗口输入 :startup.cmd -m standalone 表示以单机的方式启动
方案二:打开startup.cmd启动文件(以下的代码只取一部份),找到set MODE=“cluster”,把cluster修改为standalone,然后保存,双击startup.cmd启动
.....
set MODE="standalone"
rem set MODE="cluster" 注:rem为注释
set FUNCTION_MODE="all"
set SERVER=nacos-server
set MODE_INDEX=-1
set FUNCTION_MODE_INDEX=-1
set SERVER_INDEX=-1
set EMBEDDED_STORAGE_INDEX=-1
set EMBEDDED_STORAGE=""
启动成功后,在浏览器输入:localhost:127.0.0.1:8848/nacos,访问成功如下图
输入账号:nacos 密码:nacos,成功登录后出现如下图
三、项目整合nacos
1、在springboot-nacos-demo父工程中添加依赖
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot-nacos-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>springboot-nacos-serivce</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<!-- spring cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.0</version> # 注意版本号,我项目中sprinboot版本为2.4.4,因此springcloud版本为2020.0.0
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2、在子父模块的pom文件中添加依赖
<?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>springboot-nacos-demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>spring-boot-producer</module>
<module>spring-boot-customer</module>
</modules>
<artifactId>springboot-nacos-serivce</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
3、分别在子模块springboog-nacos-producer与springboot-nacos-customer的yml文件中添加服务注册配置
springboog-nacos-producer的yml文件
# 服务提供者服务端口
server:
port: 7100
spring:
application:
name: nacos-producer # 服务名称
# nacos 服务注册中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
springboot-nacos-customer的yml文件
# 服务消费者端口
server:
port: 7200
spring:
application:
name: nacos-customer # 服务名称
# nacos 服务注册中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
4、分别在子模块springboog-nacos-producer与springboot-nacos-customer的启动类中添加注解 ,注意:此注解加与不加并没有影响,启动后都会注册到注册中心中去,我的项目的把此注解加上并加上注解,防止自己忘记了还能找到。
// 开启nacos服务注册中心配置
@EnableDiscoveryClient
5、重启服务,在nacos服务中可以看到服务已经成功注册起来。
四、整合Fiegn,服务调用
1、在子父模块springboot-nacos-service的pom文件中添加依赖
<!-- 服务调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、在springboot-nacos-customer的启动类中添加Fiegn注解
// 开启Fign
@EnableFeignClients
3、在springboot-nacos-customer模块下新建个包,命名分别为:feign,service,并在service包中新建包,命名为impl,新建两个接口类,一个实现类
package com.customer.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* Feign调用
* @Author WEN
* @Date 2021/6/26 15:42
*/
@Component
@FeignClient("nacos-producer")
public interface ProdFeign {
/**
* 注意:使用路径传参,必须加上@PathVariable("id")注解
* @param id
* @return
*/
@GetMapping("/producer/testProd/{id}")
String testProd(@PathVariable("id") String id);
}
package com.customer.service;
/**
* @Author WEN
* @Date 2021/6/26 15:46
*/
public interface ITestService {
String testDemo(String id);
}
package com.customer.service.impl;
import com.customer.feign.ProdFeign;
import com.customer.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Author WEN
* @Date 2021/6/26 15:45
*/
@Service
public class TestServiceImpl implements ITestService {
// feign调用注入
@Autowired
private ProdFeign prodFeign;
@Override
public String testDemo(String id) {
String testProd = prodFeign.testProd(id);
return testProd;
}
}
4、并在controller层中调用service
package com.customer.controller;
import com.customer.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author WEN
* @Date 2021/6/26 10:54
*/
@RestController
@RequestMapping("/customer")
public class TestController {
@Autowired
private ITestService iTestService;
@GetMapping("/testCustomer/{id}")
public String testCustomer(@PathVariable("id") String id){
String testDemo = iTestService.testDemo(id);
return "服务消费者——成功调用:"+testDemo;
}
}
附目录结构截图:
5、完成后重新启动springboot-nacos-customer,这时启动报错No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:309) ~[spring-cloud-openfeign-core-3.0.0.jar:3.0.0]
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:335) ~[spring-cloud-openfeign-core-3.0.0.jar:3.0.0]
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:315) ~[spring-cloud-openfeign-core-3.0.0.jar:3.0.0]
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:169) ~[spring-beans-5.3.5.jar:5.3.5]
... 46 common frames omitted
查资料发现,此异常出现的原因:
由于Spring Cloud Feign在Hoxton.M2 RELEASED版本之后不再使用Ribbon而是使用spring-cloud-loadbalancer,所以不引入spring-cloud-loadbalancer会报错
因此解决方案:修改springboot-nacos-service的pom文件,添加上spring-cloud-loadbalancer依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
加入后重新启动项目,OK,成功启动,这下可以测试了,满心欢喜的在浏览器输入
http://localhost:7200/customer/testCustomer/6635555,卡!!!!!!!!页面出现
mmp,此时有一种心态炸裂的情绪的感觉!!!一看后台,又是一大串的错误
java.lang.AbstractMethodError: org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.choose(Ljava/lang/String;Lorg/springframework/cloud/client/loadbalancer/Request;)Lorg/springframework/cloud/client/ServiceInstance;
at org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient.execute(FeignBlockingLoadBalancerClient.java:88) ~[spring-cloud-openfeign-core-3.0.0.jar:3.0.0]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:119) ~[feign-core-10.10.1.jar:na]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) ~[feign-core-10.10.1.jar:na]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100) ~[feign-core-10.10.1.jar:na]
at com.sun.proxy.$Proxy79.testProd(Unknown Source) ~[na:na]
at com.customer.service.impl.TestServiceImpl.testDemo(TestServiceImpl.java:19) ~[classes/:na]
at com.customer.controller.TestController.testCustomer(TestController.java:22) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.5.jar:5.3.5]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.5.jar:5.3.5]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.3.5.jar:5.3.5]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) ~[spring-webmvc-5.3.5.jar:5.3.5]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.5.jar:5.3.5]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.5.jar:5.3.5]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060) ~[spring-webmvc-5.3.5.jar:5.3.5]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962) ~[spring-webmvc-5.3.5.jar:5.3.5]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.5.jar:5.3.5]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.5.jar:5.3.5]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.44.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.5.jar:5.3.5]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.44.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.44.jar:9.0.44]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.44.jar:9.0.44]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.5.jar:5.3.5]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.5.jar:5.3.5]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.44.jar:9.0.44]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.5.jar:5.3.5]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.5.jar:5.3.5]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.44.jar:9.0.44]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.5.jar:5.3.5]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.5.jar:5.3.5]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) [tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) [tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) [tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) [tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) [tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) [tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) [tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) [tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707) [tomcat-embed-core-9.0.44.jar:9.0.44]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.44.jar:9.0.44]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_202]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_202]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.44.jar:9.0.44]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_202]
6、报错原因:
当前我的springboot版本 2.4.4 ,springcloud版本 2020.0.0
出现这个问题nacos中ribbon会造成loadbalancer包失效,所以在包中移除ribbion依赖
7、解决方案:修改springboot-nacos-service的pom文件
修改前:
<!-- nacos -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
修改后:
<!-- nacos -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
8、重启项目,浏览器访问:http://localhost:7200/customer/testCustomer/6635555,出现如下返回,成功调用Feign。。。
四、服务熔断
1、在子父模块springboot-nacos-service,修改pom文件,添加依赖
<!-- 熔断器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
2、修改springboot-nacos-customer的yml配置文件
# 服务消费者端口
server:
port: 7200
spring:
application:
name: nacos-customer # 服务名称
# nacos 服务注册中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# Feign
feign:
hystrix:
enabled: true
3、在feign包下新建impl包,并新建类,且实现ProdFeign
package com.customer.feign.impl;
import com.customer.feign.ProdFeign;
import org.springframework.stereotype.Component;
/**
* 实现feign接口,开启熔断,该实现类,在服务正常情况下不会执行,出错时才会执行
* @Author WEN
* @Date 021/6/26 17:15
*/
@Component
public class ProdFeignImpl implements ProdFeign {
@Override
public String testProd(String id) {
return "我是服务熔断器,调用服务失败!!";
}
}
4、修改ProdFeign
package com.customer.feign;
import com.customer.feign.impl.ProdFeignImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* Feign调用
* @Author WEN
* @Date 2021/6/26 15:42
*/
@Component
@FeignClient(name = "nacos-producer", fallback = ProdFeignImpl.class)// 在此添加上实现的类
public interface ProdFeign {
/**
* 注意:使用路径传参,必须加上@PathVariable注解
* @param id
* @return
*/
@GetMapping("/producer/testProd/{id}")
String testProd(@PathVariable("id") String id);
}
5、测试,把sringboot-nacos-producer服务停止 ,重启springboot-nacos-customer服务,在浏览器访问:http://localhost:7200/customer/testCustomer/889999666,这时出现
后台报错:
feign.FeignException$ServiceUnavailable: [503] during [GET] to [http://nacos-producer/producer/testProd/889999666] [ProdFeign#testProd(String)]: [Load balancer does not contain an instance for the service nacos-producer]
at feign.FeignException.serverErrorStatus(FeignException.java:237) ~[feign-core-10.10.1.jar:na]
at feign.FeignException.errorStatus(FeignException.java:180) ~[feign-core-10.10.1.jar:na]
at feign.FeignException.errorStatus(FeignException.java:169) ~[feign-core-10.10.1.jar:na]
at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:92) ~[feign-core-10.10.1.jar:na]
at feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:96) ~[feign-core-10.10.1.jar:na]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138) ~[feign-core-10.10.1.jar:na]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) ~[feign-core-10.10.1.jar:na]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:100) ~[feign-core-10.10.1.jar:na]
at com.sun.proxy.$Proxy75.testProd(Unknown Source) ~[na:na]
at com.customer.service.impl.TestServiceImpl.testDemo(TestServiceImpl.java:19) ~[classes/:na]
at com.customer.controller.TestController.testCustomer(TestController.java:22) ~[classes/:na]
6、报错原因:报错原因及解决方案
7、解决方案:修改springboot-nacos-customer的yml配置文件
修改前:
# 服务消费者端口
server:
port: 7200
spring:
application:
name: nacos-customer # 服务名称
# nacos 服务注册中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# Feign ----修改前
feign:
hystrix:
enabled: true
修改后:
# 服务消费者端口
server:
port: 7200
spring:
application:
name: nacos-customer # 服务名称
# nacos 服务注册中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# Feign----修改后
feign:
circuitbreaker:
enabled: true
8、重启springboot-nacos-customer服务 ,访问:http://localhost:7200/customer/testCustomer/889999666,返回如下图,终于成功。
致此,springboot 2.4.4 + springcoud 2020.0.0 + nacos终于整合成功。
参考连接:
springboot整合nacos作为配置中心和注册中心
配置熔断器—Load balancer does not contain an instance for the service service-edu
报错: org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.choose