feign的搭建

1.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>net.xdclass</groupId>

    <artifactId>order_service</artifactId>

    <version>1.0-SNAPSHOT</version>

 

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.3.RELEASE</version>

        <relativePath/> <!-- lookup parent from repository -->

    </parent>

 

    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <java.version>1.8</java.version>

        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>

    </properties>

 

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>

        </dependency>

 

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-start-openfeign</artifactId>

        </dependency>

 

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

    </dependencies>

 

    <dependencyManagement>

        <dependencies>

            <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>

 

2.启动类增加@EnableFeignClients

 

3.application.yml

server:
 
port: 8782


#指定注册中心地址
eureka:
 
client:
   
serviceUrl:
     
defaultZone: http://localhost:8761/eureka/

#服务的名称
spring:
 
application:
   
name: order-service

 

3.增加一个 interface

@FeignClient

public inteface Product{

   

@requestMapping(“/zpi/v1/product/hello”)

@ResponseBody

public String hello(){

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud 是一个基于 Spring Boot 的框架,用于快速构建分布式系统的通用工具集。Nacos 是阿里巴巴开源的注册中心和配置中心,而 Feign 是一个声明式的 HTTP 客户端,使得编写 Web 服务客户端变得更加简单。 下面是 Spring Cloud 项目搭建,并整合 Nacos 和 Feign 组件的步骤: 1. 创建父工程,添加以下依赖: ``` <dependencyManagement> <dependencies> <!-- Spring Cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2020.0.1</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.4.4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 2. 创建子工程,添加以下依赖: ``` <!-- Nacos 注册中心和配置中心 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.3.RELEASE</version> </dependency> <!-- Feign 声明式 HTTP 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>3.0.2</version> </dependency> ``` 3. 在子工程的启动类上添加注解 `@EnableDiscoveryClient` 和 `@EnableFeignClients`,分别启用服务注册与发现和 Feign 组件。 ``` @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4. 配置 Nacos 注册中心和配置中心的地址,可以在 `application.properties` 文件中添加以下配置: ``` # Nacos 注册中心和配置中心地址 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.config.server-addr=127.0.0.1:8848 ``` 5. 创建 Feign 接口,使用 `@FeignClient` 注解指定要调用的服务名称和请求路径。 ``` @FeignClient(name = "service-provider") public interface HelloFeignClient { @GetMapping("/hello") String hello(); } ``` 6. 在需要使用 Feign 的地方注入 `HelloFeignClient` 对象,即可调用服务提供者的 `/hello` 接口。 ``` @RestController public class HelloController { @Autowired private HelloFeignClient helloFeignClient; @GetMapping("/hello") public String hello() { return helloFeignClient.hello(); } } ``` 至此,Spring Cloud 项目已经搭建完成,并整合了 Nacos 和 Feign 组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值