一、下载安装nacos
略。
二、搭建服务提供者
1、新建一个工程(test-springcloud),导入Spring Cloud Alibaba依赖
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.sdkj</groupId> <artifactId>cloud-alibaba-parent</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>cloud-provider-alibaba-payment9001</module> <module>cloud-consumer-alibaba-order7001</module> <module>cloud-provider-alibaba-payment9002</module> <module>cloud-config-nacos-client8901</module> </modules> <packaging>pom</packaging> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <!--boot父版本依赖--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.5.RELEASE</version> </parent> <!--cloud管理依赖--> <dependencyManagement> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--alibaba--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- spring boot依赖 --> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
2、新建一个模块服务提供者(springcloud-provider-alibaba-payment9001),引入Nacos依赖
<dependencies> <!--nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies>
3、新建一个配置文件appliction.yml
# 端口
server:
port: 9001
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: 192.168.186.128:8848
4、新建启动类
1 @EnableDiscoveryClient 2 @SpringBootApplication 3 public class PaymentMain9001 { 4 public static void main(String[] args) { 5 SpringApplication.run(PaymentMain9001.class, args); 6 } 7 }
5、新建一个Controller,如下:
1 @RestController 2 public class PaymentController { 3 4 @Value("${server.port}") 5 private String serverPort; 6 7 @GetMapping(value = "/payment/nacos/{id}") 8 public String getPayment(@PathVariable("id") Integer id) { 9 return "nacos registry, serverPort: " + serverPort + "\t id: " + id; 10 } 11 }
6、测试
1)启动Nacos服务,启动服务提供者模块
2)访问地址:http://localhost:9001/payment/nacos/11,正常获取内容
3)访问Nacos的Web界面,查看服务列表菜单
可以看到服务列表中,有nacos-payment-provider服务
三、搭建服务消费者
1、在父工程(test-springcloud)中,新建服务消费者模块(springcloud-consumer-alibaba-order7001)
2、引入nacos依赖,同上
<dependencies> <!--nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies>
3、编写配置文件,如下:
# 端口
server:
port: 7001
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: 192.168.186.128:8848
4、编写启动类,如下:
1 @EnableDiscoveryClient 2 @SpringBootApplication 3 public class OrderMain7001 { 4 public static void main(String[] args) { 5 SpringApplication.run(OrderMain7001.class, args); 6 } 7 }
5、编辑配置类,代码如下,注入RestTemplate
1 @Configuration 2 public class AppConfig { 3 4 @Bean 5 @LoadBalanced 6 public RestTemplate restTemplate(){ 7 return new RestTemplate(); 8 } 9 10 }
6、编写一个Controller用于访问
@RestController public class OrderController { @Autowired private RestTemplate restTemplate; @GetMapping(value = "/consumer/payment/nacos/{id}") public String paymentInfo(@PathVariable("id") Integer id) { return restTemplate.getForObject("http://nacos-payment-provider" + "/payment/nacos/" + id, String.class); } }
7、测试
1)启动Nacos服务,启动服务提供者模块,启动服务消费者模块
2)访问地址:http://localhost:9001/payment/nacos/11,正常获取内容
访问地址:http://localhost:7001/consumer/payment/nacos/11,正常获取内容
3)访问Nacos的Web界面,查看服务列表菜单,分别可以看到一个nacos-payment-provider服务,一个nacos-order-consumer服务
4)在搭建一个(springcloud-provider-alibaba-payment9002)服务提供者模块,与9001模块内容相同,只有端口的区别
5)启动9002模块,使用地址:http://localhost:7001/consumer/payment/nacos/11,访问获取内容
获取的内容,是轮流从9001与9002这两个节点上获取
6)访问Nacos的Web界面,查看服务列表菜单
本文来自博客园,作者:荣慕平,转载请注明原文链接:https://www.cnblogs.com/rongmuping/articles/16320149.html