1.概要
1.1 目标
就是做一个简单服务发现
1.2 要点
1.2.1 引入spring cloud 和spring alibaba的坐标
<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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2022.0.0.0-RC2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.2.2 导入web 和服务发现的坐标,配置管理的可不导入,目前不关心
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- nacos配置中心来做配置管理 begin--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> </dependencies>
1.2.3 配置服务名称,没服务名称无法管理服务
spring:
application:
name: server3
cloud:
nacos:
config:
import-check:
enabled: false
1.2.4 启动spring boot
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
System.out.println("Hello world!");
}
}
1.3 技术关键词
- spring-cloud-dependencies 依赖
- spring-cloud-alibaba-dependencies 依赖
- spring-boot-starter-web 依赖
- spring-cloud-starter-alibaba-nacos-discovery 服务注册和发现
- @SpringBootApplication 启动扫描配置
- SpringApplication.run(Main.class); 启动
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>demo21</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>demo21</name>
<description>demo21</description>
<modules>
<module>untitled</module>
</modules>
<properties>
<java.version>21</java.version>
<spring-cloud.version>2023.0.0</spring-cloud.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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2022.0.0.0-RC2</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>demo21</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>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos配置中心来做配置管理 begin-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
</project>
2.2.2 配置文件
spring:
application:
name: server3
cloud:
nacos:
config:
import-check:
enabled: false
2.2.3 主函数
package com.xjc.springcloundtest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class);
System.out.println("Hello world!");
}
}
2.2.3 服务函数
2.2.3.1 服务函数
package com.xjc.springcloundtest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("/fun")
public String fun(){
return "函数1";
}
}
2.2.3.2 消费函数
package com.xjc.springcloundtest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Random;
@RestController
public class TestController2 {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/fun2")
public String fun2(){
String url = "http://server3/fun";
String ret = restTemplate.getForObject(url,String.class);
return "函数2"+ret;
}
}
实验结果
3.运行结果
3.1 服务发现
3.2 运行结果
Microsoft Windows [版本 10.0.19045.3324]
(c) Microsoft Corporation。保留所有权利。
D:\project\nacos3\nacos\bin>startup.cmd -m standalone
"nacos is starting with standalone"
,--.
,--.'|
,--,: : | Nacos 2.3.0
,`--.'`| ' : ,---. Running in stand alone mode, All function modules
| : : | | ' ,'\ .--.--. Port: 8848
: | \ | : ,--.--. ,---. / / | / / ' Pid: 41276
| : ' '; | / \ / \. ; ,. :| : /`./ Console: http://192.168.0.107:8848/nacos/index.html
' ' ;. ;.--. .-. | / / '' | |: :| : ;_
| | | \ | \__\/: . .. ' / ' | .; : \ \ `. https://nacos.io
' : | ; .' ," .--.; |' ; :__| : | `----. \
| | '`--' / / ,. |' | '.'|\ \ / / /`--' /
' : | ; : .' \ : : `----' '--'. /
; |.' | , .-./\ \ / `--'---'
'---' `--`---' `----'
2024-02-02 09:16:37,935 INFO Tomcat initialized with port(s): 8848 (http)
2024-02-02 09:16:38,362 INFO Root WebApplicationContext: initialization completed in 3839 ms
2024-02-02 09:16:44,369 INFO Adding welcome page: class path resource [static/index.html]
2024-02-02 09:16:44,965 WARN You are asking Spring Security to ignore Ant [pattern='/**']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.
2024-02-02 09:16:44,967 INFO Will not secure Ant [pattern='/**']
2024-02-02 09:16:45,000 INFO Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@4cc547a, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@7555b920, org.springframework.security.web.context.SecurityContextPersistenceFilter@67ef029, org.springframework.security.web.header.HeaderWriterFilter@9f46d94, org.springframework.security.web.csrf.CsrfFilter@5db99216, org.springframework.security.web.authentication.logout.LogoutFilter@578524c3, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@6e57e95e, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@56db847e, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4152d38d, org.springframework.security.web.session.SessionManagementFilter@2e77b8cf, org.springframework.security.web.access.ExceptionTranslationFilter@5c1bd44c]
2024-02-02 09:16:45,047 INFO Exposing 1 endpoint(s) beneath base path '/actuator'
2024-02-02 09:16:45,105 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'
2024-02-02 09:16:45,148 INFO Nacos started successfully in stand alone mode. use embedded storage
2024-02-02 09:17:10,898 INFO Initializing Servlet 'dispatcherServlet'
2024-02-02 09:17:10,900 INFO Completed initialization in 1 ms