1、开发环境
Nacos
2.2.3
Maven
3.8.2
SpringCloudAlibaba
2022.0.0.0
SpringBoot
3.2.0
Java
17
SpringCloud
2023.0.0-RC1
2、准备工作 2.2、 启动Nacos Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):
1 sh startup.sh -m standalone
如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:
1 bash startup.sh -m standalone
Windows 启动命令(standalone代表着单机模式运行,非集群模式):
1 startup.cmd -m standalone
3、工程创建 3.1、使用maven创建父工程(删除src文件) pom.xml文件如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 <?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 > org.su</groupId > <artifactId > SpringCloud_Two</artifactId > <version > 1.0-SNAPSHOT</version > <properties > <maven.compiler.source > 17</maven.compiler.source > <maven.compiler.target > 17</maven.compiler.target > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <java.version > 17</java.version > <spring-cloud.version > 2023.0.0-RC1</spring-cloud.version > <spring-cloud-alibaba.version > 2022.0.0.0</spring-cloud-alibaba.version > <spring-boot-version > 3.2.0</spring-boot-version > </properties > <dependencyManagement > <dependencies > <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 > org.springframework.cloud</groupId > <artifactId > spring-cloud-dependencies</artifactId > <version > ${spring-cloud.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 > </dependencies > </dependencyManagement > <modules > <module > spring-provider</module > <module > spring-consumer</module > </modules > <packaging > pom</packaging > <repositories > <repository > <id > spring-milestones</id > <name > Spring Milestones</name > <url > https://repo.spring.io/milestone</url > <snapshots > <enabled > false</enabled > </snapshots > </repository > </repositories > </project >
3.2、创建spring-provider与spring-consumer子工程 3.2.0、spring-provider -> pom.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <parent > <groupId > org.su</groupId > <artifactId > SpringCloud_Two</artifactId > <version > 1.0-SNAPSHOT</version > <relativePath > ../pom.xml</relativePath > </parent > <groupId > com.su</groupId > <artifactId > spring-provider</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > spring-provider</name > <description > spring-provider</description > <properties > <java.version > 17</java.version > </properties > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-bootstrap</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter</artifactId > </dependency > <dependency > <groupId > com.alibaba.cloud</groupId > <artifactId > spring-cloud-starter-alibaba-nacos-config</artifactId > </dependency > <dependency > <groupId > com.alibaba.cloud</groupId > <artifactId > spring-cloud-starter-alibaba-nacos-discovery</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-openfeign</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-loadbalancer</artifactId > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > </plugins > </build > </project >
3.2.1、在spring-provider工程中添加bootstrap.properties文件 1 2 3 4 server.port =8070 spring.application.name =service-provider spring.cloud.nacos.discovery.server-addr =127.0.0.1:8848
3.2.2、修改启动器以提供服务注册功能 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.su.springprovider;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication @EnableDiscoveryClient public class SpringProviderApplication { public static void main (String[] args) { SpringApplication.run(SpringProviderApplication.class, args); } @RestController static class EchoController { @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) public String echo (@PathVariable(name = "string") String string) { System.out.println(string); return "Hello Nacos Discovery " + string; } } }
3.2.3、spring-consumer -> pom.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <parent > <groupId > org.su</groupId > <artifactId > SpringCloud_Two</artifactId > <version > 1.0-SNAPSHOT</version > <relativePath > ../pom.xml</relativePath > </parent > <groupId > com.su</groupId > <artifactId > spring-consumer</artifactId > <version > 0.0.1-SNAPSHOT</version > <name > spring-consumer</name > <description > spring-consumer</description > <properties > <java.version > 17</java.version > </properties > <dependencies > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-test</artifactId > <scope > test</scope > </dependency > <dependency > <groupId > com.alibaba.cloud</groupId > <artifactId > spring-cloud-starter-alibaba-nacos-discovery</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-openfeign</artifactId > </dependency > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-loadbalancer</artifactId > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-maven-plugin</artifactId > </plugin > </plugins > </build > </project >
3.2.4、 在spring-consumer的application.properties添加 1 2 3 spring.application.name =service-consumer server.port =18082 spring.cloud.nacos.discovery.server-addr =127.0.0.1:8848
3.2.5、新建EchoService 1 2 3 4 5 6 7 8 9 10 11 12 package com.su.springconsumer;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@FeignClient(name = "service-provider") public interface EchoService { @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) String echo (@PathVariable(name = "str") String str) ; }
3.2.6、新建TestController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package com.su.springconsumer;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestController public class TestController { private final RestTemplate restTemplate; private final EchoService echoService; public TestController (RestTemplate restTemplate, EchoService echoService) { this .restTemplate = restTemplate; this .echoService = echoService; } @RequestMapping(value = "/echo-rest/{str}", method = RequestMethod.GET) public String rest (@PathVariable(name = "str") String str) { return restTemplate.getForObject("http://service-provider/echo/" + str, String.class); } @RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET) public String feign (@PathVariable(name = "str") String str) { return echoService.echo(str); } }
3.2.7、修改启动器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.su.springconsumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.openfeign.EnableFeignClients;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class SpringConsumerApplication { @LoadBalanced @Bean public RestTemplate restTemplate () { return new RestTemplate(); } public static void main (String[] args) { SpringApplication.run(SpringConsumerApplication.class, args); } }
3.2.8 启动spring-provider与spring-consumer 4、测试 4.1、服务列表 在浏览器打开(http://127.0.0.1:8848/nacos)可以看见 在左侧导航栏中打开服务管理/服务列表可以看见我们的两个服务
4.2、测试服务 浏览器打开
1 2 http://127.0.0.1:18082/echo-rest/rest-rest http://127.0.0.1:18082/echo-feign/feign-rest
可以发现spring-consumer消费了spring-provider提供的服务