Jeecg-Cloud學習之路(一)

子非魚220發表於2020-08-30

首先,Spring-Cloud目前是行業的潮流,貌似不會就落後了,筆者為了不脫離大部隊只能深入學習一下了。

其次、跳槽到一家公司,給公司推薦了Jeecg-Boot的開發平臺,那麼為了後面擴充套件為cloud也需要學習了。

廢話不多說,跟著Jeecg團隊出品的視訊學習,發現沒有給原始碼,搞開發這一行眼過千遍不如手過一遍,還是跟著視訊敲一遍,為了方便大家把原始碼貼在下面

Jeecg-CloudB站視訊地址(V2.2.0):https://www.bilibili.com/video/BV1pV411r7xW

nacos下載地址(V1.2.1):https://pan.baidu.com/s/1XtAguW4JNrwuGF-U3SNSeQ     提取碼:z7d9

————————————————————————————————————————————————————————————————————————

首先新建一個Maven專案,jeecg-cloud,父專案的pom.xml檔案如下面程式碼所示:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>jeecg-cloud</groupId>
 8     <artifactId>jeecg-cloud</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10     <modules>
11         <module>jeecg-provider</module>
12         <module>jeecg-config</module>
13         <module>jeecg-gateway</module>
14         <module>jeecg-feign</module>
15     </modules>
16 
17     <packaging>pom</packaging>
18 
19     <dependencyManagement>
20         <dependencies>
21             <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies -->
22             <dependency>
23                 <groupId>org.springframework.cloud</groupId>
24                 <artifactId>spring-cloud-dependencies</artifactId>
25                 <version>Hoxton.SR7</version>
26                 <type>pom</type>
27                 <scope>import</scope>
28             </dependency>
29 
30             <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies -->
31             <dependency>
32                 <groupId>org.springframework.boot</groupId>
33                 <artifactId>spring-boot-dependencies</artifactId>
34                 <version>2.3.3.RELEASE</version>
35                 <type>pom</type>
36                 <scope>import</scope>
37             </dependency>
38 
39 
40             <dependency>
41                 <groupId>com.alibaba.cloud</groupId>
42                 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
43                 <version>2.2.1.RELEASE</version>
44                 <type>pom</type>
45                 <scope>import</scope>
46             </dependency>
47 
48         </dependencies>
49     </dependencyManagement>
50 
51     <build>
52         <plugins>
53             <plugin>
54                 <groupId>org.apache.maven.plugins</groupId>
55                 <artifactId>maven-compiler-plugin</artifactId>
56                 <configuration>
57                     <source>1.8</source>
58                     <target>1.8</target>
59                     <encoding>UTF-8</encoding>
60                 </configuration>
61             </plugin>
62         </plugins>
63 
64         <resources>
65             <resource>
66                 <directory>src/main/resources</directory>
67                 <filtering>true</filtering>
68             </resource>
69         </resources>
70     </build>
71 
72 </project>

之後新建一個jeecg-provider模組(具體步驟參見視訊,我這裡只是把他們的PPT上面的依賴、配置、程式碼寫下來):

pom.xml(jeecg-provider)檔案:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>jeecg-cloud</artifactId>
        <groupId>jeecg-cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jeecg-provider</artifactId>


    <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>

        <!--服務降級、熔斷-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>
</project>

application.yml:

server:
  port: 8082
spring:
  application:
    name: jeecg-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

ProviderApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author zx
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class,args);
    }
}

TestController.java

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Value;
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;

/**
 * @author zx
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Value("${server.port}")
    private String serverPort;

    @RequestMapping("/one")
    public String one(){
        return "my port is: "+serverPort;
    }

    @RequestMapping("/showName/{name}")
    public String showName(@PathVariable("name") String name){

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "your name is : " + name;
    }

    @HystrixCommand(fallbackMethod = "hystrixDefault",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})
    @RequestMapping("/hystrixTest/{name}")
    public String hystrixTest(@PathVariable("name") String name) {
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "我是正常的返回" + name;
    }

    private String hystrixDefault(String  name){
        return "程式超市 " + name;
    }

    @HystrixCommand(fallbackMethod = "netFallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "5000"),})
    @GetMapping("/net/{age}")
    public String net(@PathVariable("age") int age){
        if (age < 18){
            throw new RuntimeException();
        }
        return "正在上網,年齡 " +age;
    }

    public String netFallback(@PathVariable("age") int age){
        return "未成年,不允許進入網咖,年齡 " +age;
    }
}

其他模組請看下一篇,就不要搞得篇幅太長了。

相關文章