首先说明,本文章内容是搭建一个基于alibaba的微服务框架,只涉及nacos与feign的使用,不会搭建微服务项目的伙伴们可以来看看。
nacos的安装下载请自行百度,网上教程很多。
我们创建一个父项目,这个父项目下有两个子项目,一个用来注册到nacos,另一个利用feign从nacos中获取数据。
创建父项目
通过maven来创建一个父项目,创建完成后将src目录删除
父 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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>cloudtest</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>manage-nacos</module>
<module>manage-nacos-comunser</module>
</modules>
<!-- 打包方式,必须要-->
<packaging>pom</packaging>
<!-- boot依赖,必须要-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.11.RELEASE</version>
<relativePath/>
</parent>
<!-- 定义版本的地方-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.targer>8</maven.compiler.targer>
<spring-boot-version>2.1.4.RELEASE</spring-boot-version>
<mysql-connector-version>5.1.47</mysql-connector-version>
<druid-version>1.1.21</druid-version>
<mybatis-spring-boot-version>2.1.4</mybatis-spring-boot-version>
<lombok-version>1.18.8</lombok-version>
<junit-version>4.12</junit-version>
<log4j-version>1.2.17</log4j-version>
</properties>
<!-- dependencyManagement 是依赖管理,子项目可以从中获取需要的依赖 -->
<dependencyManagement>
<dependencies>
<!-- springcloud alibaba 依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<!-- 数据库依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid-version}</version>
</dependency>
<!-- springboot启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot-version}</version>
</dependency>
<!-- 单元测试 依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok-version}</version>
<scope>provided</scope>
</dependency>
<!-- 日志依赖 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
</dependencies>
</project>
创建第一个子项目,将这个子项目注册到nacos中
也是通过maven来创建
红框里面刚开始是没有的,需要自己创建
现在是他的pom文件
<?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>cloudtest</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.guming</groupId>
<artifactId>manage-nacos</artifactId>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!--项目启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
- application.yml文件内容
server:
port: 8001
spring:
application:
name: manage-nacos
cloud:
nacos:
discovery:
username: nacos
password: nacos
server-addr: 127.0.0.1:8848
namespace: public # 注册到 nacos 的指定 namespace,默认为 public
- 创建启动类
@SpringBootApplication
public class NacosApplication {
public static void main(String[] args) {
SpringApplication.run(NacosApplication.class, args);
}
}
- 在controller中定义一个方法,等会feign需要用到
@RestController
public class TestController {
@PostMapping("/get/name")
public String getName(String name){
return "name:"+name;
}
}
- 现在启动NacosApplication ,不出意外你打开nacos客户端就会发现里面存在了一个服务
创建一个用于消费的项目
- 和前面一个步骤一样,也是用maven来创建
- pom 与依赖
<?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>cloudtest</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.guming</groupId>
<artifactId>manage-nacos-comunser</artifactId>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-cloud-alibaba.version>2.1.2.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- feign 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${spring-cloud-alibaba.version}</version>
</dependency>
</dependencies>
</project>
- 在启动类添加注解
@SpringBootApplication
@EnableFeignClients //开启feign客户端
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
- 创建一个接口来定义需要从哪个服务中获取数据
// 表明这个接口是一个feign客户端,里面的参数表示从哪个服务提供者获取数据
@FeignClient("manage-nacos")
public interface TestFeign {
服务提供者提供的接口,也就是自己可以通过这个接口获取想要的数据
@PostMapping("/get/name")
public String getName(@RequestParam("name") String name);
}
- 创建controller来消费
@RestController
public class ConsumerController {
@Autowired
public TestFeign TestFeign;
@GetMapping("/test")
public String getName(){
String name = TestFeign.getName("我叫王五");
System.out.println(name);
return name;
}
}
feign原理图
- 在上面的接口中,我们定义了去哪个服务中获取数据,也就是相当于定义了对应的ip以及端口,后面我们通过@GetMapping("/test") 又定义具体的请求路径,他会在后台自动发送http请求来获取数据。