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; } }
其他模块请看下一篇,就不要搞得篇幅太长了。