springcloud – alibaba – 2 – 集成Feign 和 Ribbon – 更新完成

导读:本篇文章讲解 springcloud – alibaba – 2 – 集成Feign 和 Ribbon – 更新完成,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1、依赖

  • 依赖管理

<parent>
        <artifactId>spring-boot-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.3.12.RELEASE</version>
        <relativePath/>
    </parent>

  • 项目需要的依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

<!--        springcloud-alibaba需要的依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR12</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

截图

2、服务提供者

2.1)、yml配置


server:
  port: 8011

spring:
  application:
    name: ALIBABA-PUBLISHER
  cloud:
    nacos:
      discovery:
        server-addr: 162.14.66.60:8848  # 自己的服务器ip:8848

management:
  endpoints:
    web:
      exposure:
        include: "*" #健康检查

截图

2.2)、启动类编写


package cn.zixieqing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @ClassName PublisherApplication
 * @Author ZiXieQing
 * @Date 2021/12/7
 * Version 1.0
 **/

@SpringBootApplication
@EnableDiscoveryClient      // 开启nacos的客户端功能
public class PublisherApplication {

    public static void main(String[] args) {

        SpringApplication.run(PublisherApplication.class, args);
    }
}

截图

2.3)、controller编写


package cn.zixieqing.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/provider")
public class ProvideService {

    @GetMapping("/service")
    public String provideService() {

        return "this is my son";
    }
}

3、Feign

3.1)、导入依赖


    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

截图

3.2)、编写yml


server:
  port: 8012

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 162.14.66.60:8848  # 自己的服务器ip:8848

  application:
    name: ALIBABA-OPENFEIGN

截图

3.3)、接口编写


package cn.zixieqing;


import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "ALIBABA-PUBLISHER")  // publisher中注册进去的服务名
public interface ProxyService {

    @RequestMapping(value = "/provider/service" , method = RequestMethod.GET)
    String provideService();

}

截图

3.4)、启动类编写


package cn.zixieqing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;


@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OpenFeignApplication {

    public static void main(String[] args) {

        SpringApplication.run(OpenFeignApplication.class, args);
    }
}

截图

3.5)、服务消费层编写


package cn.zixieqing.controller;

import cn.zixieqing.ProxyService;
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;

/**
 * @ClassName getService
 * @Author ZiXieQing
 * @Date 2021/12/7
 * Version 1.0
 **/

@RestController
@RequestMapping("/test")
public class getService {

    @Autowired
    private ProxyService proxyService;

    @GetMapping("/getService")
    public String getServer() {

        return proxyService.provideService();

    }
}

截图

4、启动publisher 和 Feign

截图

截图

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/11402.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!