多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] # **1. 创建聚合工程 springcloud-parent-test** > 创建父工程用于版本管理 ~~~ <?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.tuna</groupId> <artifactId>springcloud-parent-test</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>provider</module> </modules> <properties> <spring-boot.version>2.3.4.RELEASE</spring-boot.version> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> <spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version> </properties> <!-- 依赖声明 --> <dependencyManagement> <dependencies> <!-- SpringCloud 微服务 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- SpringCloud Alibaba 微服务 --> <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> <!-- SpringBoot 依赖配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> ~~~ # **2. 服务提供者整合服务发现** * springboot也可以直接结合nacos ## **2.1 创建工程provider** ![](https://img.kancloud.cn/d7/d2/d7d2d2aa049c6c93740dea6d6d1a0ce3_379x254.png) ## **2.2 pom** > 1. 注意`spring-cloud-context`依赖,引入bootstrap.xml才会生效 > 2. 引入服务发现依赖 ~~~ <?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>springcloud-parent-test</artifactId> <groupId>com.tuna</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>provider</artifactId> <dependencies> <!--springboot web启动器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--需要引入该jar才能使bootstrap配置文件生效--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </dependency> <!--服务发现--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project> ~~~ ## 2.3 启动类 ~~~ @SpringBootApplication public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } } ~~~ ## 2.4 controller ~~~ @RestController @RequestMapping("/test") public class TestController { @GetMapping("") public Object say(){ return "hello cloud - provider"; } } ~~~ ## 2.5 bootstrap.yml ~~~ ## Spring spring: application: # 应用名称 name: provider profiles: # 环境配置 active: dev ~~~ ## 2.6 application-dev.yml ~~~ server: port: 8000 spring: application: name: provider cloud: nacos: discovery: # 服务注册地址 server-addr: 192.168.56.10:8848 ~~~ * 测试 ![](https://img.kancloud.cn/af/cc/afcc1c70cd2c40d30ec5a891009b7f9a_554x149.png) * provider注册到nacos ![](https://img.kancloud.cn/54/78/547845d188d56f845c9e5dfdb7f9aae1_923x277.png) # 3. 消费者整合服务发现 ## 3.1 整合Feign cloud-api模块,统一fein接口调用,ribbon轮询和hettrix ![](https://img.kancloud.cn/64/2f/642f48839571c19e8052e1316f71521e_368x186.png) **1. pom** ~~~ <?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>springcloud-parent-test</artifactId> <groupId>com.tuna</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-api</artifactId> <dependencies> <!--feign 依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> </project> ~~~ **2. 定义接口** ~~~ @FeignClient(contextId = "provider-test",value = "provider") //指定调用的微服务名称 public interface ProviderClientService { @RequestMapping(value = "/test", method = RequestMethod.GET) String say(); } ~~~ ## 3.2 Consumer模块 ![](https://img.kancloud.cn/8f/4c/8f4ca8051922c49c18b9af7203fbb552_392x237.png) **1. pom需要引入api** ~~~ <dependency> <groupId>com.tuna</groupId> <artifactId>cloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> ~~~ **2. 配置文件和provider一样** **3. controller** 引入foreign ``` @Autowired ProviderClientService providerClientService; ``` ~~~ @RestController @RequestMapping("/consumer") public class ConsumerController { @Autowired ProviderClientService providerClientService; @GetMapping("") public Object say(){ return "consumer::" + providerClientService.say(); } } ~~~ **4. 启动类开启服务发现和foreign** ~~~ @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients(basePackages = "com.tuna.cloud.api") public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } } ~~~ 测试:Consumer成功调用provider ![](https://img.kancloud.cn/d7/92/d7929b23fdb7acd99c7582649f9440a2_535x129.png) # 动态修改名称空间 ![](https://img.kancloud.cn/21/ca/21ca88c74aa72e6a67c15e213033edc1_1185x318.png) 添加到dev名称空间,修改配置保存 ![](https://img.kancloud.cn/1f/98/1f98c6eea792f739b50dbda251ad6d31_895x417.png) 自动更新 ![](https://img.kancloud.cn/b9/55/b955641a2d8252946a62ed245e04c0b7_867x319.png)