提交 2f646233 编写于 作者: 檀越@新空间's avatar 檀越@新空间 🐭

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

上级 f3c2e916
...@@ -13,11 +13,35 @@ ...@@ -13,11 +13,35 @@
<description>nacos-server-common</description> <description>nacos-server-common</description>
<version>1.0.0</version> <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> <dependencies>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
</dependency> </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> </dependencies>
</project> </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 @@ ...@@ -20,6 +20,7 @@
<spring-boot.version>2.3.0.RELEASE</spring-boot.version> <spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version> <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.kwan.springcloudalibaba</groupId> <groupId>com.kwan.springcloudalibaba</groupId>
......
...@@ -4,17 +4,11 @@ import org.springframework.boot.SpringApplication; ...@@ -4,17 +4,11 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* http://localhost:8085/nacos/consumer/nameInfo //@EnableFeignClients
* <p> //@EnableCircuitBreaker
* <p>
* 消费者
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/1/17 12:37
*/
@SpringBootApplication @SpringBootApplication
//@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class NacosConsumerApplication { public class NacosConsumerApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
package com.kwan.springcloudalibaba.controller; package com.kwan.springcloudalibaba.controller;
import com.kwan.springcloudalibaba.api.ComputeClient;
import com.kwan.springcloudalibaba.common.Result; 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.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -12,18 +14,29 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -12,18 +14,29 @@ import org.springframework.web.bind.annotation.RestController;
@RefreshScope @RefreshScope
@RestController @RestController
@RequestMapping("/nacos/consumer") @RequestMapping("/nacos/consumer")
public class NacosConsumerController { public class ConsumerUserController {
@Value("${user.name}") @Autowired
private String nameInfo; 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) @GetMapping(value = "/nameInfo", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result nameInfo() { public Result nameInfo() {
return Result.ok(nameInfo); return Result.ok();
} }
} }
......
...@@ -2,6 +2,18 @@ ...@@ -2,6 +2,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <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"> 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> <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> <parent>
<groupId>com.kwan.springcloudalibaba</groupId> <groupId>com.kwan.springcloudalibaba</groupId>
<artifactId>nacos-server-parent</artifactId> <artifactId>nacos-server-parent</artifactId>
......
package com.kwan.springcloudalibaba.controller; package com.kwan.springcloudalibaba.controller;
import com.kwan.springcloudalibaba.common.Result;
import com.kwan.springcloudalibaba.entity.User; import com.kwan.springcloudalibaba.entity.User;
import com.kwan.springcloudalibaba.service.IUserService; import com.kwan.springcloudalibaba.service.IUserService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -24,19 +25,23 @@ public class UserController { ...@@ -24,19 +25,23 @@ public class UserController {
@Autowired @Autowired
private IUserService userService; 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) @RequestMapping(value = "/all", method = RequestMethod.GET)
public List<User> addAdvertise() { public List<User> addAdvertise() {
log.info("测试日志={}", "success"); log.info("测试日志={}", "success");
return userService.getUsers(); 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) @RequestMapping(value = "/getUserByName", method = RequestMethod.GET)
public User getUserByName(@RequestParam String sex) { public User getUserByName(@RequestParam String sex) {
return userService.getUserByName(sex); return userService.getUserByName(sex);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册