- 进入到nacos安装目录,Linux开启 nacos 服务
nacos安装
docker-compose -f example/standalone-derby.yaml up
- 创建Spring Cloud 工程
- 父
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.7.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
</dependencies>
provider
- pom.xml 导入注册discovery 、连接数据库 和 mybatis-plus 的必要依赖
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
</dependencies>
- application.yml 配置nacos:discovery 相关属性 服务器地址和nacos命名空间
server:
port: 8880
servlet:
context-path: "/api/"
spring:
profiles:
active: dev
application:
name: provider-server
cloud:
nacos:
discovery:
server-addr: "192.168.140.130:8848"
namespace: "b091994d-1518-4537-9ad6-290c821dd681"
username: ""
password: ""
- 配置 mybatis-plus
@Configuration
@MapperScan("com.smart.provider.**.mapper")
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setOverflow(true);
paginationInnerInterceptor.setMaxLimit(50L);
mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
return mybatisPlusInterceptor;
}
}
- 反向生成数据库 user 表(entity、mapper、mapper.xml )
- service
@Service
public class UserService {
@Resource
UserMapper userMapper;
public List<User> userList(int page,int size){
Page<User> selectPage = userMapper.selectPage(new Page<>(page, size), new QueryWrapper<>());
return selectPage.getRecords();
}
}
- controller
@RestController
@RequestMapping("/provider")
public class ProviderController {
@Resource
UserService userService;
@GetMapping("/hello")
public String hello() {
return "provider";
}
@GetMapping("/user")
public List<User> userList(int page,int size) {
return userService.userList(page,size);
}
}
consumer
- pom.xml 引入nacos、feign相关依赖
<dependencies>
<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>
</dependencies>
- application.yml
server:
port: 8881
spring:
application:
name: consumer-server
cloud:
nacos:
discovery:
server-addr: "192.168.140.130:8848"
namespace: "b091994d-1518-4537-9ad6-290c821dd681"
- 启动类 @EnableFeignClients 将feignClient接口注入到spring ioc
@SpringBootApplication
@EnableFeignClients
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class, args);
}
}
- user实体类也要写在consumer,删掉不必要的注解
- 服务降级
@FeignClient(name = "provider-server", path = "/api/provider")
public interface ProviderApi {
@GetMapping("/user")
List<User> userList(@RequestParam int page, @RequestParam int size);
}
- controller
@RestController
public class ConsumerController {
@Resource
ProviderApi providerApi;
@GetMapping("/user")
public List<User> userList(int page,int size) {
return providerApi.userList(page, size);
}
}
- provider 开启多实例运行 修改端口号 8880、8882两个实例
测试
|