1.spring-boot-starter-parent 与 spring-boot-dependencies
spring-boot-starter-parent 与 spring-boot-dependencies - 简书
Spring Boot的每个发布版本都会规划它所支持的依赖项。实际上,你不用指定这些依赖项的版本号,因为Spring Boot都为你管理好了。当更新Spring Boot时,会相应的更新依赖。
spring-cloud-dependencies也是一个依赖管理器的pom文件,与spring-boot-starter-parent的作用一样,不同的是spring-cloud-dependencies是对cloud的依赖管理。如:spring-cloud-starter-config、spring-cloud-starter-netflix-eureka-server
2.nacos
Spring Cloud 应用如何接入 Nacos 注册中心和使用 Nacos 作为配置中心_springcloud 链接nacos-CSDN博客
springboot2.7.18
启动类加注解
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/>
</parent>
<properties>
<app.release.version>1.0-SNAPSHOT</app.release.version>
<java.version>21</java.version>
<!--指定Maven用什么编码来读取源码及文档 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--指定Maven用什么编码来呈现站点的HTML文件 -->
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
<spring.cloud.version>2021.0.5</spring.cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</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>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.3.0</version>
</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>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>
</dependencies>
</dependencyManagement>
启动类加注解在启动类上使用@EnableDiscoveryClient 注解开启服务注册与发现功能
//@EnableDiscoveryClient
//@EnableFeignClients
3.nacos 配置中心的用法
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
bootstrap.yml 的配置和注册的配置信息
spring:
application:
name: xxljob
cloud:
nacos:
server-addr: ${NACOS_SERVER_ADDR:xxx}
username: ${NACOS_USERNAME:nacos}
password: ${NACOS_PASSWORD:xxx}
config: #配置
namespace: ${NACOS_NAMESPACE:xxx}
name: ${NACOS_CONFIG_FILE:service-job}
#name: ${NACOS_CONFIG_FILE:service-local}
file-extension: yaml
discovery: #注册
server-addr: xxx
namespace: xxx
@EnableDiscoveryClient
4.nacos 注册中心
<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-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
</dependency>
<!-- openfeign依赖 1. http客户端选择okhttp 2. loadbalancer替换ribbon -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
springboot启动类添加注解:@EnableDiscoveryClient,实现服务发现
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableDiscoveryClient
public class ContentApplication
{
public static void main(String[] args)
{
SpringApplication.run(ContentApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ 启动成功 ლ(´ `ლ)゙");
}
}
服务提供方提供的服务接口:
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
5.openfeign
服务消费方代码示例
<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-loadbalancer</artifactId>
</dependency>
<!-- 提供了注解的方式调用服务,简化了http请求 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
spring:
cloud:
nacos:
discovery:
##nacos的地址
server-addr: 127.0.0.1:8848
##nacos的命名空间ID
namespace: d5afac56-78a0-48e5-ac76-c6e13c96f35f
application:
##应用名称,全局唯一,一个nacos中应用名不可重复
name: consumer
消费端的服务启动类增加注解:@EnableFeignClients
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableDiscoveryClient
@EnableFeignClients
public class CAApplication
{
public static void main(String[] args)
{
SpringApplication.run(CAApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ 消费端启动成功 ლ(´ `ლ)゙");
}
}
通过feign调用服务端接口
@FeignClient("service_provider")//注解填写服务名称,即可实现服务接口的负载均衡调用
@Component //将该接口声明为spring组件,交由spring管理
public interface IContentClient {
//该地址为服务端提供的接口地址
@GetMapping("/content/echo/{string}")
public String echo(@PathVariable("string")String string);
}
编写一个controller类调用上面组件的接口
@RestController
@RequestMapping("/ca")
public class CAController {
@Autowired
private IContentClient contentClient;
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
public String echo(@PathVariable String str) {
return contentClient.echo(str);
}
}