构建springboot2.5.x 高版本基于nacos 架构 附项目

主要的版本如下:
<spring-boot.version>2.3.12.RELEASE</spring-boot.version>
<spring-cloud.version>2.2.1.RELEASE</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
再次更新为:
<spring-boot.version>2.5.3</spring-boot.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
<spring-cloud-alibaba.version>2020.0.RC1</spring-cloud-alibaba.version>

架构实现的demo功能有nacos的配置中心、服务注册中心、服务的注册于消费、gateway网关的基础配置等

先下载一个nacos 我用的比较新2.0.2的版本,貌似还有个2.0.3的版本。

下载地址:https://github.com/alibaba/nacos/releases/tag/2.0.2

下载完成之后  根据自己的情况执行cmd(window版)或sh(linux版)命令,因为是学习一般都是单独运行,需要在后面补上  -m standalone 

例如我的是window版  命令就是: startup.cmd -m standalone

注意8848端口 别被占用了

启动之后登陆nacos,登陆地址:http://127.0.0.1:8848/nacos    账号:nacos/nacos

配置最好用自己的项目名称,不用也可以单独配置。 

配置完成之后  然后就是上代码了。

在maven里面配置主项目

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.12.RELEASE</version>
    <relativePath/>
</parent>

<groupId>com.htgx.acp</groupId>
<artifactId>acp</artifactId>
<version>1.0.0</version>

<!-- 系统jar版本管理-->
<properties>
    <acp.version>1.0.0</acp.version>
    <java.version>1.8</java.version>
    <spring-cloud.version>2.2.1.RELEASE</spring-cloud.version>
    <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
    <alibaba-druid.version>1.2.6</alibaba-druid.version>
    <pagehelper.version>1.3.0</pagehelper.version>
    <tk.mybatis.version>2.1.5</tk.mybatis.version>
    <fastjson.version>1.2.9</fastjson.version>
</properties>

然后是服务注册于消费的项目的maven主配置,详细的可以看项目里面的配置。

<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>
    <version>${spring-cloud-alibaba.version}</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>${spring-cloud-alibaba.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>${spring-cloud.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

 启动类,不用数据库的 可以去掉MapperScan 注解

/**
 * @author andy.wang
 * @className ApplicationCenterApplication
 * @Date 2021/7/26 14:53
 */
@MapperScan("com.htgx.acp.applicationcenter.mapper")
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ApplicationCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationCenterApplication.class, args);
    }
}

然后是 bootstrap.yml 配置 ,注意 用配置中心的一定要用bootstrap  不能直接使用application 配置。

server:
  port: 8860
  servlet:
    context-path: /acp-application-center

spring:
  application:
    name: acp-application-center
  cloud:
    nacos:
      discovery:
        server-addr: 172.16.6.32:8848
      #不需要配置中心可以注释掉下面的配置
      config:
        server-addr: 172.16.6.32:8848
        file-extension: properties

application 配置  

spring:
  profiles:
    active: @profileActive@

logging:
  config: classpath:logback.xml

mybatis:
  mapper-locations: classpath*:/mapper/*.xml
  configuration:
      #org.apache.ibatis.logging.stdout.StdOutImpl 控制台打印sql语句方便调试sql语句执行错误
      #org.apache.ibatis.logging.log4j2.Log4j2Impl:这个不在控制台打印查询结果,但是在log4j中打印
      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

然后是服务的消费使用,注意项目名称 和path路径,我的是一样的 如果不一样配置也要注意。

@FeignClient(name = "acp-application-center",path = "acp-application-center")
public interface Test {

    @RequestMapping("/tLabel/findOne")
    TLabel findOne(Map param);
}

然后是Controller 类的demo代码,配置中心有两种实现方式一种是boot 一种是cloud 。我这边是cloud的方式  RefreshScope 注解是标注跟随配置中心刷新配置。

@Slf4j
@RestController
@RequestMapping("/tLabel")
@RefreshScope
public class TLabelController {

   @Autowired
   private TLabelService tLabelService;

@RequestMapping(value="/findOne")
 public TLabel findOne(@RequestBody Map param){
     String idStr = (String)param.get("id");
     Long id = Long.parseLong(idStr);
     TLabel tLabel = tLabelService.selectByPrimaryKey(id);
     System.out.println(JSON.toJSONString(tLabel));
     return tLabel;
 }

 @Value(value = "${ones}")
 private String ones;

@Autowired
 private Test test;

 @RequestMapping(value="/findOneTest")
 public TLabel findOneTest(@RequestBody Map param){
     System.out.println("nacos config:"+ones);
     return test.findOne(param);
 }

启动之后 用postman 测试下接口能不能用 ,再接着配置网关。pom配置

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--gateway网关-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>

启动类配置

@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

配置

server:
  port: 9001
spring:
  application:
    name: ac-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      discovery:
        locator:
          enabled: false
          #开启小写验证,默认feign根据服务名查找都是用的全大写
          lowerCaseServiceId: true
      routes:
      - id: client-test
        uri: lb://CLIENT-TEST
        predicates:
        - Path=/testclient/**
        filters:
        - StripPrefix=1
      - id: service-feign
        uri: lb://FEIGN
        predicates:
        - Path=/service-feign/**
        filters:
        - StripPrefix=1

启动之后把之前测试的端口调整到gateway 服务器来再试试。

项目下载地址:https://download.csdn.net/download/wangzhi291/20658449

再次把配置更新到nacos配置中心去,创建命名空间来区分不同的环境。

 配置放在不同的命名空间里面,到时候服务也是类似的把不同环境隔离开来。

具体的配置 在代码里面有注释可以看。

再次上传新版本的项目代码: https://download.csdn.net/download/wangzhi291/20685724

猜你喜欢

转载自blog.csdn.net/wangzhi291/article/details/119253368