上篇博客已经为大家简单介绍了Spring Cloud Alibaba以及组件Nacos并且实现了服务注册与发现,接下来本篇博客为大家介绍两个实例引入服务发现。
我们已经创建了两个服务一个是user另一个是content
目录结构为:
服务生产者
首先我们在User项目中编写一个接口(具体实现省略,此处不涉及业务)
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserController {
private final UserService userService;
@GetMapping("/{id}")
public User findById(@PathVariable Integer id) {
return userService.findById(id);
}
}
服务消费者
1.使用RestTemplate方式调用
1.调用地址url 写死
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShareService {
private final RestTemplate restTemplate;
// 第一种利用
UserDTO userDTO = restTemplate.getForObject("http://localhost:8080/users/{id}", UserDTO.class, userId);
//消息装配
ShareDTO shareDTO = new ShareDTO();
BeanUtils.copyProperties(share,shareDTO);
shareDTO.setWxNickname(userDTO.getWxNickname());
return shareDTO;
}
}
2.利用DiscoveryClient 获取实例地址
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShareService {
private final RestTemplate restTemplate;
private final DiscoveryClient discoveryClient;
public ShareDTO findById(Integer id) {
//用户中心所有实例的信息
List<ServiceInstance> instances = discoveryClient.getInstances("center-user");
String targetURL = instances.stream().map(instance -> instance.getUri().toString() + "/users/{id}")
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("当前没有实例"));
//第二种利用discoveryClient 获取实例
UserDTO userDTO = restTemplate.getForObject(targetURL, UserDTO.class, userId);
//消息装配
ShareDTO shareDTO = new ShareDTO();
BeanUtils.copyProperties(share,shareDTO);
shareDTO.setWxNickname(userDTO.getWxNickname());
return shareDTO;
}
3. 使用Ribbon调用实现负载均衡
3.1 给RestTemplate添加注解
@Bean
//整合Ribbon 的注解
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
3.2 修改类直接调用
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShareService {
private final ShareMapper shareMapper;
private final RestTemplate restTemplate;
private final DiscoveryClient discoveryClient;
public ShareDTO findById(Integer id) {
//获取分享详情
Share share = this.shareMapper.selectByPrimaryKey(id);
//发布人id
Integer userId = share.getUserId();
// 第三种 使用ribbon
String targetURL = "http://center-user/users/{userId}";
UserDTO userDTO = restTemplate.getForObject("http://center-user/users/{userId}", UserDTO.class, userId);
//消息装配
ShareDTO shareDTO = new ShareDTO();
BeanUtils.copyProperties(share,shareDTO);
shareDTO.setWxNickname(userDTO.getWxNickname());
return shareDTO;
}
}
4.使用feign实现远程HTTP调用
4.1 引入openfeign依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
4.2 启动类添加注解 @EnableFeignClients
@MapperScan("com.thyme")
@SpringBootApplication
@EnableFeignClients
public class ContentApplication {
public static void main(String[] args) {
SpringApplication.run(ContentApplication.class, args);
}
}
4.3 新建 CenterUserFeignClient 类
@FeignClient(name = "center-user")
public interface CenterUserFeignClient {
@GetMapping("/users/{id}")
UserDTO findById(@PathVariable Integer id);
}
4.4 修改调用 ShareService类代码
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShareService {
private final ShareMapper shareMapper;
private final CenterUserFeignClient centerUserFeignClient;
public ShareDTO findById(Integer id) {
//获取分享详情
Share share = this.shareMapper.selectByPrimaryKey(id);
//发布人id
Integer userId = share.getUserId();
// 第四种 使用feign实现远程HTTP 调用
UserDTO userDTO = centerUserFeignClient.findById(userId);
//消息装配
ShareDTO shareDTO = new ShareDTO();
BeanUtils.copyProperties(share,shareDTO);
shareDTO.setWxNickname(userDTO.getWxNickname());
return shareDTO;
}
}