Ribben以及Feign实现负载均衡
SpringCloud
Ribbon是基于Netflix Ribben实现的一套 客户端负载均衡的工具 通过微服务名字访问
@RestController
public class DeptConsumerController {
//消费者不应该有service层
//支持restful风格请求
//RestTemplate 模板 可以直接调用(首先注册到Spring中)
// url地址 参数(map封装,实体,url传递) class返回值
//参数(String url, Class<T> responseType, Object... uriVariables)
@Autowired
private RestTemplate restTemplate;//提供多种便捷访问远程http的方法 简单的restful风格服务模板
//通过Ribbon去请求服务器时 REST_URL_PREFIX应该是个变量,通过服务名来访问 http://localhost:8001
private static final String REST_URL_PREFIX="http://SPRING-CLOUD-PROVIDER-DEPT";
//url:http://localhost:8001/dept/getAll
@RequestMapping("/consumer/dept/get1/{id}")
public Dept get(@PathVariable("id") Long deptno){
return restTemplate.getForObject (REST_URL_PREFIX+"/dept/get/"+deptno,Dept.class);
}
@RequestMapping("/consumer/dept/add")
public boolean add(Dept dept)
{
return restTemplate.postForObject (REST_URL_PREFIX+"/dept/add/",dept,Boolean.class);
}
@RequestMapping("/consumer/dept/list")
public List<Dept> getAll(){
return restTemplate.getForObject (REST_URL_PREFIX+"/dept/getAll/",List.class);
}
}
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@Autowired //获取具体信息,得到具体的服务
private org.springframework.cloud.client.discovery.DiscoveryClient discoveryClient;
@RequestMapping("/dept")
@ResponseBody
public String addDept11(){
return "deptService.addDept (dept)";
}
@PostMapping("/dept/add")
public boolean addDept(Dept dept){
return deptService.addDept (dept);
}
@GetMapping("/dept/get/{deptno}")
public Dept getDept(@PathVariable("deptno") Long deptno){
System.out.println (deptno);
return deptService.DeptById (deptno);
}
@GetMapping("/dept/getAll")
public List<Dept> getAllDept(){
System.out.println (deptService.queryAll ());
return deptService.queryAll ();
}
}
因为已经显示配置路径所以不需要消费者请求url名称与服务提供者请求url相同
Feign是基于webService客户端(增加一层Service,性能可能会变低),通过接口和注解访问(Feign本质还是Ribbon)
@Service
@FeignClient(value = "SPRING-CLOUD-PROVIDER-DEPT")
public interface DeptClientService {
@GetMapping("/dept/get/{id}") //需要与服务提供者端请求路径相同
public Dept queryById(@PathVariable("id")Long deptno);
@GetMapping("/dept/getAll")
public List<Dept> queryAll();
@PostMapping("/dept/add")
public boolean addDept(Dept dept);
}
@RestController
public class DeptConsumerController {
@Autowired
DeptClientService deptClientService=null;
@RequestMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long deptno){
return this.deptClientService.queryById (deptno);
}
@RequestMapping("/consumer/dept/add")
public boolean add(Dept dept)
{
return this.deptClientService.addDept (dept);
}
@RequestMapping("/consumer/dept/list")
public List<Dept> getAll(){
System.out.println (this.deptClientService.queryAll ());
return this.deptClientService.queryAll ();
}
}
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@Autowired //获取具体信息,得到具体的服务
private org.springframework.cloud.client.discovery.DiscoveryClient discoveryClient;
@RequestMapping("/dept")
@ResponseBody
public String addDept11(){
return "deptService.addDept (dept)";
}
@PostMapping("/dept/add")
public boolean addDept(Dept dept){
return deptService.addDept (dept);
}
@GetMapping("/dept/get/{deptno}")
public Dept getDept(@PathVariable("deptno") Long deptno){
System.out.println (deptno);
return deptService.DeptById (deptno);
}
@GetMapping("/dept/getAll")
public List<Dept> getAllDept(){
System.out.println (deptService.queryAll ());
return deptService.queryAll ();
}
}
没有显示配置路径,如果必须使Api Service层接口中路径与服务提供者相同,否则找不到页面