标签:Feign String SpringCloud springframework GetMapping org import port 搭建
Feign
一、搭建提供者模块(普通EurekaClient)
- 右击父工程,在父工程下创建Feign模块
- 在启动类上添加@EnableEurekaClient
3. 配置application.yml
server:
port: 8001
#指定当前eureka客户端注册地址
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka
# 指定应用名称
spring:
application:
name: provider
- 写一个Controller(对外提供接口服务)
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName UserController
* @Description TODO
* @author cuiyingfan
* @date 2021/8/4 16:38
* @Version 1.0
*/
@RestController
@RequestMapping("user")
public class UserController {
@Value("${server.port}")
String port;
@GetMapping("/sayHello")
public String sayhello() {
return "I`m provider " + port + " ,Hello consumer!";
}
@GetMapping("/sayHi")
public String sayHi() {
return "I`m provider " + port + " ,Hi consumer!";
}
@GetMapping("/sayHaha")
public String sayHaha() {
return "I`m provider " + port + " ,Haha consumer!";
}
}
- 服务提供者已经搭建完成,启动项目测试一下
访问 http://localhost:8001/user/sayHi 查看
二、搭建消费者模块(Feign)
- 依旧父工程下新建模块
- 启动类上添加@EnableEurekaClient 和 @EnableFeignClients
- 修改application.yml
server:
port: 8003
#指定当前eureka客户端注册地址
eureka:
client:
service-url:
defaultZone: http://localhost:8000/eureka
# 指定应用名称
spring:
application:
name: consumer
- 写一个跟服务提供者模块相同url的接口类
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @ClassName UserApi
* @Description TODO
* @author cuiyingfan
* @date 2021/8/4 16:55
* @Version 1.0
*/
@FeignClient("provider")
public interface UserApi {
@GetMapping("user/sayHello")
String sayhello();
@GetMapping("user/sayHi")
String sayHi();
@GetMapping("user/sayHaha")
String sayHaha();
}
- 写一个消费者自己的controller去调用提供者提供的接口
import com.ivan.user.api.UserApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName UserController
* @Description TODO
* @author cuiyingfan
* @date 2021/8/4 17:01
* @Version 1.0
*/
@RestController
public class UserController {
@Autowired
private UserApi userApi;
@GetMapping("user")
public String show() {
return userApi.sayHaha();
}
}
- 再复制出一个服务提供者模块(记得修改端口 -Dserver.port=8002),并启动所有模块
启动后共4个模块,Eureka注册中心、2个服务提供者、1个消费者
7. 访问消费者对外提供的接口 http://localhost:8003/user ,发现消费者通过Ribbon负载均衡轮询调用2个服务提供者
标签:Feign,String,SpringCloud,springframework,GetMapping,org,import,port,搭建 来源: https://www.cnblogs.com/isIvanTsui/p/15099929.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。