为了账号安全,请及时绑定邮箱和手机立即绑定

Spring Cloud Alibaba 开发案例之 Nacos 整合 Feign

标签:
Java SpringBoot

前言

前段时间,撸主为了调用小黄图API特意引入的Feign,这显然不是最优雅的使用方式。但是对于大部分中小公司来说,服务已经拆了辣么多了,基本能满足需求就可以了,什么熔断、集群啊可以都先一边靠靠。

这里先撸一个最贱单的例子分享给大家。

组件

  • Spring-Boot 2.2.0.RELEASE
  • Spring-Cloud Greenwich.SR3
  • Spring-Cloud-Alibaba 2.1.0.RELEASE
  • Nacos 注册中心
  • Feign HTTP客户端

说明一下,Spring Cloud Alibaba 已在2019年8月1日顺利毕业,并在 Spring 官方正式挂牌,挂牌成功后的最新版本是2.1.0

主项目

	<!--服务端、客户端-->
    <modules>
        <module>nacos-feign-server</module>
        <module>nacos-feign-client</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.2.0.RELEASE</spring-boot.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
        <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.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-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
           <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.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>
            <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>
        </dependencies>
    </dependencyManagement>

服务端

application.properties 配置:

# 项目contextPath 科帮网https://blog.52itstyle.vip
server.servlet.context-path=/
# 服务端口
server.port=8080
# session最大超时时间(分钟),默认为30
server.session-timeout=60
# tomcat最大线程数,默认为200
server.tomcat.max-threads=100
# tomcat的URI编码
server.tomcat.uri-encoding=UTF-8

# Nacos 注册中心
spring.application.name = nacos-discovery-server
spring.cloud.nacos.discovery.server-addr = 47.104.197.9:8848

简单的服务提供接口:

/**
 * 启动类
 */
@SpringBootApplication
@RestController
public class Application {
    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        logger.info("nacos-discovery-server启动");
    }

    @GetMapping("getMessage")
    public String getMessage(){
        return "来调戏我啊!!!";
    }
}

客户端

application.properties 配置:

# 项目contextPath 科帮网https://blog.52itstyle.vip
server.servlet.context-path=/
# 服务端口
server.port=8081
# session最大超时时间(分钟),默认为30
server.session-timeout=60
# tomcat最大线程数,默认为200
server.tomcat.max-threads=100
# tomcat的URI编码
server.tomcat.uri-encoding=UTF-8

# Nacos 注册中心
spring.application.name = nacos-discovery-client
spring.cloud.nacos.discovery.server-addr = 47.104.197.9:8848

配置客户端调用接口:

/**
 * 客户端调用
 */
@FeignClient(value = "nacos-discovery-server")
public interface FeignClientService {

    @RequestMapping(value="/getMessage")
    String getMessage();

}

客户端调用示例:

/**
 * 启动类
 */
@SpringBootApplication
@RestController
@EnableFeignClients
public class Application {
    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        logger.info("nacos-discovery-client启动");
    }

    @Autowired
    private  FeignClientService feignClientService;

    @GetMapping("getMessage")
    public String getMessage(){
        return feignClientService.getMessage();
    }
}

测试服务

分别启动 feign-serverfeign-client,然后登录Nacos控制台,如果出现以下两个服务说明配置成功。

客户端访问 http://localhost:8081/getMessage 如果返回来调戏我啊说明调用成功。

源码

参考

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
1.7万
获赞与收藏
2434

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消