一:SpringCloud概念
这里用一个电商平台举例
流程:
1.SpringCloud
SpringCloud核心组件:Eureka
SpringCloud核心组件:Fegin
SpringCloud核心组件:Ribbon(负载均衡)
SpringCloud核心组件:Hystrix
SpringCloud核心组件:Zuul
二:Nacos的搭建
1.解压Nacos-server的压缩包
2.双击nacos包下bin目录下的startup.cmd文件
3.然后cmd窗口最上方有网址进去就可以了
三:消费者和生产者的编写
1.新建一个Maven工程,用来当作生产者消费者的父类
注:父类用来装共同的pom文件
2.在父类的pom文件中导入pom依赖
放在properties标签下<spring-boot.version>2.4.1</spring-boot.version> <spring-cloud.version>2020.0.0</spring-cloud.version> <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
放在dependencyManagement标签下<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency>
注:其他的比如build标签里的所有内容就可以删除了
3.新建消费者和生产者(新建SpringBoot项目)
4.在消费者和生产者的pom文件下面继承父类
<parent> <artifactId>cloud_02</artifactId> <groupId>org.sg</groupId> <version>1.0-SNAPSHOT</version> </parent>
同样的父类也要承认该子类
<modules> <module>provider</module> <module>consumer</module> </modules>
5.在生产者和消费者中新建yml文件
注:访问地址不同不要乱 Ctrl+c
spring: application: name: nacos-consumer cloud: nacos: discovery: server-addr: 127.0.0.1:8848 server: port: 8081
6.在生产者和消费者中建一个controller包
生产者:
package com.provider.code.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ProviderController { @RequestMapping("/run") public String run(){ return "来辽"; } }
消费者:
package com.consumer.code.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; @RestController public class ConsumerController { private final RestTemplate template; @Autowired public ConsumerController(RestTemplate template){ this.template=template; } @RequestMapping("/run") public String run(){ return template.getForObject("http://nacos-provider/run",String.class); } }
7.编辑消费者的启动类
package com.consumer.code; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; 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 ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
8.调用就可以了