完成学习nacos请求转发
This commit is contained in:
46
order-service/pom.xml
Normal file
46
order-service/pom.xml
Normal file
@@ -0,0 +1,46 @@
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cn.mayiming</groupId>
|
||||
<artifactId>javamemories-parent</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>order-service</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>order-service</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 可选:Nacos 配置管理(从 Nacos 读取配置文件) -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<!-- 负载均衡(Feign 内置,但显式引入更清晰) -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
19
order-service/src/main/java/cn/mayiming/App.java
Normal file
19
order-service/src/main/java/cn/mayiming/App.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package cn.mayiming;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package cn.mayiming.Controller;
|
||||
|
||||
|
||||
import cn.mayiming.Service.OrderService;
|
||||
import cn.mayiming.entity.User;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class OrderController {
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@PostMapping("/order")
|
||||
public User getOrder(@RequestBody User user){
|
||||
return orderService.SearchUserbyname(user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cn.mayiming.Service;
|
||||
|
||||
import cn.mayiming.entity.User;
|
||||
import cn.mayiming.feign.UserFeignClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
@Autowired
|
||||
private UserFeignClient userFeignClient;
|
||||
|
||||
public User SearchUserbyname (User user) {
|
||||
return userFeignClient.selectByUsername(user);
|
||||
}
|
||||
|
||||
}
|
||||
22
order-service/src/main/java/cn/mayiming/entity/User.java
Normal file
22
order-service/src/main/java/cn/mayiming/entity/User.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package cn.mayiming.entity;
|
||||
|
||||
public class User {
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
String username;
|
||||
String password;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.mayiming.feign;
|
||||
|
||||
import cn.mayiming.entity.User;
|
||||
import feign.Param;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
||||
@FeignClient(name = "user-service")
|
||||
public interface UserFeignClient {
|
||||
@PostMapping("/user")
|
||||
User selectByUsername(User user);
|
||||
}
|
||||
21
order-service/src/main/resources/application.yml
Normal file
21
order-service/src/main/resources/application.yml
Normal file
@@ -0,0 +1,21 @@
|
||||
server:
|
||||
port: 9092
|
||||
spring:
|
||||
application:
|
||||
name: order-service
|
||||
cloud:
|
||||
nacos:
|
||||
# 服务注册发现配置
|
||||
discovery:
|
||||
server-addr: localhost:8848 # Nacos 服务地址(默认端口 8848)
|
||||
namespace: public # 命名空间(默认 public,自定义需先在 Nacos 控制台创建)
|
||||
group: DEFAULT_GROUP # 分组(默认 DEFAULT_GROUP)
|
||||
service: order-service # 注册到 Nacos 的服务名(建议和子项目 artifactId 一致)
|
||||
# 配置管理配置(如果引入了 config 依赖才需要)
|
||||
config:
|
||||
server-addr: localhost:8848 # 和 discovery 一致
|
||||
file-extension: yaml
|
||||
namespace: public
|
||||
group: DEFAULT_GROUP
|
||||
config:
|
||||
import: nacos:${spring.application.name}.${spring.cloud.nacos.config.file-extension}?server-addr=${spring.cloud.nacos.config.server-addr}
|
||||
38
order-service/src/test/java/cn/mayiming/AppTest.java
Normal file
38
order-service/src/test/java/cn/mayiming/AppTest.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package cn.mayiming;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Rigourous Test :-)
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user