idea搭建springcloud项目_springcloud项目搭建第七节:Feign

本文基于上一节项目,介绍Idea搭建Spring Cloud项目的步骤。先删除two后缀项目,创建子项目business - client - feign,在pom文件导入包、配置application.yml,在启动类添加feign注解,创建接口类和控制类。还提到使用feign的优势及设置business - service端口动态的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于上一节项目,先把2个two后缀的项目删除了,让在创建一个子项目business-client-feign项目,子项目创建过程就不说了,之前章节有说过。

先在pom文件中导入包,这里spring-cloud-starter-openfeign就是feign要使用的jar包,其它跟business-client项目一样

<?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>spring-cloudartifactId>        <groupId>com.ydgroupId>        <version>1.0-SNAPSHOTversion>    parent>    <modelVersion>4.0.0modelVersion>    <artifactId>business-client-feignartifactId>    <dependencies>        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-webartifactId>        dependency>        <dependency>            <groupId>org.springframework.cloudgroupId>            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>        dependency>                <dependency>            <groupId>org.springframework.cloudgroupId>            <artifactId>spring-cloud-starter-netflix-hystrixartifactId>        dependency>                <dependency>            <groupId>org.springframework.cloudgroupId>            <artifactId>spring-cloud-starter-openfeignartifactId>        dependency>    dependencies>project>

然后配置application.yml文件这里几乎跟business-client一样就改了端口和服务名

server:  port: 8904spring:  application:    name: business-client-feigneureka:  client:    service-url:      defaultZone: http://127.0.0.1:8901/eureka    # 获取服务地址列表间隔时间,默认30秒    registry-fetch-interval-seconds: 5hystrix:  command:    default:      execution:        isolation:          thread:            timeoutInMilliseconds: 2000    circuitBreaker:      errorThresholdPercentage: 50 # 触发熔断错误比例阈值,默认值50%      sleepWindowInMilliseconds: 10000 # 熔断后休眠时长,默认值5秒      requestVolumeThreshold: 10 # 熔断触发最小请求次数,默认值是20

在启动类上添加feign注解@EnableFeignClients

package com.yd;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;/** * @Author: zengyz * @Date: 2020/11/16 10:01 */@SpringBootApplication/*DiscoveryClient使用 */@EnableDiscoveryClient@EnableCircuitBreaker@EnableFeignClientspublic class BusinessClientFeignApplication {    public static void main(String[] args) {        SpringApplication.run(BusinessClientFeignApplication.class);    }}

这时创建一个client的接口类,主要作用是封装接口调用,这样就不用想business-client项目一样去拼接接口地址了

package com.yd.controller.client;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;/** * @Author: zengyz * @Date: 2020/11/16 10:00 */@FeignClient("business-service")public interface UserClient {    @GetMapping("/user/test")    public String queryList();}

然后在创建一个控制类,在类中注入创建的接口类,这样就能对外提供接口,内部调用接口了

package com.yd.controller;import com.yd.controller.client.UserClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @Author: zengyz * @Date: 2020/11/16 9:59 */@RestController@RequestMapping("/cf")public class UserEntityFeignController {    @Autowired    private UserClient userClient;    @GetMapping("/test")    public String test(){        return userClient.queryList();    }}

使用feign时就不需要再启动类添加restTemplate实例化了,也可实现均衡负载,因为Feign内置ribbon。

64d8b990e6befd40a52fcf205fa4702b.png

项目business-service的application.yml配置文件中把端口设置成动态的

9b4fbad36c47710858f87d4e05745348.png

这样如果business-service需要启动多个时只有配置启动类设置不同的端口就可以了,下面是idea的设置过程,先打开图1界面,然后掉价edit configurations,打开图2 Run/Debug configurations 窗口这里就可以看到business-service的启动类配置选择,点击复制图标就可以生成一个启动类配置设置在端口就可以了,如图3

1a20a7e8080f24648b45bca8b4cba80a.png

8d5419ac4ad8b9220c878335a7bc1f00.png

2e12ac62f66c25b43d12b33d136c4f4a.png

安装Docker安装插件,可以按照以下步骤进行操作: 1. 首先,安装Docker。可以按照官方文档提供的步骤进行安装,或者使用适合您操作系统的包管理器进行安装。 2. 安装Docker Compose插件。可以使用以下方法安装: 2.1 下载指定版本的docker-compose文件: curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose 2.2 赋予docker-compose文件执行权限: chmod +x /usr/local/bin/docker-compose 2.3 验证安装是否成功: docker-compose --version 3. 在安装插件之前,可以测试端口是否已被占用,以避免编排过程中出错。可以使用以下命令安装netstat并查看端口号是否被占用: yum -y install net-tools netstat -npl | grep 3306 现在,您已经安装Docker安装Docker Compose插件,可以继续进行其他操作,例如上传docker-compose.yml文件到服务器,并在服务器上安装MySQL容器。可以参考Docker的官方文档或其他资源来了解如何使用DockerDocker Compose进行容器的安装和配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Docker安装docker-compose插件](https://blog.csdn.net/qq_50661854/article/details/124453329)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Docker安装MySQL docker安装mysql 完整详细教程](https://blog.csdn.net/qq_40739917/article/details/130891879)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值