To include Ribbon in your project, use the starter with a group ID of org.springframework.cloud
and an artifact ID of spring-cloud-starter-netflix-ribbon
. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.
在官网中提到如果使用Ribbon就添加
org.springframework.cloud spring-cloud-starter-netflix-ribbon
其实除了添加这个还需要添加Eureka的client是用来发现Eureka中的服务的
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
spring:
application:
name: order-service
server:
port: 8781
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public class OrderController {
@Resource
private RestTemplate restTemplate;
@Resource
private ProductClient productClient;
// (name = "save",produces ={MediaType.APPLICATION_PROBLEM_JSON_VALUE})
@RequestMapping(value = "save",produces =MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Map save(Integer id){
Map result=new HashMap<>();
Product pro = restTemplate.getForObject("http://product-service/api/product/findById?id=" + id, Product.class);
Product product = productClient.findById(id);
System.out.println("feign 获取到的product:"+product.getName());
result.put("code","1");
result.put("data","success");
result.put("message","订单提交成功");
result.put("product_name",pro.getName());
return result;
}
}
To include Feign in your project use the starter with group org.springframework.cloud
and artifact id spring-cloud-starter-openfeign
. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.
org.springframework.cloud
spring-cloud-starter-openfeign
然后在启动类上加入注解@EnableFeignClients
然后编写Feign相关的client
package com.example.eurekaorderservice;
import com.example.eurekaorderservice.domain.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name="product-service")
public interface ProductClient {
@GetMapping("/api/product/findById")
public Product findById(Integer id);
}
在使用到它的地方调用它即可
package com.example.eurekaorderservice.controller;
import ch.qos.logback.core.net.SyslogOutputStream;
import com.example.eurekaorderservice.ProductClient;
import com.example.eurekaorderservice.domain.Product;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@RequestMapping("/api/order")
@RestController
public class OrderController {
@Resource
private RestTemplate restTemplate;
@Resource
private ProductClient productClient;
// (name = "save",produces ={MediaType.APPLICATION_PROBLEM_JSON_VALUE})
@RequestMapping(value = "save",produces =MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Map save(Integer id){
Map result=new HashMap<>();
Product pro = restTemplate.getForObject("http://product-service/api/product/findById?id=" + id, Product.class);
Product product = productClient.findById(id);
System.out.println("feign 获取到的product:"+product.getName());
result.put("code","1");
result.put("data","success");
result.put("message","订单提交成功");
result.put("product_name",pro.getName());
return result;
}
}