Nacos+Springcloud+mybatis-plus+oracle的整合
1、项目结构
2、父类的依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ycz</groupId>
<artifactId>Nacos-study</artifactId>
<packaging>pom</packaging>
<version>${revision}</version>
<name>Nacos-study</name>
<description>Nacos-study</description>
<properties>
<!-- 指定项目版本 -->
<revision>1.0-SNAPSHOT</revision>
<!-- 指定SpringBoot版本 -->
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<!-- 指定SpringCloud版本 -->
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<!-- 指定SpringCloudAlibaba版本 -->
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
<!-- 指定JDK版本 -->
<java-jdk.version>1.8</java-jdk.version>
<!--指定pageHelper-->
<pageHelper.version>5.3.0</pageHelper.version>
<!--ojdbc8-->
<ojdbc8.version>21.8.0.0</ojdbc8.version>
<!--orai18n-->
<orai18n.version>19.7.0.0</orai18n.version>
<!--lombok-->
<lombok.version>1.18.24</lombok.version>
</properties>
<modules>
<module>nacos-config</module>
<module>nacos-provider</module>
<module>nacos-consumer</module>
<module>nacos-feign-api</module>
<module>nacos-geteway</module>
<module>nacos-commons</module>
</modules>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- 声明依赖 springBoot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 声明依赖 springCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 声明依赖 springCloudAlibaba -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pageHelper.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>${ojdbc8.version}</version>
</dependency>
<dependency>
<groupId>com.oracle.database.nls</groupId>
<artifactId>orai18n</artifactId>
<version>${orai18n.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>dev</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
<profile>
<id>home</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>home</profileActive>
</properties>
</profile>
</profiles>
</project>
3、nacos-config
(1)新建一个配置
(2)依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ycz</groupId>
<artifactId>Nacos-study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>nacos-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!-- 父项目中我们已经指定了对应的版本, 这里直接使用就行, 不用在引用版本 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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-config</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(3)bootstrap.yml
spring:
profiles:
active: @profileActive@
(4)application-dev.yml
server:
port: 7001
spring:
application:
name: nacos-config
cloud:
nacos:
config:
server-addr: localhost:8848
(5)controller测试
package com.ycz.Controller;
import com.ycz.config.CommonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private CommonConfig config;
@GetMapping("test")
public String test(){
return config.getUsername() + config.getPassword();
}
}
package com.ycz.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@RefreshScope
@Component
public class CommonConfig {
@Value(value = "${oracle.username}")
private String username;
@Value(value = "${oracle.password}")
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.ycz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NacosConfigApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class, args);
}
}
4、nacos-commons
(1)依赖
<?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>Nacos-study</artifactId>
<groupId>com.ycz</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.ycz</groupId>
<artifactId>nacos-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-commons</name>
<description>nacos-commons</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
(2)代码
实体类
@Data
public class HotApps {
private Integer appOid;
private String appName;
private String serviceRyCategoryOid;
private String appSubName;
private String businessTag;
}
请求param
@Data
public class HotAppsParams extends BaseParams{
private Integer appOid;
private String appName;
private String serviceRyCategoryOid;
private String appSubName;
private String businessTag;
}
5、nacos-provider
(1)依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ycz</groupId>
<artifactId>Nacos-study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>nacos-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!-- 引入commons -->
<dependency>
<groupId>com.ycz</groupId>
<artifactId>nacos-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 父项目中我们已经指定了对应的版本, 这里直接使用就行, 不用在引用版本 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.nls</groupId>
<artifactId>orai18n</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
(2)bootstrap.yml
spring:
profiles:
active: @profileActive@
(3)application-dev.yml
server:
port: 8001
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
# config:
# server-addr: localhost:8848
# file-extension: yaml
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@ip:1521:XE
username: admin
password: admin
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*Mapper.xml
configuration:
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
(3)配置
package com.ycz.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author alex
* @date 2023/01/13 1:44
* @Version 1.0
*/
@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
// 最新版
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 数据库的配置DbType.ORACLE 根据自己的数据库选择
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
return interceptor;
}
}
mapper
package com.ycz.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ycz.entity.HotApps;
import org.springframework.stereotype.Repository;
/**
* HotAppMapper
* <p>
* Description
* <p>
* Creation Time: 2023/1/17 下午3:45
*
* @author yang chao zhen
*/
@Repository
public interface HotAppsMapper extends BaseMapper<HotApps> {
}
(4)service
package com.ycz.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ycz.entity.HotApps;
import com.ycz.params.HotAppsParams;
/**
* HotAppService
* <p>
* Description
* <p>
* Creation Time: 2023/1/17 下午3:46
*
* @author yang chao zhen
*/
public interface HotAppService {
IPage<HotApps> getHotApps(HotAppsParams params);
Object insert(HotApps hotApps);
}
(5)serviceimpl
package com.ycz.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycz.entity.HotApps;
import com.ycz.mapper.HotAppsMapper;
import com.ycz.params.HotAppsParams;
import com.ycz.service.HotAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* HotAppService
* <p>
* Description
* <p>
* Creation Time: 2023/1/17 下午3:46
*
* @author yang chao zhen
*/
@Service
public class HotAppServiceImpl implements HotAppService {
@Autowired
private HotAppsMapper mapper;
@Override
public IPage<HotApps> getHotApps(HotAppsParams params) {
QueryWrapper<HotApps> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(params.getAppOid()!= null , "APP_OID" ,params.getAppOid());
return mapper.selectPage(new Page<HotApps>(params.getCurrent(), params.getSize()), queryWrapper);
}
@Override
public Object insert(HotApps hotApps) {
System.out.println(hotApps);
return mapper.insert(hotApps);
}
}
(6)controller
package com.ycz.controller;
import com.ycz.entity.HotApps;
import com.ycz.params.HotAppsParams;
import com.ycz.service.HotAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* HotAppsController
* <p>
* Description
* <p>
* Creation Time: 2023/1/17 下午3:51
*
* @author yang chao zhen
*/
@RequestMapping("/hotApps")
@RestController
public class HotAppsController {
@Autowired
private HotAppService service;
@GetMapping("/page")
public Object getPage(HotAppsParams params){
return service.getHotApps(params);
}
@PostMapping("/insert")
public Object insert(HotApps hotApps){
return service.insert(hotApps);
}
}
6、naoos-feign-api
(1)依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ycz</groupId>
<artifactId>Nacos-study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.ycz</groupId>
<artifactId>nacos-feign-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-feign-api</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 引入commons -->
<dependency>
<groupId>com.ycz</groupId>
<artifactId>nacos-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2)HotAppService
package com.ycz.service;
import com.ycz.params.HotAppsParams;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
/**
* HotAppsService
* <p>
* Description
* <p>
* Creation Time: 2023/1/17 下午3:52
*
* @author yang chao zhen
*/
@FeignClient(name = "${nacos.product.name}" , contextId = "hotService")
public interface HotAppsService {
@GetMapping(path = "/hotApps/page/")
Object getPage(@SpringQueryMap HotAppsParams params);
@PostMapping(path = "/hotApps/insert/")
Object insert(@SpringQueryMap HotAppsParams params);
}
7、nacos-consumer
(1)依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ycz</groupId>
<artifactId>Nacos-study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.ycz</groupId>
<artifactId>nacos-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-consumer</name>
<description>nacos-consumer</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 引入feign -->
<dependency>
<groupId>com.ycz</groupId>
<artifactId>nacos-feign-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入commons -->
<dependency>
<groupId>com.ycz</groupId>
<artifactId>nacos-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 父项目中我们已经指定了对应的版本, 这里直接使用就行, 不用在引用版本 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
(2)bootstrap.yml
spring:
profiles:
active: @profileActive@
(3)application-dev.yml
server:
port: 5001
spring:
application:
name: nacos-comsumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@127.0.0.1:1521:XE # 假的,为了前面commons里面的@TableName错误
username: admin
password: admin
nacos:
product:
name: nacos-provider
(4)配置
package com.ycz.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* RestTemplateConfig
* <p>
* Description
* <p>
* Creation Time: 2023/1/16 下午12:24
*
* @author yang chao zhen
*/
@Configuration
public class RestTemplateConfig {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
(5)controller
package com.ycz.controller;
import com.ycz.entity.HotApps;
import com.ycz.params.HotAppsParams;
import com.ycz.service.HotAppsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class TestController {
@Autowired
private HotAppsService hotAppsService;
@RequestMapping(value = "/hotApps/page" , method = RequestMethod.GET)
public Object getPage(HotAppsParams params){
return hotAppsService.getPage(params);
}
@RequestMapping(value = "/hotApps/insert" , method = RequestMethod.POST)
public Object insert(@RequestBody HotAppsParams params){
HotApps hotApps = new HotApps();
BeanUtils.copyProperties(params , hotApps);
return hotAppsService.insert(params);
}
}
8、nacos-geteway
(1)依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ycz</groupId>
<artifactId>Nacos-study</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.ycz</groupId>
<artifactId>nacos-geteway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-gateway</name>
<description>nacos-geteway</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 服务注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(2)bootstrap.yml
spring:
profiles:
active: @profileActive@
(3)application-dev.yml
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848
enabled: false
gateway:
routes:
- id: user
uri: lb://nacos-comsumer
predicates:
- Path=/gateWay/** #以city开头的请求都负载到consumer服务
filters:
- RewritePath=/gateWay/(?<segment>.*), /$\{segment} #过滤掉url里的user,例如http://ip:port/city/getCity -> http://ip:port/getCity
application:
name: nacos-gateway
server:
port: 8000
(4)启动
package com.ycz.nacosgeteway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NacosGetewayApplication {
public static void main(String[] args) {
SpringApplication.run(NacosGetewayApplication.class, args);
}
}
启动
先启动nacos-config,再nacos-provider,再nacos-consumer,再nacos-geteway
测试
成功