一句话: Nacos 就是注册中心[替代 Eureka]+配置中心[替代 Config]
下载: https://github.com/alibaba/nacos/releases/tag/1.2.1
在命令窗口中启动后如下
浏览器 http://localhost:8848/nacos
账号密码为:nacos
因为nacos是alibaba的组件,所有需要在父项目中导入spring-cloud-alibaba依赖
第一步、在父项目中导入alibaba依赖
<dependencyManagement>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependencyManagement>
第二步、子模块中引入alibaba-nacos 依赖
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
第三步、配置我们server-provider-nacos的 application.yml文件
server:
port: 10004
spring:
application:
name: member-service-nacos-provider #名称,可以自己指定
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3308/e_commerce_center_db?useUnicode=true&characterEncoding=utf-8&useSSL=false
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.xjz.springcloud.entity # 实体类的包路径
第四步、创建启动类
@EnableDiscoveryClient //开启 Nacos 发现注解
@SpringBootApplication
public class MemberNacosProviderApplication10004 {
public static void main(String[] args) {
SpringApplication.run(MemberNacosProviderApplication10004.class,args);
}
}
第五步、测试
观察 nacos 服务是否注册成功
第一步、在父项目中导入alibaba依赖
这里因为之前已经在父项目中引入过alibaba依赖,所以可以跳过
<dependencyManagement>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.1.0.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependencyManagement>
第二步、子模块中引入alibaba-nacos 依赖
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
第三步、配置我们server-provider-nacos的 application.yml文件
server:
port: 80
spring:
application:
name: member-service-nacos-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848 # Nacos Server的地址
第四步、创建启动类
@SpringBootApplication
@EnableDiscoveryClient //开启 nacos 的发现注解
public class MemberNacosConsumerApplication80 {
public static void main(String[] args) {
SpringApplication.run(MemberNacosConsumerApplication80.class,args);
}
}
第五步、创建配置类
package com.xjz.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;
// nacos 本身就集成了 Ribbon,直接支持 Ribbon + RestTemplate 调用
@Configuration
public class CustomizationBean {
//这里的@LoadBalanced 就是赋予 RestTemplate 负载均衡的能力
//默认是使用轮询算法来访问远程调用接口/地址
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
第六步、配置控制类
这里我们可以通过 OpenFeign 调用接口更方便,只不过需要在业务层创建一个接口,下一节就有啦~
package com.xjz.springcloud.controller;
import com.xjz.springcloud.entity.Member;
import com.xjz.springcloud.entity.Result;
import com.xjz.springcloud.service.MemberOpenFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@Slf4j
public class MemberNacosConsumerController {
/*
http://member-service-nacos-provider Nacos要求这里的服务名要小写,
和服务 Nacos Server 中注册名保持一致
*/
public static final String MEMBER_SERVICE_NACOS_PROVIDER_URL =
"http://member-service-nacos-provider";
@Resource
private RestTemplate restTemplate;
@Resource
private MemberOpenFeignService memberOpenFeignService;
//方法1/接口,添加member
@PostMapping("/member/nacos/consumer/save")
public Result<Member> save(Member member) {
return restTemplate.postForObject
(MEMBER_SERVICE_NACOS_PROVIDER_URL + "/member/save", member, Result.class);
}
//方法2/接口,查询member
@GetMapping("/member/nacos/consumer/get/{id}")
public Result<Member> queryMemberById(@PathVariable("id") Long id) {
return restTemplate.getForObject
(MEMBER_SERVICE_NACOS_PROVIDER_URL + "/member/get/" + id, Result.class);
}
}
第七步、测试
观察 nacos 服务是否注册成功
默认是轮询算法,也可以配置自己的负载均衡算法,配置如下
package com.xjz.springcloud.config;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// RibbonRule:配置自己的负载均衡算法
@Configuration
public class RibbonRule {
//配置注入自己的负载均衡算法
@Bean
public IRule myRibbonRule(){
//这里我们返回的是 RandomRule,随机算法
return new RandomRule();
}
}