fix:添加feign,未成功调用,还得参考之前的项目

上级 f3c2e916
......@@ -13,11 +13,35 @@
<description>nacos-server-common</description>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-hystrix</artifactId>-->
<!-- <version>1.4.7.RELEASE</version>-->
<!-- </dependency>-->
</dependencies>
</project>
\ No newline at end of file
package com.kwan.springcloudalibaba.api;
import com.kwan.springcloudalibaba.common.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* 使用feign配置goods-service服务
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/10 13:45
*/
@FeignClient(value = "nacos-server-producer/user", fallback = ComputeClientHystrix.class)
public interface ComputeClient {
@RequestMapping(method = RequestMethod.GET, value = "{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
Result getUserById(@PathVariable Integer id);
}
package com.kwan.springcloudalibaba.api;
import com.kwan.springcloudalibaba.common.Result;
import org.springframework.stereotype.Component;
/**
* feign的降级
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/14 16:47
*/
@Component
public class ComputeClientHystrix implements ComputeClient {
@Override
public Result getUserById(Integer id) {
return Result.error("feign方式请求 服务不可用 请求失败");
}
}
......@@ -20,6 +20,7 @@
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>com.kwan.springcloudalibaba</groupId>
......
......@@ -4,17 +4,11 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* http://localhost:8085/nacos/consumer/nameInfo
* <p>
* <p>
* 消费者
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/17 12:37
*/
//@EnableFeignClients
//@EnableCircuitBreaker
@SpringBootApplication
//@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class NacosConsumerApplication {
public static void main(String[] args) {
......
package com.kwan.springcloudalibaba.controller;
import com.kwan.springcloudalibaba.api.ComputeClient;
import com.kwan.springcloudalibaba.common.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
......@@ -12,18 +14,29 @@ import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
@RequestMapping("/nacos/consumer")
public class NacosConsumerController {
public class ConsumerUserController {
@Value("${user.name}")
private String nameInfo;
@Autowired
private ComputeClient computeClient;
/**
* http://127.0.0.1:8085/nacos/consumer/getUserById/1
*
* @param id
* @return
*/
@GetMapping(value = "/getUserById/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result getUserById(@PathVariable Integer id) {
return Result.ok(computeClient.getUserById(id));
}
/**
* 获取配置的变量
*/
@GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result nameInfo() {
return Result.ok(nameInfo);
return Result.ok();
}
}
......
......@@ -2,6 +2,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>com.kwan.springcloudalibaba</groupId>
<artifactId>nacos-server-parent</artifactId>
......
package com.kwan.springcloudalibaba.controller;
import com.kwan.springcloudalibaba.common.Result;
import com.kwan.springcloudalibaba.entity.User;
import com.kwan.springcloudalibaba.service.IUserService;
import lombok.extern.slf4j.Slf4j;
......@@ -24,19 +25,23 @@ public class UserController {
@Autowired
private IUserService userService;
/**
* http://127.0.0.1:9091/user/getUserById/1
*
* @param id
* @return
*/
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Result getUserById(@PathVariable Integer id) {
return Result.ok(userService.getUserById(id));
}
@RequestMapping(value = "/all", method = RequestMethod.GET)
public List<User> addAdvertise() {
log.info("测试日志={}", "success");
return userService.getUsers();
}
@RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)
public User getUserById(@PathVariable Integer id) {
return userService.getUserById(id);
}
@RequestMapping(value = "/getUserByName", method = RequestMethod.GET)
public User getUserByName(@RequestParam String sex) {
return userService.getUserByName(sex);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册