|
|
@@ -1,70 +0,0 @@
|
|
|
-package anyi.space.memberConsumer.controller;
|
|
|
-
|
|
|
-import anyi.sapce.common.entity.Member;
|
|
|
-import anyi.sapce.common.entity.ResponseResult;
|
|
|
-import anyi.space.memberConsumer.service.MemberService;
|
|
|
-import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import lombok.RequiredArgsConstructor;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.cloud.client.ServiceInstance;
|
|
|
-import org.springframework.cloud.client.discovery.DiscoveryClient;
|
|
|
-import org.springframework.util.DigestUtils;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
-
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
-/**
|
|
|
- * @ProjectName: distributedSystemLearn
|
|
|
- * @FileName: MemberConsumerController
|
|
|
- * @Author: 杨逸
|
|
|
- * @Data:2025/4/13 20:07
|
|
|
- * @Description:
|
|
|
- */
|
|
|
-@Slf4j
|
|
|
-@RequiredArgsConstructor
|
|
|
-@RestController
|
|
|
-@RequestMapping("/member")
|
|
|
-public class MemberConsumerController {
|
|
|
- private final MemberService memberService;
|
|
|
- private final DiscoveryClient discoveryClient;
|
|
|
- private final ObjectMapper objectMapper;
|
|
|
- @GetMapping
|
|
|
- public ResponseResult<Member> getMemberById(Long id){
|
|
|
- Member member = memberService.getById(id);
|
|
|
- return ResponseResult.okResult(member);
|
|
|
- }
|
|
|
- @PostMapping
|
|
|
- public ResponseResult addMember(Member member){
|
|
|
- String hex = DigestUtils.md5DigestAsHex(member.getPwd().getBytes(StandardCharsets.UTF_8));
|
|
|
- member.setPwd(hex);
|
|
|
- boolean save = memberService.save(member);
|
|
|
- return ResponseResult.okResult(save);
|
|
|
- }
|
|
|
- @GetMapping("/discovery")
|
|
|
- public ResponseResult discovery(){
|
|
|
- //获取所有注册的服务名称
|
|
|
- List<String> services = discoveryClient.getServices();
|
|
|
- for (int i = 0; i < services.size(); i++) {
|
|
|
- String service = services.get(i);
|
|
|
- log.info("服务名称:{}",service);
|
|
|
- //获取对应服务的实例信息列表
|
|
|
- List<ServiceInstance> instances = discoveryClient.getInstances(service);
|
|
|
- for (int j = 0; j < instances.size(); j++) {
|
|
|
- //获取具体服务实例的信息,比如IP和端口等
|
|
|
- ServiceInstance instance = instances.get(j);
|
|
|
- try {
|
|
|
- log.info("服务名称:{},服务实例信息:{}",service,objectMapper.writeValueAsString(instance));
|
|
|
- } catch (JsonProcessingException e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.error("服务名称:{},服务实例信息:{}",service,e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return ResponseResult.okResult(services);
|
|
|
- }
|
|
|
-}
|