springboot如何改成springcloud项目?

1. 注意:

nacos服务器版本,springboot,springcloud 和springcloudAliibaba 版本一定要对应

否则项目报错或者注册不到nacos上 

1. pom里加入cloud和cloudalibaba的 管理和 服务发现 的依赖

 <!--springcloud ,springcloudalibaba 和 springboot 各版本对应地址如下-->
    <!--https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E-->

    <!--参考官网: https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/en-us/index.html#_spring_cloud_alibaba_nacos_discovery-->

    <dependencyManagement>
        <!--springboot 要使用 cloudlibaba的功能,先引入 cloud的依赖,然后再引入 cloudalibaba的依赖,因为  cloudalibaba的依赖 是 cloud 的子工程 -->
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <!-- SpringBoot 2.2.x 以上版本这样配置 -->
                <version>${spring-cloud.version}</version>
<!--                <version>Hoxton.SR8</version>-->
<!--                <version>2.2.6.RELEASE</version>-->
<!--                <version>Spring Cloud 2020.0.1</version>-->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
<!--                <version>${spring-cloud-alibaba.version}</version>-->
<!--                <version>2.2.3.RELEASE</version>-->
                <version>2.2.7.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>

    </dependencyManagement>
 <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>jsr305</artifactId>
                    <groupId>com.google.code.findbugs</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>HdrHistogram</artifactId>
                    <groupId>org.hdrhistogram</groupId>
                </exclusion>
            </exclusions>
        </dependency>

2.启动类上加注解,开启服务发现

@EnableDiscoveryClient

3. 配置文件加nacos的注册地址

server.port=8092
spring.application.name=order-service



#nacos服务发现中心
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#spring.cloud.nacos.config.server-addr=127.0.0.1:8848
management.endpoints.web.exposure.include=*
#spring.cloud.nacos.config.file-extension=properties
# true为注册服务到nacos上,false为不注册
spring.cloud.service-registry.auto-registration.enabled=true

nacos 版本 2.0.3(windows)

默认是 集群启动

 

发现启动报错

 

改成单机模式启动

startup.cmd -m standalone

 

 上面给出了nacos控制台地址:

http://192.168.60.84:8848/nacos/index.html

先启动子项目:

 说明注册成功,

然后登入 nacos控制台 ,发现服务注册上了

 

 

全部的pom依赖如下:

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
<!--        <version>2.4.2</version>-->
        <version>2.2.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.authresourcs</groupId>
    <artifactId>order-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>order-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <!--        <spring-cloud.version>Greenwich.SR4</spring-cloud.version>-->
<!--        <spring-cloud.version>Spring Cloud Hoxton.SR12</spring-cloud.version>-->
        <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
    </properties>


    <!--springcloud ,springcloudalibaba 和 springboot 各版本对应地址如下-->
    <!--https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E-->

    <!--参考官网: https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/en-us/index.html#_spring_cloud_alibaba_nacos_discovery-->

    <dependencyManagement>
        <!--springboot 要使用 cloudlibaba的功能,先引入 cloud的依赖,然后再引入 cloudalibaba的依赖,因为  cloudalibaba的依赖 是 cloud 的子工程 -->
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <!-- SpringBoot 2.2.x 以上版本这样配置 -->
                <version>${spring-cloud.version}</version>
<!--                <version>Hoxton.SR8</version>-->
<!--                <version>2.2.6.RELEASE</version>-->
<!--                <version>Spring Cloud 2020.0.1</version>-->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
<!--                <version>${spring-cloud-alibaba.version}</version>-->
<!--                <version>2.2.3.RELEASE</version>-->
                <version>2.2.7.RELEASE</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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>



        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>jsr305</artifactId>
                    <groupId>com.google.code.findbugs</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>HdrHistogram</artifactId>
                    <groupId>org.hdrhistogram</groupId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>


    <!-- SpringCloud 官方远程仓库.MS:部分本地仓库和镜像仓库没有SpringCloud子项目依赖。 -->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>





</project>

然后配置一个网关服务:

cloud版本和上面的一样:

<?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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cloudalibaba</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
    </properties>

    <!--因为gateway的webflux与spring-webmvc有冲突,所以不用引入stater-web-->
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</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-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>
        <!--geteway-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!--feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>jsr305</artifactId>
                    <groupId>com.google.code.findbugs</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <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>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

启动类代码如下:

package com.gateway;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class GatewayServiceApplication {

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


}

配置文件: application.yml

server:
  port: 9999

spring:
  application:
    # 应用名称
    name: gateway-service

    main:
      allow-bean-definition-overriding: true

  profiles:
    # 当前环境对应的 profile  name-active 为读取的配置文件
    active: develop
  cloud:
    nacos:
  # true为注册服务到nacos上,false为不注册
      discovery:
        server-addr: localhost:8848
        #此处的namespace是discovery服务对应的命名空间,与config不同
        #namespace: b7d26021-28b5-42d1-b36d-737a818a2146

      config:
        # 如果不想使用 Nacos 进行配置管理,设置为 false 即可
        enabled: false
        # Nacos Server 地址
        server-addr: 127.0.0.1:8848
        # 组,默认为 DEFAULT_GROUP
        group: DEFAULT_GROUP
        # 配置内容的数据格式,默认为 properties
        file-extension: yaml
        # 指定命名空间,默认为public
        namespace: dev_space
    # 将网关也 注册到nacos 上
    service-registry:
        auto-registration:
         enabled: true

    gateway:
      discovery:
        locator:
          #表示将请求路径的服务名配置改成小写  因为服务注册的时候,向注册中心注册时将服务名转成大写的了
          lowerCaseServiceId: true
          #表示gateway开启服务注册和发现功能,
          #并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务
          enabled: true
      httpclient:
        response-timeout: 5000
        connect-timeout: 5000


ribbon:
  ReadTimeout: 120000
  ConnectTimeout: 120000





然后进入nacos控制台进行查看:

 接下来测试网关:

 网关的配置如下:

spring:
    cloud:
        gateway:
            routes:
            - id: nacos-server
              uri: lb://nacos-server #代表从注册中心获取服务,且以lb(负载均衡)的方式转发
              predicates:             #断言
              - Path=/nacos-server/**     #表示将以/provider/**开头的请求转发到uri为lb://nacos-server的地址上
              filters:
              - StripPrefix=1 #1为去掉path的/provider
            - id: pay-service
              uri: lb://pay-service #代表从注册中心获取服务,且以lb(负载均衡)的方式转发
              predicates:             #断言
              - Path=/pay-service/**     #表示将以/provider/**开头的请求转发到uri为lb://pay-service的地址上
              filters:
              - StripPrefix=1 #1为去掉path的/pay-service

            - id: order-service
              uri: lb://order-service #代表从注册中心获取服务,且以lb(负载均衡)的方式转发
              predicates:             #断言
              - Path=/order-service/**     #表示将以/provider/**开头的请求转发到uri为lb://order-service的地址上
              filters:
              - StripPrefix=1 #1为去掉path的/order-service

在浏览器输入:

http://localhost:9999/order-service/order/hello

 说明网关转发成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值