feign调用简单实例
表述一个简单的feign调用实现1. 服务端服务端添加一个client模块,专门用于跨微服务之间的feign调用;一下在该模块中操作。1.1 pom<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign<
·
表述一个简单的feign调用实现
注意:对象只能传jsonString,不能传层数多的对象,会丢数据。
最新参考实例:
https://blog.csdn.net/leinminna/article/details/124200936
1. 服务端
服务端添加一个client模块,专门用于跨微服务之间的feign调用;
一下在该模块中操作。
1.1 pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
1.2 feign接口
添加一个feign包,包中添加feign接口,用于调用该接口指向的业务实现代码,
与feign包同级添加,对外的client客户端。
feign接口代码
它的业务实现代码此处略。
package cn.cncomdata.snowflake.feign;
import cn.cncomdata.snowflake.vo.BaseVO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
/**
* author: xxx
* date: 2019/1/21 13:57
*/
@FeignClient(name = "snowflake", url = "${snowflake.service.url}")
public interface SnowflakeFeignClient {
@GetMapping("/id")
BaseVO<Long> uniqueId();
}
client代码
package cc.iooc.snowflake;
import cn.cncomdata.snowflake.feign.SnowflakeFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @program: snowflake
* @description: SnowflakeClient
* @author: xxx
* @create: 2019-03-21-12-51
**/
@Component
public class SnowflakeClient {
/**
* snowflakeFeignClient
*/
@Autowired
private SnowflakeFeignClient snowflakeFeignClient;
/**
* uniqueId
*
* @return Long
*/
public Long uniqueId() {
return snowflakeFeignClient.uniqueId().getData();
}
}
1.3 推送maven包管理仓库
方便项目加载,需要打包后推送到公司的maven中央仓库。
2.消费端
2.1 pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>cn.cncomdata</groupId>
<artifactId>snowflake-client</artifactId>
<version>1.0.0</version>
</dependency>
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://maven.cncomdata.cn/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
2.2 application.yaml
#本地调用可以IP加端口号
snowflake:
service:
url: ${snowflake_service_url:http://snowflake.test.yunmesh.com:10016}
feign:
hystrix:
enabled: false
client:
config:
default: # 服务名,default表示所有服务
connectTimeout: 300000
readTimeout: 300000
loggerLevel: full
2.3 启动类
添加@EnableFeignClients
package cn.cncmmdata.form;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* ElasticsearchApplication
* @author admin
*/
@SpringBootApplication(scanBasePackages = {"cn.cncomdata", "cc.iooc"},
exclude = {DataSourceAutoConfiguration.class})
@EnableFeignClients(basePackages = {"cn.cncomdata", "cc.iooc"})
@MapperScan("cn.cncomdata.form.repository")
public class FormApplication {
/**
* main
*
* @param args args
*/
public static void main(String[] args) {
SpringApplication.run(FormApplication.class, args);
}
}
2.4 使用
package cn.cncomdata.form.controller;
import cc.iooc.snowflake.SnowflakeClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @Author: leimin
* @Description: new class
* @Date: 2020/6/18 13:45
* @Version: 1.0
*/
@RestController
public class TestController {
@Resource
private SnowflakeClient snowflakeClient;
/**
* id生成器
* @return Long id
*/
@GetMapping("/id")
public Long getId(){
return snowflakeClient.uniqueId();
}
}
更多推荐
已为社区贡献5条内容
所有评论(0)