├── .gitignore
├── .idea
├── .gitignore
├── dataSources.xml
├── encodings.xml
├── misc.xml
├── uiDesigner.xml
└── vcs.xml
├── README.md
├── api-gateway
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── wilfcode
│ └── Main.java
├── image
├── img.png
├── img_1.png
└── img_2.png
├── pom.xml
├── shop-order-api
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── wilfcode
│ ├── Main.java
│ └── domain
│ └── Order.java
├── shop-order-server
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── wilfcode
│ │ ├── OrderServer.java
│ │ ├── controller
│ │ ├── AnnoController.java
│ │ ├── AuthController.java
│ │ ├── HotSpotController.java
│ │ ├── OrderController.java
│ │ ├── RequestOriginParserDefinition.java
│ │ ├── SentinelController.java
│ │ └── TraceController.java
│ │ ├── dao
│ │ └── OrderDao.java
│ │ ├── feign
│ │ ├── ProductFeignApi.java
│ │ └── fallback
│ │ │ └── ProductFeignClientFallback.java
│ │ └── service
│ │ ├── IOrderService.java
│ │ └── impl
│ │ ├── OrderServiceImpl.java
│ │ └── SentinelServiceImpl.java
│ └── resources
│ └── application.yml
├── shop-product-api
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── wilfcode
│ ├── Main.java
│ └── domain
│ └── Product.java
├── shop-product-server
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── wilfcode
│ │ ├── ProductServer.java
│ │ ├── controller
│ │ └── ProductController.java
│ │ ├── dao
│ │ └── ProductDao.java
│ │ └── service
│ │ ├── IProductService.java
│ │ └── impl
│ │ └── ProductServiceImpl.java
│ └── resources
│ └── application.yml
└── src
└── main
└── java
└── com
└── wilfcode
└── Main.java
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | !.mvn/wrapper/maven-wrapper.jar
3 | !**/src/main/**/target/
4 | !**/src/test/**/target/
5 |
6 | ### IntelliJ IDEA ###
7 | .idea/modules.xml
8 | .idea/jarRepositories.xml
9 | .idea/compiler.xml
10 | .idea/libraries/
11 | *.iws
12 | *.iml
13 | *.ipr
14 |
15 | ### Eclipse ###
16 | .apt_generated
17 | .classpath
18 | .factorypath
19 | .project
20 | .settings
21 | .springBeans
22 | .sts4-cache
23 |
24 | ### NetBeans ###
25 | /nbproject/private/
26 | /nbbuild/
27 | /dist/
28 | /nbdist/
29 | /.nb-gradle/
30 | build/
31 | !**/src/main/**/build/
32 | !**/src/test/**/build/
33 |
34 | ### VS Code ###
35 | .vscode/
36 |
37 | ### Mac OS ###
38 | .DS_Store
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Editor-based HTTP Client requests
5 | /httpRequests/
6 | # Datasource local storage ignored files
7 | /dataSources/
8 | /dataSources.local.xml
9 |
--------------------------------------------------------------------------------
/.idea/dataSources.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | mysql.8
6 | true
7 | com.mysql.cj.jdbc.Driver
8 | jdbc:mysql://localhost:3306/shop-product
9 |
10 |
11 |
12 | $ProjectFileDir$
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/uiDesigner.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | -
6 |
7 |
8 | -
9 |
10 |
11 | -
12 |
13 |
14 | -
15 |
16 |
17 | -
18 |
19 |
20 |
21 |
22 |
23 | -
24 |
25 |
26 |
27 |
28 |
29 | -
30 |
31 |
32 |
33 |
34 |
35 | -
36 |
37 |
38 |
39 |
40 |
41 | -
42 |
43 |
44 |
45 |
46 | -
47 |
48 |
49 |
50 |
51 | -
52 |
53 |
54 |
55 |
56 | -
57 |
58 |
59 |
60 |
61 | -
62 |
63 |
64 |
65 |
66 | -
67 |
68 |
69 |
70 |
71 | -
72 |
73 |
74 | -
75 |
76 |
77 |
78 |
79 | -
80 |
81 |
82 |
83 |
84 | -
85 |
86 |
87 |
88 |
89 | -
90 |
91 |
92 |
93 |
94 | -
95 |
96 |
97 |
98 |
99 | -
100 |
101 |
102 | -
103 |
104 |
105 | -
106 |
107 |
108 | -
109 |
110 |
111 | -
112 |
113 |
114 |
115 |
116 | -
117 |
118 |
119 | -
120 |
121 |
122 |
123 |
124 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Spring Cloud Sample Project
2 |
3 | ## Introduction
4 |
5 | This project is a sample Spring Cloud application that implements a microservices architecture with services like product and order management, and an API gateway for service routing.
6 |
7 | ## Prerequisites
8 |
9 | - Java JDK 1.8
10 | - Maven
11 | - MySQL Server
12 | - Nacos Service Discovery
13 | - Alibaba Sentinel
14 | - Jmeter
15 |
16 | Ensure Java and Maven are installed on your system and the `JAVA_HOME` environment variable is set.
17 |
18 | ## Setup
19 |
20 | 1. Clone the repository to your local machine.
21 | 2. Create MySQL databases `shop_product` and `shop_order`.
22 | 3. Start the Nacos Service Discovery and Configuration server.
23 | 4.Start the Alibaba sentinel and Jemeter to pull Http request.
24 |
25 | ## Services
26 |
27 | The project includes the following microservices:
28 |
29 | - **shop-product-api**: The API definition for the product service.
30 | - **shop-product-server**: Implementation of the product service.
31 | - **shop-order-api**: The API definition for the order service.
32 | - **shop-order-server**: Implementation of the order service.
33 | - **api-gateway**: The gateway service that routes API requests to the appropriate microservice.\
34 |
35 | ## Configuration
36 |
37 | Each service has its own `application.yml` or `application.properties` file in the `src/main/resources` directory where you can set up the datasource, server port, and other configurations.
38 |
39 | ### Common Properties
40 |
41 | - `server.port`: The port on which the service will run.
42 | - `spring.application.name`: The name of the application in the service registry.
43 | - `spring.datasource`: The datasource configuration.
44 | - `spring.jpa`: The JPA/Hibernate configuration.
45 | - `spring.cloud.nacos.discovery.server-addr`: The address of the Nacos server.
46 |
47 | ## Starting Services
48 |
49 | Each microservice can be started with the Spring Boot Maven plugin. Below are the commands to start each service:
50 |
51 | ### API Gateway
52 |
53 | ```bash
54 | cd api-gateway
55 | mvn spring-boot:run
56 | ```
57 |
58 |
59 | ## Product Service
60 |
61 | ### API
62 |
63 | To start the `shop-product-api`, run the following command:
64 |
65 | ```bash
66 | cd shop-product-api
67 | mvn spring-boot:run
68 |
69 | ```
70 | ### Server
71 | To start the shop-product-server, use this command:
72 |
73 | ```bash
74 | cd shop-product-server
75 | mvn spring-boot:run
76 | ```
77 | ## Order Service
78 |
79 | ### API
80 |
81 | Start the shop-order-api with:
82 | ```bash
83 | cd shop-order-api
84 | mvn spring-boot:run
85 |
86 | ```
87 | ### Server
88 | To launch the shop-order-server, execute:
89 |
90 | ```bash
91 | cd shop-order-server
92 | mvn spring-boot:run
93 | ```
94 |
95 |
96 | ## Nacos Setup
97 |
98 | ### Linux/Mac
99 |
100 | ```bash
101 | sh startup.sh -m standalone
102 |
103 | ```
104 |
105 | ### Windows
106 | ```bash
107 | startup.cmd -m standalone
108 |
109 | ```
110 |
111 | ## Starting the Alibaba Sentinel Dashboard
112 |
113 | Alibaba Sentinel provides a dashboard for monitoring and managing rules in a graphical interface.
114 |
115 | To start the Sentinel dashboard, execute the following command in the directory where Sentinel is located
116 |
117 | ```bash
118 | java -Dserver.port=8080 -jar sentinel-dashboard-1.8.0.jar
119 |
120 | ```
121 | ## Using JMeter for Performance Testing
122 |
123 | Apache JMeter is an open-source load testing tool. You can use it to test the performance and stability of your services under different load conditions.
124 |
125 | ### Setting Up JMeter
126 |
127 | - Download and install [Apache JMeter](https://jmeter.apache.org/download_jmeter.cgi).
128 | - Open JMeter GUI by running the `jmeter` command in the `bin` directory of JMeter.
129 | - Configure your test plan by adding Thread Groups, HTTP Request samplers, listeners, etc.
130 |
131 | ### Running a Simple Test
132 |
133 | 1. In JMeter, create a new Test Plan.
134 | 2. Add a Thread Group to define the number of users and ramp-up period.
135 | 3. Add an HTTP Request sampler to set up requests to your services.
136 | 4. Add a Listener (e.g., View Results in Table) to view the results.
137 | 5. Start the test by clicking the 'Start' button (green play icon).
138 |
139 | ### Integrating JMeter with Sentinel
140 |
141 | To integrate JMeter with Sentinel, you need to:
142 |
143 | 1. Add the Sentinel dependency to your project's `pom.xml`.
144 | 2. Configure Sentinel resource rules in your application.
145 | 3. Run your JMeter tests and observe the results on the Sentinel dashboard.
146 |
--------------------------------------------------------------------------------
/api-gateway/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.wilfcode
8 | spring-cloud-parent
9 | 1.0-SNAPSHOT
10 |
11 |
12 | api-gateway
13 |
14 |
15 | 8
16 | 8
17 | UTF-8
18 |
19 |
20 |
21 |
22 | org.springframework.cloud
23 | spring-cloud-starter-gateway
24 |
25 |
26 |
27 | com.alibaba.cloud
28 | spring-cloud-starter-alibaba-nacos-discovery
29 |
30 |
31 |
32 | org.projectlombok
33 | lombok
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/api-gateway/src/main/java/com/wilfcode/Main.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode;
2 |
3 | // Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
4 | // then press Enter. You can now see whitespace characters in your code.
5 | public class Main {
6 | public static void main(String[] args) {
7 | // Press Alt+Enter with your caret at the highlighted text to see how
8 | // IntelliJ IDEA suggests fixing it.
9 | System.out.printf("Hello and welcome!");
10 |
11 | // Press Shift+F10 or click the green arrow button in the gutter to run the code.
12 | for (int i = 1; i <= 5; i++) {
13 |
14 | // Press Shift+F9 to start debugging your code. We have set one breakpoint
15 | // for you, but you can always add more by pressing Ctrl+F8.
16 | System.out.println("i = " + i);
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/image/img.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chenrui-hu1/springcloud1-sample/dd1b6fc10178bb66a8370474a2fc8302b50b9eaf/image/img.png
--------------------------------------------------------------------------------
/image/img_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chenrui-hu1/springcloud1-sample/dd1b6fc10178bb66a8370474a2fc8302b50b9eaf/image/img_1.png
--------------------------------------------------------------------------------
/image/img_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chenrui-hu1/springcloud1-sample/dd1b6fc10178bb66a8370474a2fc8302b50b9eaf/image/img_2.png
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.wilfcode
8 | spring-cloud-parent
9 | 1.0-SNAPSHOT
10 | pom
11 |
12 | shop-product-api
13 | shop-product-server
14 | shop-order-api
15 | shop-order-server
16 | api-gateway
17 |
18 |
19 |
20 | org.springframework.boot
21 | spring-boot-starter-parent
22 | 2.3.3.RELEASE
23 |
24 |
25 |
26 | Hoxton.SR8
27 | 2.2.3.RELEASE
28 | UTF-8
29 | UTF-8
30 |
31 |
32 |
33 |
34 |
35 | org.springframework.cloud
36 | spring-cloud-dependencies
37 | ${spring-cloud-version}
38 | pom
39 | import
40 |
41 |
42 | com.alibaba.cloud
43 | spring-cloud-alibaba-dependencies
44 | ${spring-cloud-alibaba.version}
45 | pom
46 | import
47 |
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/shop-order-api/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.wilfcode
8 | spring-cloud-parent
9 | 1.0-SNAPSHOT
10 |
11 |
12 | shop-order-api
13 |
14 |
15 | 8
16 | 8
17 | UTF-8
18 |
19 |
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter-data-jpa
24 |
25 |
26 |
27 | org.projectlombok
28 | lombok
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/shop-order-api/src/main/java/com/wilfcode/Main.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode;
2 |
3 | // Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
4 | // then press Enter. You can now see whitespace characters in your code.
5 | public class Main {
6 | public static void main(String[] args) {
7 | // Press Alt+Enter with your caret at the highlighted text to see how
8 | // IntelliJ IDEA suggests fixing it.
9 | System.out.printf("Hello and welcome!");
10 |
11 | // Press Shift+F10 or click the green arrow button in the gutter to run the code.
12 | for (int i = 1; i <= 5; i++) {
13 |
14 | // Press Shift+F9 to start debugging your code. We have set one breakpoint
15 | // for you, but you can always add more by pressing Ctrl+F8.
16 | System.out.println("i = " + i);
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/shop-order-api/src/main/java/com/wilfcode/domain/Order.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.domain;
2 |
3 | import lombok.Data;
4 |
5 | import javax.persistence.Entity;
6 | import javax.persistence.GeneratedValue;
7 | import javax.persistence.GenerationType;
8 | import javax.persistence.Id;
9 |
10 | @Entity(name = "t_shop_order")
11 | @Data
12 | public class Order {
13 | @Id
14 | @GeneratedValue(strategy = GenerationType.IDENTITY)
15 | private Long oid;
16 |
17 | private Long uid;
18 |
19 | private String username;
20 |
21 | private Long pid;
22 | private String pname;
23 | private Double pprice;
24 |
25 | private Integer number;
26 | }
27 |
--------------------------------------------------------------------------------
/shop-order-server/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.wilfcode
8 | spring-cloud-parent
9 | 1.0-SNAPSHOT
10 |
11 |
12 | shop-order-server
13 |
14 |
15 | 8
16 | 8
17 | UTF-8
18 |
19 |
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-starter-web
26 |
27 |
28 |
29 | mysql
30 | mysql-connector-java
31 |
32 |
33 |
34 | com.alibaba
35 | fastjson
36 | 1.2.56
37 |
38 |
39 |
40 | com.wilfcode
41 | shop-product-api
42 | 1.0-SNAPSHOT
43 |
44 |
45 |
46 |
47 | com.wilfcode
48 | shop-order-api
49 | ${project.version}
50 |
51 |
52 |
53 | com.alibaba.cloud
54 | spring-cloud-starter-alibaba-nacos-discovery
55 |
56 |
57 |
58 | org.springframework.cloud
59 | spring-cloud-starter-openfeign
60 |
61 |
62 |
63 |
64 | com.alibaba.cloud
65 | spring-cloud-starter-alibaba-sentinel
66 |
67 |
68 |
69 |
70 |
71 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/OrderServer.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6 | import org.springframework.cloud.openfeign.EnableFeignClients;
7 |
8 | @SpringBootApplication
9 | //@EntityScan("com.wilfcode.domain")
10 | @EnableDiscoveryClient
11 | @EnableFeignClients
12 | public class OrderServer {
13 | public static void main(String[] args) {
14 | SpringApplication.run(OrderServer.class, args);
15 | }
16 |
17 |
18 | // @Bean
19 | // @LoadBalanced
20 | // public RestTemplate restTemplate() {
21 | // return new RestTemplate();
22 | // }
23 | }
24 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/controller/AnnoController.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.controller;
2 |
3 |
4 | import com.alibaba.csp.sentinel.annotation.SentinelResource;
5 | import lombok.extern.slf4j.Slf4j;
6 | import org.springframework.web.bind.annotation.RequestMapping;
7 | import org.springframework.web.bind.annotation.RestController;
8 |
9 | @RestController
10 | @Slf4j
11 | public class AnnoController {
12 |
13 | @RequestMapping("/anno1")
14 | @SentinelResource(value = "anno1", blockHandler = "blockHandler", fallback = "fallback")
15 |
16 | public String anno1(String serviceName) {
17 | log.info("anno1 + serviceName={}", serviceName);
18 | if("wilfcoder".equals(serviceName)) {
19 | throw new RuntimeException("wilfcoder");
20 | }
21 | return serviceName;
22 | }
23 |
24 | public String blockHandler(String serviceName) {
25 | log.error("blockHandler + serviceName={}", serviceName);
26 | return "blockHandler";
27 | }
28 |
29 | public String fallback(String serviceName) {
30 | log.error("fallback + serviceName={}", serviceName);
31 | return "fallback";
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/controller/AuthController.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.controller;
2 |
3 |
4 | import lombok.extern.slf4j.Slf4j;
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 | import org.springframework.web.bind.annotation.RestController;
7 |
8 | @RestController
9 | @Slf4j
10 | public class AuthController {
11 |
12 | @RequestMapping("/auth1")
13 |
14 | public String auth1(String serviceName) {
15 | log.info("auth1 + serviceName={}", serviceName);
16 | return "auth1";
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/controller/HotSpotController.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.controller;
2 |
3 | import com.alibaba.csp.sentinel.annotation.SentinelResource;
4 | import lombok.extern.slf4j.Slf4j;
5 | import org.springframework.web.bind.annotation.RequestMapping;
6 | import org.springframework.web.bind.annotation.RestController;
7 |
8 | @RestController
9 | @Slf4j
10 |
11 | public class HotSpotController {
12 | @RequestMapping("/hotSpot1")
13 | @SentinelResource(value = "hotSpot1")
14 | public String hotSpot1(Long productId) {
15 | log.info("hotSpot1+productId={}", productId);
16 | return "hotSpot1";
17 | }
18 | }
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/controller/OrderController.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.controller;
2 |
3 | import com.wilfcode.domain.Order;
4 | import com.wilfcode.service.IOrderService;
5 | import lombok.extern.slf4j.Slf4j;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RestController;
9 |
10 | @RestController
11 | @Slf4j
12 |
13 | public class OrderController {
14 | @Autowired
15 | private IOrderService orderService;
16 | @RequestMapping("/save")
17 | public Order order(Long pid, Long uid) {
18 | log.info("create order start and pid={}, number={}", pid, uid);
19 | Order order = orderService.createOrder(pid, uid);
20 | log.info("create order end and order={}", order);
21 | return order;
22 | }
23 |
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/controller/RequestOriginParserDefinition.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.controller;
2 |
3 | import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
4 |
5 | import javax.servlet.http.HttpServletRequest;
6 |
7 | public class RequestOriginParserDefinition implements RequestOriginParser {
8 |
9 | @Override
10 | public String parseOrigin(HttpServletRequest request) {
11 | String serviceName = request.getParameter("serviceName");
12 | return serviceName;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/controller/SentinelController.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.controller;
2 |
3 |
4 | import org.springframework.web.bind.annotation.RequestMapping;
5 | import org.springframework.web.bind.annotation.RestController;
6 |
7 | import java.util.concurrent.TimeUnit;
8 |
9 | @RestController
10 |
11 | public class SentinelController {
12 |
13 | @RequestMapping("/sentinel1")
14 | public String sentinel1() {
15 | try {
16 | TimeUnit.SECONDS.sleep(1);
17 | } catch (InterruptedException e) {
18 | throw new RuntimeException(e);
19 | }
20 | return "sentinel1";
21 | }
22 |
23 | @RequestMapping("/sentinel2")
24 | public String sentinel2() {
25 | return "sentinel2";
26 | }
27 |
28 | @RequestMapping("/sentinel3")
29 | public String sentinel3() {
30 | return "sentinel3";
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/controller/TraceController.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.controller;
2 |
3 |
4 | import com.wilfcode.service.impl.SentinelServiceImpl;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.web.bind.annotation.RequestMapping;
7 | import org.springframework.web.bind.annotation.RestController;
8 |
9 | @RestController
10 |
11 | public class TraceController {
12 | @Autowired
13 | private SentinelServiceImpl sentinelService;
14 |
15 | @RequestMapping("/trace1")
16 | public String trace1() {
17 | sentinelService.tranceService();
18 | return "trace1";
19 | }
20 |
21 | @RequestMapping("/trace2")
22 | public String trace2() {
23 | sentinelService.tranceService();
24 | return "trace2";
25 | }
26 |
27 |
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/dao/OrderDao.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.dao;
2 |
3 | import com.wilfcode.domain.Order;
4 | import org.springframework.data.jpa.repository.JpaRepository;
5 |
6 | public interface OrderDao extends JpaRepository {
7 | }
8 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/feign/ProductFeignApi.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.feign;
2 |
3 | import com.wilfcode.domain.Product;
4 | import com.wilfcode.feign.fallback.ProductFeignClientFallback;
5 | import org.springframework.cloud.openfeign.FeignClient;
6 | import org.springframework.web.bind.annotation.PathVariable;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 |
9 | @FeignClient(name = "product-service", fallback = ProductFeignClientFallback.class)
10 |
11 | public interface ProductFeignApi {
12 | @RequestMapping("/product/{pid}")
13 | Product findById(@PathVariable("pid") Long pid);
14 | }
15 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/feign/fallback/ProductFeignClientFallback.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.feign.fallback;
2 |
3 | import com.wilfcode.domain.Product;
4 | import com.wilfcode.feign.ProductFeignApi;
5 |
6 | public class ProductFeignClientFallback implements ProductFeignApi {
7 | @Override
8 | public Product findById(Long pid) {
9 | System.out.println("fallback");
10 | Product product = new Product();
11 | return product;
12 |
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/service/IOrderService.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.service;
2 |
3 | import com.wilfcode.domain.Order;
4 |
5 | public interface IOrderService {
6 | Order createOrder(Long pid, Long number);
7 | }
8 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/service/impl/OrderServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.service.impl;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.wilfcode.dao.OrderDao;
5 | import com.wilfcode.domain.Order;
6 | import com.wilfcode.domain.Product;
7 | import com.wilfcode.feign.ProductFeignApi;
8 | import com.wilfcode.service.IOrderService;
9 | import lombok.extern.slf4j.Slf4j;
10 | import org.springframework.beans.factory.annotation.Autowired;
11 | import org.springframework.cloud.client.discovery.DiscoveryClient;
12 | import org.springframework.stereotype.Service;
13 |
14 | @Slf4j
15 | @Service
16 |
17 | public class OrderServiceImpl implements IOrderService {
18 | @Autowired
19 | private OrderDao orderDao;
20 | // @Autowired
21 | // private RestTemplate restTemplate;
22 |
23 | @Autowired
24 | private DiscoveryClient discoveryClient;
25 |
26 | @Autowired
27 | private ProductFeignApi productFeignApi;
28 | @Override
29 | public Order createOrder(Long productId, Long userId) {
30 | log.info("create order start and pid={}, number={}", productId, userId);
31 |
32 | // String url = "http://localhost:8081/product/" + productId;
33 |
34 | // String url = "http://product-service/product/" + productId;
35 |
36 |
37 | // log.info("url={}", url);
38 |
39 |
40 |
41 |
42 | // Product product = restTemplate.getForObject(url, Product.class);
43 | Product product = productFeignApi.findById(productId);
44 | log.info(("class={}" + product.getClass()));
45 | log.info("product={}", JSON.toJSONString(product));
46 |
47 |
48 |
49 | Order order = new Order();
50 | order.setUid(userId);
51 | order.setUsername("wilfcode");
52 |
53 | order.setPid(productId);
54 | order.setPname(product.getPname());
55 | order.setPprice(product.getPprice());
56 | order.setNumber(1);
57 | orderDao.save(order);
58 |
59 | log.info("create order end and order={}", JSON.toJSONString(order));
60 |
61 | return order;
62 |
63 |
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/java/com/wilfcode/service/impl/SentinelServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.service.impl;
2 |
3 |
4 | import com.alibaba.csp.sentinel.annotation.SentinelResource;
5 | import org.springframework.stereotype.Service;
6 |
7 | @Service
8 |
9 | public class SentinelServiceImpl {
10 |
11 | @SentinelResource(value = "tranceService")
12 | public String tranceService() {
13 | return "tranceService";
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/shop-order-server/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8091
3 |
4 | spring:
5 | application:
6 | name: order-service
7 | datasource:
8 | driver-class-name: com.mysql.cj.jdbc.Driver
9 | url: jdbc:mysql://localhost:3306/shop_order?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
10 | username: root
11 | password: root
12 |
13 |
14 | jpa:
15 | properties:
16 | hibernate:
17 | hbm2ddl:
18 | auto: update
19 | dialect: org.hibernate.dialect.MySQL5InnoDBDialect
20 |
21 |
22 |
23 |
24 | cloud:
25 | nacos:
26 | discovery:
27 | server-addr: localhost:8848
28 |
29 |
30 | sentinel:
31 | transport:
32 | port: 9999
33 | dashboard: localhost:8080
34 | web-context-unify: false
35 |
36 |
37 |
38 |
39 |
40 | product-service:
41 |
42 | ribbon:
43 | NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/shop-product-api/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.wilfcode
8 | spring-cloud-parent
9 | 1.0-SNAPSHOT
10 |
11 |
12 | shop-product-api
13 |
14 |
15 |
16 | UTF-8
17 |
18 |
19 |
20 |
21 | org.springframework.boot
22 | spring-boot-starter-data-jpa
23 |
24 |
25 |
26 | org.projectlombok
27 | lombok
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/shop-product-api/src/main/java/com/wilfcode/Main.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode;
2 |
3 | // Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
4 | // then press Enter. You can now see whitespace characters in your code.
5 | public class Main {
6 | public static void main(String[] args) {
7 | // Press Alt+Enter with your caret at the highlighted text to see how
8 | // IntelliJ IDEA suggests fixing it.
9 | System.out.printf("Hello and welcome!");
10 |
11 | // Press Shift+F10 or click the green arrow button in the gutter to run the code.
12 | for (int i = 1; i <= 5; i++) {
13 |
14 | // Press Shift+F9 to start debugging your code. We have set one breakpoint
15 | // for you, but you can always add more by pressing Ctrl+F8.
16 | System.out.println("i = " + i);
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------
/shop-product-api/src/main/java/com/wilfcode/domain/Product.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.domain;
2 |
3 |
4 | import lombok.Data;
5 |
6 | import javax.persistence.Entity;
7 | import javax.persistence.GeneratedValue;
8 | import javax.persistence.GenerationType;
9 | import javax.persistence.Id;
10 |
11 | @Entity(name = "t_shop_product")
12 | @Data
13 | public class Product {
14 | @Id
15 | @GeneratedValue(strategy = GenerationType.IDENTITY)
16 | private Long pid;
17 | private String pname;
18 | private Double pprice;
19 | private Integer stock;
20 | }
21 |
--------------------------------------------------------------------------------
/shop-product-server/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.wilfcode
8 | spring-cloud-parent
9 | 1.0-SNAPSHOT
10 |
11 |
12 | shop-product-server
13 |
14 |
15 | UTF-8
16 |
17 |
18 |
19 |
20 |
21 | com.wilfcode
22 | shop-product-api
23 | ${project.version}
24 |
25 |
26 |
27 | org.springframework.boot
28 | spring-boot-starter-web
29 |
30 |
31 |
32 | mysql
33 | mysql-connector-java
34 |
35 |
36 |
37 | com.alibaba
38 | fastjson
39 | 1.2.56
40 |
41 |
42 |
43 | com.alibaba.cloud
44 | spring-cloud-starter-alibaba-nacos-discovery
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/shop-product-server/src/main/java/com/wilfcode/ProductServer.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode;
2 |
3 |
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7 |
8 | @SpringBootApplication
9 | @EnableDiscoveryClient
10 | public class ProductServer {
11 | public static void main(String[] args) {
12 | SpringApplication.run(ProductServer.class, args);
13 | }
14 | }
--------------------------------------------------------------------------------
/shop-product-server/src/main/java/com/wilfcode/controller/ProductController.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.controller;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.wilfcode.domain.Product;
5 | import com.wilfcode.service.IProductService;
6 | import lombok.extern.slf4j.Slf4j;
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.beans.factory.annotation.Value;
9 | import org.springframework.web.bind.annotation.PathVariable;
10 | import org.springframework.web.bind.annotation.RequestMapping;
11 | import org.springframework.web.bind.annotation.RestController;
12 |
13 | @RestController
14 | @Slf4j
15 |
16 | public class ProductController {
17 |
18 | @Autowired
19 | private IProductService productService;
20 |
21 | @Value("${server.port}")
22 | private String port;
23 |
24 |
25 | @RequestMapping("/product/{pid}")
26 | public Product findById(@PathVariable("pid") Long pid) {
27 | log.info("product-service findById() is called");
28 | Product product = productService.findById(pid);
29 | product.setPname(product.getPname() + " data from port=" + port);
30 | log.info("product-service findById() is completed, content is :{}", JSON.toJSONString(product));
31 | return product;
32 |
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/shop-product-server/src/main/java/com/wilfcode/dao/ProductDao.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.dao;
2 |
3 |
4 | import com.wilfcode.domain.Product;
5 | import org.springframework.data.jpa.repository.JpaRepository;
6 |
7 | public interface ProductDao extends JpaRepository {
8 | }
9 |
--------------------------------------------------------------------------------
/shop-product-server/src/main/java/com/wilfcode/service/IProductService.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.service;
2 |
3 | import com.wilfcode.domain.Product;
4 |
5 | public interface IProductService {
6 | Product findById(Long pid);
7 | }
8 |
--------------------------------------------------------------------------------
/shop-product-server/src/main/java/com/wilfcode/service/impl/ProductServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode.service.impl;
2 |
3 | import com.wilfcode.dao.ProductDao;
4 | import com.wilfcode.domain.Product;
5 | import com.wilfcode.service.IProductService;
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.stereotype.Service;
8 |
9 | @Service
10 |
11 | public class ProductServiceImpl implements IProductService {
12 | @Autowired
13 | private ProductDao productDao;
14 | @Override
15 | public Product findById(Long pid) {
16 | return productDao.findById(pid).get();
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/shop-product-server/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8081
3 |
4 | Spring:
5 | application:
6 | name: product-service
7 | datasource:
8 | driver-class-name: com.mysql.cj.jdbc.Driver
9 | url: jdbc:mysql://localhost:3306/shop-product?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8
10 | username: root
11 | password: root
12 |
13 | jpa:
14 | properties:
15 | hibernate:
16 | hbm2ddl:
17 | auto: update
18 | dialect: org.hibernate.dialect.MySQL5InnoDBDialect
19 |
20 |
21 |
22 | cloud:
23 | nacos:
24 | discovery:
25 | server-addr: localhost:8848
26 |
27 |
--------------------------------------------------------------------------------
/src/main/java/com/wilfcode/Main.java:
--------------------------------------------------------------------------------
1 | package com.wilfcode;
2 |
3 | // Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
4 | // then press Enter. You can now see whitespace characters in your code.
5 | public class Main {
6 | public static void main(String[] args) {
7 | // Press Alt+Enter with your caret at the highlighted text to see how
8 | // IntelliJ IDEA suggests fixing it.
9 | System.out.printf("Hello and welcome!");
10 |
11 | // Press Shift+F10 or click the green arrow button in the gutter to run the code.
12 | for (int i = 1; i <= 5; i++) {
13 |
14 | // Press Shift+F9 to start debugging your code. We have set one breakpoint
15 | // for you, but you can always add more by pressing Ctrl+F8.
16 | System.out.println("i = " + i);
17 | }
18 | }
19 | }
--------------------------------------------------------------------------------