Feign是实现微服务架构下,不同服务之间通信很好的方式。在这里小编会给大家带来服务之间通信的几种方式。
1.LoadBalancerClient
2.RestTemplate
3 以及我们的主角 @FeignClient(name = “”)
eureka 注册中心
https://blog.csdn.net/weixin_43055096/article/details/98474285
config 配置中心
https://blog.csdn.net/weixin_43055096/article/details/98474855
话不多说开始
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
spring:
application:
name: order
datasource:
driver-class-name: com.mysql.jdbc.Driver
password: 1234
url: jdbc:mysql://127.0.0.1:3306/sell?serverTimezone=UTC
username: wcw
jpa:
show-sql: true
spring:
application:
name: client #这里对应的git上的前缀
cloud:
config:
discovery:
enabled: true
service-id: CONFIG
profile: cev #client-cev.yml
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
package com.wuge.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
1 采用 RestTemplate (直接使用restTemplate,url 写死 不太好)
@GetMapping("/getProductMsg")
public String getMessage(){
RestTemplate restTemplate = new RestTemplate();
String result =restTemplate.getForObject("http://localhost:8081/message",String.class);
log.info("response={}",result);
return result;
}
restTemplate.getForObject(url , class)
第一个url为目标地址的方法名
第二个class 为返回的类型
2 采用 LoadBalancerClient (利用loadBalancerClient 通过应用名来找到对应url 在使用restTemplate)
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/getProductMsg")
public String getMessage(){
RestTemplate restTemplate = new RestTemplate();
//在eureka的服务名
ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT");
//serviceInstance可获取到目标服务的端口号 我们这里序列化一下就OK
String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort())+"/message";
String result = restTemplate.getForObject(url,String.class);
log.info("reponse{}",result);
log.info("port{}",serviceInstance.getPort());
return result;
}
3 RestTemplate注解的方式
首先写一个配置类
package com.wuge.order.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
其次在用注解
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getProductMsg")
public String getMessage(){
//这里的url为eureka服务上的服务名外加访问的mapping
String result = restTemplate.getForObject("http://PRODUCT/msg",String.class);
log.info("reponse{}",result);
return result;
}
package com.wuge.order.client;
import com.wuge.order.Dto.CartDto;
import com.wuge.order.entity.ProductInfo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
/**
* 同步调用 直接通信
*/
@FeignClient(name = "product") //eureka上的名字
public interface ProductClient {
@GetMapping("/msg")
String productMsg();
@PostMapping("/product/listForOrder")
List<ProductInfo> listForOrder(@RequestBody List<String> productIdList);
@PostMapping("/product/decreaseStock")
void decreaseStock(@RequestBody List<CartDto> cartList);
}
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getProductMsg")
public String getMessage(){
@Autowired
private ProductClient productClient;
String result = productClient.productMsg();
log.info("reponse{}",result);
return result;
}