文章目录
- SpringCloud Alibaba Nacos
- 创建Nacos 的服务消费者
- 需求说明/图解
- 创建member-service-nacos-consumer-80 并注册到NacosServer8848
- 创建member-service-nacos-consumer-80
- 修改pom.xml
- 创建application.yml
- 创建主启动类
- 业务类
- 测试
SpringCloud Alibaba Nacos
创建Nacos 的服务消费者
需求说明/图解
创建member-service-nacos-consumer-80 并注册到NacosServer8848
创建member-service-nacos-consumer-80
- 参考member-service-consumer-80 来创建member-service-nacos-consumer-80
修改pom.xml
- 将member-service-consumer-80 的pom.xml 的… 拷贝过来进行修改
<?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>e-commerce-center</artifactId>
<groupId>com.my.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>member-service-nacos-consumer-80</artifactId>
<!--引入相关的依赖: 引入当前需要的依赖,后面如果有其它需要,再进行调整-->
<dependencies>
<使用版本仲裁-->
<!--引入sentinel 和 nacos持久化整合依赖-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--引入 sentinel starter 场景启动器starter-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--引入openfeign starter 场景启动器starter-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--引入alibaba-nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--引入web-starter说明我们使用版本仲裁(从父项目继承了版本)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--lombok注解-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--引入e_commerce_center-common-api-->
<dependency>
<groupId>com.my.springcloud</groupId>
<artifactId>e_commerce_center-common-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
创建application.yml
- application.yml 内容如下:
server:
port: 80
spring:
application:
name: member-service-nacos-consumer-80
#配置nacos
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos server的地址
创建主启动类
- 创建主启动类com/my/springcloud/MemberNacosConsumerApplication80.java
@SpringBootApplication
@EnableDiscoveryClient //引入启动 nacos发现注解
public class MemberNacosConsumerApplication80 {
public static void main(String[] args) {
SpringApplication.run(MemberNacosConsumerApplication80.class,args);
}
}
业务类
- 创建配置类com/my/springcloud/config/CustomizationBean.java
@Configuration
public class CustomizationBean {
// 配置注入的RestTemplate bean/对象
//默认使用轮询算法访问远程调用接口/地址
@Bean
@LoadBalanced//赋予 RestTemplate 负载均衡的能力
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
- 创建com/my/springcloud/controller/MemberNacosConsumerController.java
@RestController
@Slf4j
public class MemberNacosConsumerController {
// http://member-service-nacos-provider 是服务注册到Nacos server的服务名,这里是小写
public static final String MEMBER_SERVICE_NACOS_PROVIDER_URL =
"http://member-service-nacos-provider";
//配置RestTemplate
@Resource
private RestTemplate restTemplate;
//添加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);
}
//查询member
@GetMapping("/member/nacos/consumer/get/{id}")
public Result<Member> getMemberById(@PathVariable("id") Long id) {
return restTemplate.getForObject(
MEMBER_SERVICE_NACOS_PROVIDER_URL + "/member/get/" + id, Result.class);
}
}
测试
启动Nacos Server 8848
启动member-service-nacos-provider-10004/10006
启动member-service-nacos-consumer-80
浏览器输入: http://localhost/member/nacos/consumer/get/1 观察是否是轮询调用
配置自己的负载均衡算法, 测试完毕恢复成原来的轮询算法
//RibbonRule: 配置自己的负载均衡算法
@Configuration
public class RibbonRule {
//配置注入自己的负载均衡算法
@Bean
public IRule myRibbonRule() {
//这里 返回的是RandomRule,也可以自己指定返回
return new RandomRule();
}
}