• Jeecg-Cloud学习之路(二)


    jeecg-config模块:

    pom.xml:

    <?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-config</artifactId>
    
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--SpringCloud alibaba nacos-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
    
            <!--nacos config-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </dependency>
        </dependencies>
    </project>
    View Code

    application.yml

    spring:
      profiles:
        active: dev
    View Code

    bootstrap.yml

    server:
      port: 8083
    spring:
      application:
        name: @artifactId@
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
          config:
            server-addr: 127.0.0.1:8848
            file-extension: yml
            group: DEFAULT_GROUP
            shared-configs: jeecg-common-config.yml
    View Code

    ConfigApplication.java

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    /**
     * @author zx
     */
    @SpringBootApplication
    @EnableDiscoveryClient
    public class ConfigApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigApplication.class,args);
        }
    }
    View Code

    TestController.java

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("config")
    @RefreshScope
    public class TestController {
    
        @Value("${jeecg.edu}")
        private String jeecgEdu;
    
        @RequestMapping("/test")
        public String test(){
            return jeecgEdu;
        }
    }
    View Code

    在TestController里面有个@Value("${jeecg.edu}"),在这地方要记得在nacos里面配置这个参数,不然启动的时候会报错,导致启动不成功

    jeecg-gateway模块:

    pom.xml:

    <?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-gateway</artifactId>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
            </dependency>
    
            <!--SpringCloud alibaba nacos-->
            <!--服务注册时需要用到-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    View Code

    application.yml

    server:
      port: 8084
    spring:
      application:
        name: @artifactId@
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
        gateway:
          routes:
            #- id: 123
              #uri: http://127.0.0.1:8082
              #predicates:
               # - Path=/test/**
            #- id: 456
              #uri: http://127.0.0.1:8083
              #predicates:
               # - Path=/config/**
            - id: jeecg-provider
              uri: lb://jeecg-provider
              predicates:
                - Path=/test/**
            - id: jeecg-config
              uri: lb://jeecg-config
              predicates:
                - Path=/config/**
    View Code

    GatewayApplication.java

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    /**
     * @author zx
     */
    @SpringBootApplication
    @EnableDiscoveryClient
    public class GatewayApplication {
        public static void main(String[] args) {
            SpringApplication.run(GatewayApplication.class,args);
        }
    }
    View Code

    在application.yml网关有两种跳转的方式,第一种注释了,感兴趣的都可以试试。

    jeecg-feign模块:

    pom.xml:

    <?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-feign</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!--SpringCloud alibaba nacos-->
            <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-openfeign</artifactId>
            </dependency>
    
            <!--服务降级、熔断-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
        </dependencies>
    
    </project>
    View Code

    application.yml

    server:
      port: 8085
    spring:
      application:
        name: @artifactId@
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
    feign:
      hystrix:
        enabled: true
      client:
        config:
          jeecg-provider:
            connectTimeout: 4000
            readTimeout: 6000
    View Code

    FeignApplication.java

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.hystrix.EnableHystrix;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    /**
     * @version V1.0
     * @author: zx
     * @date: 2020-08-29 21:07
     * @Description: TODO
     * @modifiedBy:
     */
    
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients
    @EnableHystrix
    public class FeignApplication {
        public static void main(String[] args) {
            SpringApplication.run(FeignApplication.class,args);
        }
    }
    View Code

    FeignController.java

    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;
    import org.test.jeecg.service.FeignService;
    
    import javax.annotation.Resource;
    
    /**
     * @version V1.0
     * @author: zx
     * @date: 2020-08-29 21:16
     * @Description: TODO
     * @modifiedBy:
     */
    @RestController
    @RequestMapping("feign")
    public class FeignController {
    
        @Resource
        FeignService feignService;
    
        @GetMapping("/show/{name}")
        public String showname(@PathVariable("name") String name){
            return  feignService.getName(name);
        }
    }
    View Code

    FeignService.java

    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.test.jeecg.service.impl.FeignServiceImpl;
    
    /**
     * @version V1.0
     * @author: zx
     * @date: 2020-08-29 21:09
     * @Description: TODO
     * @modifiedBy:
     */
    
    @FeignClient(name = "jeecg-provider",path = "/test",fallback = FeignServiceImpl.class)
    public interface FeignService {
    
        @GetMapping("/showName/{name}")
        public String getName(@PathVariable("name") String name);
    }
    View Code

    FeignServiceImpl.java

    import org.springframework.stereotype.Component;
    import org.test.jeecg.service.FeignService;
    
    /**
     * @version V1.0
     * @author: zx
     * @date: 2020-08-30 9:40
     * @Description: TODO
     * @modifiedBy:
     */
    @Component
    public class FeignServiceImpl implements FeignService {
        @Override
        public String getName(String name) {
            return "我是降级方法" + name;
        }
    }
    View Code

    到此结束了视频的代码部分,大家如果有问题,请下方留言,看到的话会回复的。

  • 相关阅读:
    Powershell & SQL
    Powershell运行Invoke-Sqlcmd命令的先决条件
    Powershell实现Telnet Port
    Quest Active Directory
    数据库系统概述(Data Model、DBMS、DBS、RDBS、Structured Query Language)
    Oracle简介
    Downgrading an Exchange 2010 Server(Exchange降级)
    MAPISession(EventID9646-MS-ExchangeIS)
    指定运行Exchange Powershell的Server
    Linux磁盘管理命令(fdisk,mount,umount,mkfs)
  • 原文地址:https://www.cnblogs.com/zfy-220/p/13586430.html
Copyright © 2020-2023  润新知