ums调用notice

UMS调用notice

在notice中写好代码,用ums调用,notice中用到的实体类,在ums中相同的地方也创建出来,另外在多创建一个client层或者config层

在这里插入图片描述
在这里插入图片描述

ums调用notice层用两种方法,可以用openfeign和普通方式,普通方式中需要定义url并且创建config类

普通方式 定义url和创建config层

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

在要使用的controller中调用 RestTemplate 定义url,url里的是notice注册到eureka中的服务器名字。

 public static final String NOTICE_URL="http://NOTICE-SERVICE";

    @Resource
    private RestTemplate restTemplate;

也可以使用注解,在client层创建一个接口,加上注解,value中是notice的服务器名字,里面的方法调用的是notice中的方法,方法中的value要和notice中的方法同名。

openfeign注解,client层加注解

添加依赖

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
import four.eneity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


@Component
@FeignClient(value="NOTICE-SERVICE") //微服务名称
public interface NoticeClient {

    @PostMapping(value="/login")
    public String selectById(@RequestBody User user);

}

在controller中调用client中的方法

import four.client.NoticeClient;
import four.eneity.User;
import four.model.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;

@CrossOrigin
@RestController
@Slf4j
public class FollowController {
    
    @Resource
    private NoticeClient noticeClient;
    
    @PostMapping(value="/login")
    public JsonResult follow(@RequestBody  User user){
        JsonResult js=new JsonResult();
        String s=noticeClient.selectById(user);
        if(s.equals("ok")){
            js.setCode(0);
            js.setMsg("成功");
            js.setData("你好");
        }else{
            js.setCode(200);
            js.setMsg("失败");
            js.setData("该用户不存在");
        }

        return js;
    }
}

最后打开服务,用postman等工具测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值