前言
本篇博文是关于SpringCloud Alibaba Nacos的基本介绍和使用,希望你能够喜欢
个人主页:晨犀主页
个人简介:大家好,我是晨犀,希望我的文章可以帮助到大家,您的满意是我的动力
欢迎大家:这里是CSDN,我总结知识的地方,欢迎来到我的博客,感谢大家的观看
如果文章有什么需要改进的地方还请大佬不吝赐教 先在此感谢啦
官网地址: https://github.com/alibaba/Nacos
说明: Nacos 就是注册中心[替代Eureka]+配置中心[替代Config]。
Nacos:Dynamic Naming and Configuration Service。
Nacos:架构理论基础: CAP 理论(支持AP 和CP, 可以切换)
下载: https://github.com/alibaba/nacos/releases/tag/1.2.1
环境要求: Java8/Maven 3.2.x+
解压,运行bin/startup.cmd
浏览器输入http://localhost:8848/nacos
登录用户名/密码为nacos
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<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>e-commerce-centerartifactId>
<groupId>com.my.springcloudgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>member-service-nacos-provider-10004artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-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.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.17version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>com.my.springcloudgroupId>
<artifactId>e_commerce_center-common-apiartifactId>
<version>${project.version}version>
dependency>
dependencies>
project>
server:
port: 10004
spring:
application:
name: member-service-nacos-provider #配置应用的名称
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/e_commerce_center_db?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
#配置nacos
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置Nacos Server的地址
#配置暴露所有的监控点
management:
endpoints:
web:
exposure:
include: '*'
#配置mybatis
mybatis:
mapper-locations: classpath:mapper/*.xml #指定mapper.xml文件位置
type-aliases-package: com.my.springcloud.entity # 实例类所在的包,这样通过类名就可以引用
//@EnableDiscoveryClient 引入的是Nacos 发现注解
@EnableDiscoveryClient
@SpringBootApplication
public class MemberNacosProviderApplication10004 {
public static void main(String[] args) {
SpringApplication.run(MemberNacosProviderApplication10004.class, args);
}
}
@RestController
@Slf4j
public class MemberController {
//装配MemberService
@Resource
private MemberService memberService;
//添加方法/接口
//这里请小伙伴回顾, 应该如何提交
//说明
//1. 我们的前端如果是以json格式来发送添加信息Member, 那么我们需要使用@RequestBody
// , 才能将数据封装到对应的bean, 同时保证http的请求头的 content-type是对应
//2. 如果前端是以表单形式提交了,则不需要使用@RequestBody, 才会进行对象参数封装, 同时保证
// http的请求头的 content-type是对应
@PostMapping("/member/save")
public Result save(@RequestBody Member member) {
log.info("service-provider member={}", member);
int affected = memberService.save(member);
if (affected > 0) { //说明添加成功
return Result.success("添加会员成功 member-service-nacos-provider-10004", affected);
} else {
return Result.error("401", "添加会员失败");
}
}
//查询的方法/接口
//师这里使用url占位符+@PathVariable
@GetMapping("/member/get/{id}")
//public Result getMemberById(@PathVariable("id") Long id, HttpServletRequest request) {
public Result getMemberById(@PathVariable("id") Long id) {
Member member = memberService.queryMemberById(id);
//使用Result把查询到的结果返回
if (member != null) {
//return Result.success("查询会员成功 member-service-provider-10000 " + color + "-" + address, member);
return Result.success("查询会员成功 member-service-nacos-provider-10004", member);
} else {
return Result.error("402", "ID= " + id + "不存在");
}
}
}
启动Nacos Server 8848
启动member-service-nacos-provider-10004
文章到这里就结束了,如果有什么疑问的地方请指出,诸大佬们一起来评论区一起讨论
希望能和诸大佬们一起努力,今后我们一起观看感谢您的阅读
如果帮助到您不妨3连支持一下,创造不易您们的支持是我的动力