使用spring cloud alibaba-使用Feign
程序员文章站
2022-07-15 09:00:07
...
继续上一个项目,在创建一个与nacos2一样的服务,只修改一下端口号一样,这个操作与springcloud类似,创建多个实例组集群。
然后在创建一个server-one,用来消费。
还是创建springboot,然后引入必要依赖
相比于之前的多一个feign
因为feign属于springcloud,所以需要在pom中设置springcloud 的版本库
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.cloudalibaba</groupId>
<artifactId>server-one</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server-one</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
我使用的是<spring-cloud.version>Hoxton.SR6</spring-cloud.version>,版本与springboot的对应关系可以百度一下。
然后feign的依赖就会下载对应的版本。
因为spring-cloud-alibaba.version中没有feign,所以不设置会报错,由于后来会用到springcloud中别的东西,所以就设置统一版本号。
设置yml,与之前的没什么差异
server:
port: 8001
spring:
application:
name: server-one
cloud:
nacos:
discovery:
server-addr: localhost:8848
#命名空间
#namespace: b7d26021-28b5-42d1-b36d-737a818a2146
启动类加上启动注解,然后启动
明个继续写。。。。。。,下班
推荐阅读