赞
踩
Nacos提供了四个主要功能
Nacos既可以作为服务治理中心(可以替代 Spring Cloud Eureka)也可以作为配置中心(可替代 Spring Cloud Config)。
Nacos作为一个单独的服务进行部署,不需要用户搭建项目作为服务治理节点。
注入依赖
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<!-- Nacos Spring dependency -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>0.0.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>0.6.1</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
dubbo-provider.xml:以xml形式配置dubbo
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="springcloud-alibaba-producer" />
<dubbo:registry id="nacos_service" address="nacos://127.0.0.1:8848" timeout="15"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" threads="1000" />
<!-- dubbo监控配置 -->
<dubbo:monitor protocol="registry" />
<!-- dubbo管理平台接口 -->
<bean id="UserService" class="com.luo.producer.service.impl.UserServiceImpl" />
<dubbo:service interface="com.luo.api.service.IUserService" ref="UserService" cluster="failfast"
registry="nacos_service" />
</beans>
调用接口
public interface IUserService {
public String ceshi(String input);
}
服务的实现类
public class UserServiceImpl implements IUserService {
@Override
public String ceshi(String input) {
return "Hello World,"+input;
}
}
注入依赖
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.6</version>
</dependency>
<!-- Nacos Spring dependency -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>0.0.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>0.6.1</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
bootstrap.properties 配置Nacos配置中心
#应用名
spring.application.name=springcloud-alibaba-consumer
#配置文件的前缀
spring.cloud.nacos.config.prefix=springcloud-alibaba-consumer
#当前生效的配置文件
spring.profiles.active=Test
#配置中心路径
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#配置文件后缀
spring.cloud.nacos.config.file-extension=properties
服务降级的实现类
public class MockUserService implements IUserService {
public MockUserService(){
}
@Override
public String ceshi(String input) {
return "服务暂时不可用,请重试";
}
}
Controller层
@RestController
@RefreshScope
public class DubboComsumerController {
@Autowired
IUserService userService;
@Value("${name:未知者}")
private String name;
@RequestMapping("/hello")
public String hello(){
return userService.ceshi(name);
}
}
第一步:Nacos管理平台,查看到服务的注册信息
第二步:访问localhost:8081/hello,进行验证
第三步:配置Nacos分布式配置文件
第四步:再次访问localhost:8081/hello,进行验证
第五步:关闭服务提供者后,再次访问:localhost:8081/hello,进行验证
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。