spring cloud alibaba 2.2.1 入門教程

一:nacos相關

  1. 先下載nacos 1.3,直接官網下載解壓即可
    由於我使用的mysql是8.0,默認支持的是5版本,所以需要修改點東西
    在解壓文件裏面新建文件夾如 D:\nacos\plugins\mysql 然後將8版本的java驅動包放進去(linux同理)

在這裏插入圖片描述在這裏插入圖片描述
2.新建數據庫nacos,執行下圖中的nacos-mysql.sql 初始化表 ,修改配置文件
在這裏插入圖片描述
在文件末尾加上

spring.datasource.platform=mysql

db.num=1
db.url.0=jdbc:mysql://192.168.1.130:3306/nacos?zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&useSSL=false&useUnicode=true&characterEncoding=UTF-8
db.user=root
db.password=root
  1. 直接啓動 D:\nacos\bin\startup.cmd 就行了

二: 自定義parents

在這裏插入圖片描述


<?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">
    <modelVersion>4.0.0</modelVersion>

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

    <groupId>aaa.com.cn</groupId>
    <artifactId>spring-cloud-alibaba-parent</artifactId>
    <version>2.2.1-RELEASE</version>
    <packaging>pom</packaging>

    <properties>
        <!-- Environment Settings -->
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Spring Settings -->
        <spring-boot.version>2.2.8.RELEASE</spring-boot.version>
        <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <name>spring-cloud-alibaba-parent</name>
    <url>https://www.aaa.com.cn</url>
    <inceptionYear>2020-Now</inceptionYear>

    <developers>
        <developer>
            <name></name>
            <url>https://gitee.com/</url>
            <email>672@qq.com</email>
        </developer>
    </developers>

    <organization>
        <name>aaa</name>
        <url>https://www.aaa.com.cn/</url>
    </organization>

    <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>

    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <configuration>
                            <archive>
                                <manifest>
                                    <!-- Add directory entries -->
                                    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                    <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                                    <addClasspath>true</addClasspath>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- resource -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>

            <!-- install -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
            </plugin>

            <!-- clean -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
            </plugin>

            <!-- ant -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
            </plugin>

            <!-- dependency -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
        </plugins>

        <pluginManagement>
            <plugins>
                <!-- Java Document Generate -->

                <!-- YUI Compressor (CSS/JS壓縮) -->
            </plugins>
        </pluginManagement>

        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
 
    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-repos</id>
            <name>Aliyun Repository</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>



然後deploy自己私服就行了

三:簡易服務提供者

  1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>aaa.com.cn</groupId>
        <artifactId>spring-cloud-alibaba-parent</artifactId>
        <version>2.2.1-RELEASE</version>
    </parent>

    <artifactId>spring-cloud-alibaba-nacos-provider</artifactId>
    <version>1.0.0-RELEASE</version>

    <packaging>jar</packaging>

    <dependencies>
        <!-- Spring Boot Begin -->
        <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>aaa.com.cn.Start</mainClass>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  1. 配置文件 bootstrap.properties
# setting
server.port=8000
spring.profiles.active=dev
spring.application.name=provider-A
server.servlet.context-path=/provider
spring.output.ansi.enabled=always

spring.cloud.inetutils.preferred-networks=192.168

spring.cloud.nacos.config.group=${spring.profiles.active}
spring.cloud.nacos.config.server-addr=192.168.1.130:8848

spring.cloud.nacos.discovery.group=DEFAULT_GROUP
spring.cloud.nacos.discovery.server-addr=192.168.1.130:8848

management.endpoints.web.exposure.include=*
  1. 啓動類
 

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

 
@SpringBootApplication
@EnableDiscoveryClient
public class Start {
    public static void main(String[] args) {
        SpringApplication.run(Start.class, args);
    }

    @Value("${my-version:1.0.0}")
    String version;

    @RestController
    public class IndexController {
        @GetMapping(value = "/index/{message}")
        public String index(@PathVariable(value = "message") String message) {
            return "Hello Nacos Discovery provider 1 => " + version + message;
        }
    }
}

四:簡易服務消費

  1. 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>aaa.com.cn</groupId>
        <artifactId>spring-cloud-alibaba-parent</artifactId>
        <version>2.2.1-RELEASE</version>
    </parent>

    <artifactId>spring-cloud-alibaba-nacos-consumer</artifactId>
    <version>1.0.0-RELEASE</version>

    <packaging>jar</packaging>

    <dependencies>
        <!-- Spring Boot Begin -->
        <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>aaa.com.cn.Start</mainClass>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  1. 配置文件bootstrap.properties
# setting
server.port=8001
spring.profiles.active=dev
spring.application.name=consumer-A
server.servlet.context-path=/consumer
spring.output.ansi.enabled=always

spring.cloud.inetutils.preferred-networks=192.168

spring.cloud.nacos.config.group=${spring.profiles.active}
spring.cloud.nacos.config.server-addr=192.168.1.130:8848

spring.cloud.nacos.discovery.group=DEFAULT_GROUP
spring.cloud.nacos.discovery.server-addr=192.168.1.130:8848

management.endpoints.web.exposure.include=*

feign.client.config.default.connectTimeout= 5000
feign.client.config.default.readTimeout= 5000

3.啓動類

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 Start {
    public static void main(String[] args) {
        SpringApplication.run(Start.class, args);
    }

}
  1. 接口demo
 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.cloud.context.config.annotation.RefreshScope;
 
@RestController
@RefreshScope//動態刷新 從nacos config獲取最新配置數據,不用重啓服務
public class NacosConsumerController {

    @Autowired
    private IEchoService echoService;

    @Value("${author:ab}")//nacos config 配置字段
    String name;

    @Value("${my-version:1.0.0}")
    String version;

    @GetMapping(value = "/index/hi")
    public String index() {
        return echoService.index(name + version);
    }
}

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author wongH
 * @date 2020/1/16 16:32
 * @Version 1.0
 * @description  path調用服務方的根路徑,沒有則不用寫
 */
@FeignClient(value = "provider-A", path = "/provider")
public interface IEchoService {

    @GetMapping(value = "/index/{message}")
    String index(@PathVariable(value = "message") String message);

}

本地啓動試試吧 http://127.0.0.1:8001/consumer/index/hi

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章