服务注册与发现参考
https://blog.csdn.net/qq_29752857/article/details/129220590
一、使用 RestTemplate调用
注入RestTemplate
package com.test.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration //@LoadBalancerClient(value = "userservice", configuration = LoadBalancerConfig.class) public class BeanConfiguration { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
服务调用
package com.test.service.impl; import com.minos.entity.Book; import com.minos.entity.Borrow; import com.minos.entity.User; import com.minos.entity.UserBorrowDetail; import com.test.mapper.BorrowMapper; import com.test.service.BorrowService; import com.test.service.client.UserClient; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.util.List; import java.util.stream.Collectors; @Service public class BorrowServiceImpl implements BorrowService { @Resource BorrowMapper mapper; @Resource RestTemplate restTemplate; @Resource UserClient userClient; @Override public UserBorrowDetail getUserBorrowDetailByUid(int uid) { List<Borrow> borrow = mapper.getBorrowsByUid(uid); //RestTemplate支持多种方式的远程调用 //这里通过调用getForObject来请求其他服务,并将结果自动进行封装 //获取User信息 User user = restTemplate.getForObject("http://userService/user/" + uid, User.class); //User user = userClient.getUserById(uid); //获取每一本书的详细信息 List<Book> bookList = borrow .stream() .map(b -> restTemplate.getForObject("http://bookService/book/" + b.getBid(), Book.class)) .collect(Collectors.toList()); return new UserBorrowDetail(user, bookList); } }
二、使用OpenFeign调用
增加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
@EnableFeignClients
定义调用接口
package com.test.service.client; import com.minos.entity.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; //@FeignClient(value = "userService", fallback = UserFallbackClient.class) //声明为userservice服务的HTTP请求客户端 @FeignClient(value = "userService") //声明为userservice服务的HTTP请求客户端 public interface UserClient { //路径保证和其他微服务提供的一致即可 @RequestMapping("/user/{uid}") User getUserById(@PathVariable("uid") int uid); //参数和返回值也保持一致 }
使用OpenFeign调用,注入调用接口,调用方法。
package com.test.service.impl; import com.minos.entity.Book; import com.minos.entity.Borrow; import com.minos.entity.User; import com.minos.entity.UserBorrowDetail; import com.test.mapper.BorrowMapper; import com.test.service.BorrowService; import com.test.service.client.UserClient; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.util.List; import java.util.stream.Collectors; @Service public class BorrowServiceImpl implements BorrowService { @Resource BorrowMapper mapper; @Resource RestTemplate restTemplate; @Resource UserClient userClient; @Override public UserBorrowDetail getUserBorrowDetailByUid(int uid) { List<Borrow> borrow = mapper.getBorrowsByUid(uid); //RestTemplate支持多种方式的远程调用 //这里通过调用getForObject来请求其他服务,并将结果自动进行封装 //获取User信息 //User user = restTemplate.getForObject("http://userService/user/" + uid, User.class); User user = userClient.getUserById(uid); //获取每一本书的详细信息 List<Book> bookList = borrow .stream() .map(b -> restTemplate.getForObject("http://bookService/book/" + b.getBid(), Book.class)) .collect(Collectors.toList()); return new UserBorrowDetail(user, bookList); } }