1.概要
1.1 说明
使用nacos做为注册中
1.2 要点
-
spring-cloud-alibaba-dependencies
-
spring-cloud-starter-alibaba-nacos-discovery
-
spring-boot-starter-web
-
@SpringBootApplication
-
@EnableDiscoveryClient
-
SpringApplication.run(Main.class);
-
@RestController
-
@RequestMapping("/fun")
-
cloud: nacos: discovery: server-addr: localhost:8848
-
@Bean //@LoadBalanced public RestTemplate restTemplate()
-
@Autowired RestTemplate restTemplate;
2 代码
2.1 父工程
<?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.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xjc.springcloundtest</groupId>
<artifactId>demo9</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>demo9</name>
<description>demo9</description>
<modules>
<module>untitled</module>
<module>untitled1</module>
<module>untitled2</module>
<module>untitled3</module>
</modules>
<properties>
<java.version>21</java.version>
<spring-cloud.version>2023.0.0</spring-cloud.version>
<spring-cloud-alibaba.version>2020.0.RC1</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud 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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 服务工程
2.2.1 工程文件
<?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>
<parent>
<groupId>com.xjc.springcloundtest</groupId>
<artifactId>demo9</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>untitled</artifactId>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
2.2.2 配置文件
spring:
application:
name: server1
cloud:
nacos:
discovery:
server-addr: localhost:8848
#eureka:
# client:
# service-url:
# defaultZone: http://127.0.0.1:10086/eureka/
2.2.3 主函数
package com.xjc.springcloundtest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
System.out.println("Hello world!");
}
}
2.2.4 服务接口
package com.xjc.springcloundtest;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
//@EnableDiscoveryClient
public class TestController {
@RequestMapping("/fun")
public String fun(){
return "函数1";
}
}
2.2.5 运行效果
2.3 消费工程
2.3.1 工程文件
<?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>
<parent>
<groupId>com.xjc.springcloundtest</groupId>
<artifactId>demo9</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>untitled1</artifactId>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
2.3.2 配置文件
server:
port: 8081
spring:
application:
name: server2
cloud:
nacos:
discovery:
server-addr: localhost:8848
#eureka:
# client:
# service-url:
# defaultZone: http://127.0.0.1:10086/eureka/
2.3.3 主函数
package com.xjc.springcloundtest;
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.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
//@EnableDiscoveryClient
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
System.out.println("Hello world!");
}
@Bean
//@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
2.3.4 消费函数
package com.xjc.springcloundtest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class TestController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/fun")
public String fun(){
//String url = "http://server1/fun";
String url = "http://localhost:8080/fun";
String ret = restTemplate.getForObject(url,String.class);
return "函数2"+ret;
}
}
2.3.5 运行效果
3 nacos 下载安装