springcloud搭建实战<三>【消费方 consumer+FeignClient调用】

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-23 13:05   11   0

工具:IntelliJ IDEA 2019.2.4

JDK:1.8

父项目以及注册中心、服务方详见之前博客。

【消费方与之前服务方步骤一致,以下文章步骤基本一致】

补充:服务之间调用:ribbon 、 feign。那为什么选择feign呢?

feign其实封装了ribbon,也就是说,ribbon的配置feign也生效。

其次,feign的使用,是接口更加规范。代码会更加统一。

第一步:新建消费方 springcloud-consumer

1、打开父项目,file->new module

2、选择spring Initialize ->next

3、填写项目信息、next

4、dependencies 选择Srping Cloud Routing->OpenFeign 选择springboot版本。next,finish.

5、pom.xm 文件修改

<?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>com.cloud</groupId>
        <artifactId>springcloud_demo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cloud</groupId>
    <artifactId>cloud_client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cloud_client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

    <dependencies>
        <!--web依赖-->
        <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-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>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>

6、修改application配置文件,本人习惯yml配置.

注意:name 最好不要用下划线_ ,否则feign之间调用会报错。

server:
  port: 8091

spring:
  application:
    name: cloud-consumer

eureka:
  client:
    service-url:
      defaultZone: http://user:123456@localhost:8080/eureka/ #服务注册中信地址,含有BASIC认证的用户名和密码
  instance:
    prefer-ip-address: true #将IP注册到服务注册中心

7、写一个测试类controller

@RestController
public class UserController {
    @Autowired
    UserFeignClient userFeignClient;

    @RequestMapping(value = "/consumerHello",method = RequestMethod.GET)
    public String consumerTest(){
        return userFeignClient.sayHi();
    }
}

8、写一个测试Client,并通过FeignClient方式调用provider服务

注意:最好不要用GetMapping,否则有可能报404

@FeignClient(name = "cloud-provider")
public interface UserFeignClient {
    @RequestMapping(value = "/sayHi" ,method = RequestMethod.GET)
    public String sayHi();
}

9、修改启动文件,CloudProviderApplication .java, 启动项目

注意:确保provider服务端已经正常启动。

provider项目详见上一篇

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class CloudConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudConsumerApplication.class, args);
    }

}

9、访问测试接口

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP