Jeecg-Cloud學習之路(一)


首先,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;
    }
}

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM