赞
踩
\nacos-spring-cloud-alibaba-demo\pom.xml
子项目要使用Springboot
,但又要继承父项目,所以在父项目中import
相关依赖,代替继承操作。
<?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>com.jerry</groupId> <artifactId>nacos-spring-cloud-alibaba-demo</artifactId> <version>1.0</version> <packaging>pom</packaging> <name>${artifactId}</name> <description>学习 nacos 配置管理</description> <modules> <module>nacos-spring-cloud-alibaba-provider</module> <module>nacos-spring-cloud-alibaba-consumer</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <!--修改 Language level--> <maven.compiler.source>1.8</maven.compiler.source> <!--修改 Java Compiler--> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencyManagement> <dependencies> <!-- spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring-cloud-alibab-dependencies --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- spring-boot-dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.3.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>
\.\nacos-spring-cloud-alibaba-provider\pom.xml
依赖 | 说明 | 添加与否 |
---|---|---|
web服务 | 生产者消费者都要对外提供 restful 接口 | 两个都添加 |
服务注册发现 | 生产者消费者都要注册到nacos | 两个都添加 |
配置管理 | 针对本demo只有生产 者读了配置 | 只有生产者添加 |
<?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-spring-cloud-alibaba-demo</artifactId> <groupId>com.jerry</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>nacos-spring-cloud-alibaba-provider</artifactId> <dependencies> <!--web服务--> <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> <!--配置管理--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> </dependencies> </project>
SpringCloud
配置文件 bootstrap.yml\.\nacos-spring-cloud-alibaba-provider\src\main\resources\bootstrap.yml
server:
port: 8085
spring:
application:
name: nacos-alibaba-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
SpringBoot
配置文件 nacos-alibaba-provider.ymljerry:
name: 这里是 consumer-service-alibaba
com.jerry.nacos.discovery.provider.NacosProviderApplication
package com.jerry.nacos.discovery.provider; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * @author jerryjin */ @SpringBootApplication @EnableDiscoveryClient public class NacosProviderApplication { public static void main(String[] args) { SpringApplication.run(NacosProviderApplication.class, args); } }
com.jerry.nacos.discovery.provider.controller.ProviderController
package com.jerry.nacos.discovery.provider.controller; import com.alibaba.nacos.api.annotation.NacosProperties; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.time.LocalDateTime; /** * @author jerryjin */ @RestController @RefreshScope public class ProviderController { @Value("${jerry.name:666}") private String name; @GetMapping("/hi") public String hi(){ return name + " " + LocalDateTime.now().toString(); } }
\.\nacos-spring-cloud-alibaba-consumer\pom.xml
依赖 | 说明 | 添加与否 |
---|---|---|
web服务 | 生产者消费者都要对外提供 restful 接口 | 两个都添加 |
服务注册发现 | 生产者消费者都要注册到nacos | 两个都添加 |
声明式HTTP客户端 | 针对本demo只有消费者使用了 feign 调用别人 | 只有消费者添加 |
<?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-spring-cloud-alibaba-demo</artifactId> <groupId>com.jerry</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>nacos-spring-cloud-alibaba-consumer</artifactId> <dependencies> <!--web服务--> <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> <!--声明式HTTP客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> </project>
\.\nacos-spring-cloud-alibaba-consumer\src\main\resources\application.yml
server:
port: 8086
spring:
application:
name: nacos-alibaba-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
logging:
level:
root: info
org.springframework: info
com.jerry.nacos.discovery.consumer.NacosConsumerApplication
package com.jerry.nacos.discovery.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.openfeign.EnableFeignClients; /** * @author jerryjin */ @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class NacosConsumerApplication { public static void main(String[] args) { SpringApplication.run(NacosConsumerApplication.class, args); } }
消费者通过Feign
调用生产者的接口:
interface
。@FeignClient
注解,值标识生产者
的服务名
。例如:nacos-alibaba-provider
interface
中定义胡方法与生产者的controller
对应。com.jerry.nacos.discovery.consumer.client.ProviderClient
nacos-alibaba-provider
package com.jerry.nacos.discovery.consumer.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author jerryjin
*/
@FeignClient("nacos-alibaba-provider")
public interface ProviderClient {
@GetMapping("/hi")
String hi();
}
com.jerry.nacos.discovery.consumer.controller.ConsumerController
FeignClient
直接调用即可。package com.jerry.nacos.discovery.consumer.controller; import com.jerry.nacos.discovery.consumer.client.ProviderClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author jerryjin */ @RestController public class ConsumerController { @Resource ProviderClient providerClient; @GetMapping("/hi") public String hi(){ String hi = providerClient.hi(); return "消费者调用了生产者,生产者说:" + hi; } }
Nacos Spring Cloud 快速开始
官网:配置项详解:Spring Cloud Alibaba Nacos Config
官网:Wiki 配置项详解:Spring Cloud Alibaba Nacos Config
笑虾CSDN Nacos 学习笔记:SpringBoot 整合 Nacos 配置管理、服务发现
笑虾码云 spring-cloud-alibaba-demo 源码
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。