Spring Cloud Alibaba Nacos:服务发现
程序员文章站
2022-07-10 17:37:16
1、新建SpringBoot父工程 nacos-discoverypom.xml
1、新建SpringBoot父工程 nacos-discovery
pom.xml
<?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>
<modules>
<module>nacos-provider</module>
<module>nacos-consumer</module>
</modules>
<groupId>com.apple.nacos</groupId>
<artifactId>nacos-discovery</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-discovery</name>
<packaging>pom</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.2.RELEASE</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、新建子模块 nacos-provider
pom.xml
<?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>nacos-discovery</artifactId>
<groupId>com.apple.nacos</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-provider</artifactId>
<dependencies>
<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>
</dependencies>
</project>
application.yml
server:
port: 56001
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
# 默认注册到public命名空间 DEFAULT_GROUP分组下,也可指定命名空间与分组
# namespace: dev
# group: DEFAULT_GROUP
# 默认集群名称
# cluster-name: DEFAULT
providerController.java
package com.apple.nacos.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@GetMapping("/service")
public String service(){
return "provider service";
}
}
NacosProviderApplication.java
package com.apple.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
}
3、新建子模块 nacos-consumer
pom.xml
<?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>nacos-discovery</artifactId>
<groupId>com.apple.nacos</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-consumer</artifactId>
<dependencies>
<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>
</dependencies>
</project>
application.yml
server:
port: 56002
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
通过feign来调用provider提供的服务
ProviderClient.java
package com.apple.nacos.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "nacos-provider")
public interface ProviderClient {
@GetMapping("/service")
String service();
}
ConsumerController.java
package com.apple.nacos.controller;
import com.apple.nacos.client.ProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Autowired
private ProviderClient providerClient;
@GetMapping("/service")
public String service(){
String providerResult = providerClient.service();
return "consumer service: " + providerResult;
}
}
NacosConsumerApplication.java
package com.apple.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
}
4、验证
启动nacos-provider、nacos-consumer
查看nacos server管理页面
点详情可以查看该微服务的ip,端口,元数据等详细信息,如下图
调用nacos-consumer暴露的服务:http://localhost:56002/service
返回:consumer service: provider service
5、工程截图
本文地址:https://blog.csdn.net/CSDN_LAFF/article/details/109824809
推荐阅读
-
Spring Cloud开发人员如何解决服务冲突和实例乱窜?(IP实现方案)
-
Spring Cloud Gateway的动态路由怎样做?集成Nacos实现很简单
-
详解Spring Cloud Gateway基于服务发现的默认路由规则
-
SpringCloud之服务注册与发现Spring Cloud Eureka实例代码
-
基于Nacos实现Spring Cloud Gateway实现动态路由的方法
-
Spring Cloud Alibaba | Sentinel: 服务限流高级篇
-
Spring Cloud第七篇 | 声明式服务调用Feign
-
Spring Cloud Alibaba | 序言
-
Spring Cloud Alibaba | Nacos集群部署
-
[Spring cloud 一步步实现广告系统] 11. 使用Feign实现微服务调用