Spring Cloud Alibaba 添加 nacos 注册服务
一、复制module
order 复制为 order-nacos
修改pom.xml
<artifactId>order-nacos</artifactId>
修改application.yml
server:
port: 8020
stock 复制为 stock-nacos
修改pom.xml
<artifactId>stock-nacos</artifactId>
修改application.yml
server:
port: 8021
修改父类 pom.xml
<modules> <module>order</module> <module>stock</module> <module>order-nacos</module> <module>stock-nacos</module> </modules>
如果复制后的module无法编译,解法方法:
将无法编译的pom.xml去掉勾就行了
order-nacos、stock-nacos中的pom.xml添加
<!-- nacos 服务注册发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
<?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>springcloudalibaba</artifactId> <groupId>com.wsm.springcloud</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>order-nacos</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <version>2.5.5</version>--> </dependency> <!-- nacos 服务注册发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project>
<?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>springcloudalibaba</artifactId> <groupId>com.wsm.springcloud</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>stock-nacos</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <version>2.5.5</version>--> </dependency> <!-- nacos 服务注册发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project>
application.yml 添加 nacos配置
server:
port: 8020
#应用名称 (nacos 会将该名称当作服务名称)
spring:
application:
name: order-service
cloud:
nacos:
server-addr: 127.0.0.1:8848
discovery:
username: nacos
password: nacos
namespace: public
server:
port: 8021
#应用名称 (nacos 会将该名称当作服务名称)
spring:
application:
name: stock-service
cloud:
nacos:
server-addr: 127.0.0.1:8848
discovery:
username: nacos
password: nacos
namespace: public
package com.wsm.order; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication //@EnableDiscoveryClient 老版本要加,新版本可以不加 public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class,args); } @Bean public RestTemplate restTemplate(RestTemplateBuilder builder){ RestTemplate restTemplate = builder.build(); return restTemplate; } }
package com.wsm.order.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.swing.*; @RestController @RequestMapping("/order") public class OrderController { @Autowired RestTemplate restTemplate; @RequestMapping("/add") public String add(){ System.out.println("aaaaaaaaaaaaa"); // String msg = restTemplate.getForObject("http://localhost:8011/stock/reduct", String.class); String msg = restTemplate.getForObject("http://stock-service/stock/reduct", String.class); return "hello world "+msg; } }
package com.wsm.order; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication //@EnableDiscoveryClient 老版本要加,新版本可以不加 public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class,args); } @Bean @LoadBalanced //启用负载均衡 public RestTemplate restTemplate(RestTemplateBuilder builder){ RestTemplate restTemplate = builder.build(); return restTemplate; } }
package com.wsm.stock.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/stock") public class StockController { @RequestMapping("/reduct") public String reduct(){ System.out.println("扣减库存"); return "bbbbbccc"; } }
负载均衡启动多个sotck-nacos
package com.wsm.stock.controller;
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/stock") public class StockController { @Value("${server.port}") String port; @RequestMapping("/reduct") public String reduct(){ System.out.println("扣减库存"); return "扣减库存:"+port; } }
package com.wsm.order; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication //@EnableDiscoveryClient 老版本要加,新版本可以不加 public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class,args); } @Bean @LoadBalanced //启用负载均衡 public RestTemplate restTemplate(RestTemplateBuilder builder){ RestTemplate restTemplate = builder.build(); return restTemplate; } }
application.yml
server: port: 8021 #应用名称 (nacos 会将该名称当作服务名称) spring: application: name: stock-service cloud: nacos: # server-addr: 127.0.0.1:8848 server-addr: 192.168.133.128:8847 #集群 nginx 负载均衡访问 nacos discovery: username: nacos password: nacos namespace: public # cluster-name: wsm111 # 集群名称 默认 DEFAULT # ephemeral: true #是否临时实例 true:临时实例 false:永久实例 # weight: 1 #权重 # metadata: #元数据 # group: DEFAULT_GROUP #默认DEFAULT_GROUP 用于更细的相同特征的服务进行分组管理 # service: stock-service #默认取${spring.application.name}