环境
Nacos 依赖 java环境来运行。如果您是从代码开始构建并运行Nacos,还需要为此配置 maven环境,请确保是在以下版本环境中安装用:
###2、下载nacos server 服务
您可以从 最新稳定版本 下载 nacos-server-$version.zip
包。
官方网址:Releases · alibaba/nacos (github.com)
下面用的是 nacos-server-1.4.3.zip
为了方便我们更好的管理包和包的版本管理,我创建一个父工程,在父工程中创建若干个子工程
创建好后我们把src目录删除,不删也行
<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.0modelVersion>
<groupId>com.qiumingroupId>
<artifactId>springcloudartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<modules>
<module>provide-servicemodule>
<module>comsumermodule>
<module>offer-servicemodule>
modules>
<properties>
<maven.compiler.source>15maven.compiler.source>
<maven.compiler.target>15maven.compiler.target>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2021.0.1.0version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>2021.0.1version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>2.6.5version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>2.6.5version>
plugin>
plugins>
build>
project>
特别注意包:有些包一定是com.alibaba.cloud下的包
该模块用于服务的提供者即生产者,这个模块可以是原生的maven模块,也可直接是springboot模块
<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>springcloudartifactId>
<groupId>com.qiumingroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>offer-serviceartifactId>
<properties>
<maven.compiler.source>15maven.compiler.source>
<maven.compiler.target>15maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-nacos-discoveryartifactId>
<version>2.2.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<version>3.1.1version>
dependency>
dependencies>
project>
server:
port: 8886 #启动端口
spring:
application:
name: offer-service #服务名
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos注册中心地址
package com.qiumin.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OfferController {
//暴露http请求
@GetMapping("/offer")
public String offerService(){
System.out.println("offer service invoke");
return "offer service invoke";
}
}
特别注意:这里如果返回的是字符串那一定要写@RestController 如果不小心写成了@Controller,后面访问时后报404错误
如果前面该子模块创建的是maven项目,这里就手动创建主启动类,如果就是一个springboot项目就自带了,我们只需加上部分注解即可。
package com.qiumin;
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
//开启FeignClients远程调用的客户端
@EnableFeignClients
public class NacosOfferApplication {
public static void main(String[] args) {
SpringApplication.run(NacosOfferApplication.class, args);
}
}
注意这里启动之前我们需要提前启动nacos服务端
该模块用于服务的提供者即消费者,这个模块可以是原生的maven模块,也可直接是springboot模块
<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>springcloudartifactId>
<groupId>com.qiumingroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>comsumerartifactId>
<properties>
<maven.compiler.source>15maven.compiler.source>
<maven.compiler.target>15maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-nacos-discoveryartifactId>
<version>2.2.0.RELEASEversion>
<exclusions>
<exclusion>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-ribbonartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-loadbalancerartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
<version>3.1.1version>
dependency>
dependencies>
project>
server:
port: 8887 #启动端口
spring:
application:
name: comsumer-service #服务名
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #注册中心地址,要去那服务
package com.qiumin.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
//value是远程服务的服务名
@FeignClient(value = "offer-service")
public interface ProvideClient {
//该方法是生产者中注册的方法要一模一样,我们把它放在该接口中
@GetMapping("/offer")
String offerService();
}
package com.qiumin.controller;
import com.qiumin.client.ProvideClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ComsumerController {
@Autowired
//生产者的代理对象
ProvideClient provideClient;
@GetMapping("/offer")
public String offerService(){
System.out.println("comsumer service invoke");
//执行在注册中心注册的远程服务调用
String offerResult = provideClient.offerService();
return "comsumer service invoke"+"======"+offerResult;
}
}
如果前面该子模块创建的是maven项目,这里就手动创建主启动类,如果就是一个springboot项目就自带了,我们只需加上部分注解即可。
package com.qiumin;
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
//开启FeignClients远程调用的客户端
@EnableFeignClients
public class NacosComsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosComsumerApplication.class,args);
}
}
注意这里启动之前我们也需要提前启动nacos服务端
**访问:**http://localhost:8887/offer
导包改pom.xml
----->写配置文件yaml
------>controller
------>加注解
------>启动
------>调试
qiumin