欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

我的中软国际线上学习日志16-springcloud

程序员文章站 2022-06-30 19:30:30
zr-consumer1、UserClient.javapackage com.zr.client;import com.zr.pojo.User;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMappin...

zr-consumer
1、UserClient.java

package com.zr.client;

import com.zr.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "zr-provider",fallback = UserClientFallback.class)
public interface UserClient {

    @RequestMapping("user/{id}")
    User queryById(@PathVariable("id")Long id);
}

2、UserClientFailback.java

package com.zr.client;

import com.zr.pojo.User;
import org.springframework.stereotype.Component;

@Component
public class UserClientFallback implements UserClient{

    @Override
    public User queryById(Long id) {
        User user=new User();
        user.setUsername("失败啦。。。。");
        return user;
    }
}

3、UserController.java

package com.zr.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zr.client.UserClient;
import com.zr.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Controller
@RequestMapping("consumer/user")
@DefaultProperties(defaultFallback = "fallBackMethod")
public class UserController {

    /*@Autowired
    public RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;*/

    @Autowired
    private UserClient userClient;

    @RequestMapping("{id}")
    @ResponseBody
   // @HystrixCommand
    public String queryById(@PathVariable Long id){
       /* User user=restTemplate.getForObject("http://localhost:8081/user/"+id,User.class);*/
        /*List<ServiceInstance> instances = discoveryClient.getInstances("zr-provider");
        ServiceInstance instance = instances.get(1);
        User user=restTemplate.getForObject("http://localhost:"+instance.getPort()+"/user/"+id,User.class);
        return user.toString();*/
        User user = userClient.queryById(id);
        return user.toString();

    }

   /* public String fallBackMethod(){
        return "请求反面,请以后再尝试!!";
    }*/
}

4、application.yml

server:
  port: 8082
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
spring:
  application:
    name: zr-consumer
feign:
  hystrix:
    enabled: true

zr-provoder

1、UserController.java

package com.zr.controller;

import com.zr.pojo.User;
import com.zr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("{id}")
    public User queryById(@PathVariable("id")Long id){
        return  userService.queryById(id);
    }
}

2、UserService.java

package com.zr.service;

import com.zr.dao.UserDao;
import com.zr.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public User queryById(Long id){
        return userDao.selectByPrimaryKey(id);
    }
}

4、application.yml

server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/springcloud
    username: root
    password: 123456
  application:
    name: zr-provider
mybatis:
  type-aliases-package: com.zr.pojo
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

我的中软国际线上学习日志16-springcloud

本文地址:https://blog.csdn.net/weixin_42423962/article/details/107383276

相关标签: 个人学习 spring