Spring Cloud Huawei + CSE 搭建微服务项目

项目搭建基本步骤

  • (一)搭建注册与配置中心CSE
  • (二)项目总pom文件
      • 1. pom.xml 文件配置
  • (三)服务提供者(huawei-provider)
      • 1. pom.xml 配置
      • 2. bootstrap.yml 配置
      • 3. Controller 编写
      • 4. 主启动类
  • (四)服务消费者(huawei-consumer)
      • 1. pom.xml 配置
      • 2. bootstrap.yml 配置
      • 3. Controller 编写
      • 4. 主启动类
  • (五)网关(huawei-gateway)
      • 1. pom.xml 配置
      • 2. bootstrap.yml 配置
      • 3. 主启动类
  • (六)验证

(一)搭建注册与配置中心CSE

  1. 从官网下载对应版本下载地址
  2. 解压文件,然后执行 cse.exe
  3. 访问 http://127.0.0.1:30103 则可看到 CSE 页面

(二)项目总pom文件

1. pom.xml 文件配置

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

  <groupId>com.coffee</groupId>
  <artifactId>huawei-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <properties>
    <spring-boot.version>2.6.7</spring-boot.version>
    <spring-cloud.version>2021.0.3</spring-cloud.version>
    <spring-cloud-huawei.version>1.10.2-2021.0.x</spring-cloud-huawei.version>
  </properties>

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

  <modules>
    <module>huawei-consumer</module>
    <module>huawei-provider</module>
    <module>huawei-gateway</module>
  </modules>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <compilerArgument>-parameters</compilerArgument>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>2.1.6.RELEASE</version>
          <executions>
            <execution>
              <goals>
                <goal>repackage</goal>
              </goals>
              <configuration>
                <mainClass>${main.class}</mainClass>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

(三)服务提供者(huawei-provider)

1. pom.xml 配置

<?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>com.coffee</groupId>
    <artifactId>huawei-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>huawei-provider</artifactId>
  <packaging>jar</packaging>

  <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.huaweicloud</groupId>
      <artifactId>spring-cloud-starter-huawei-service-engine</artifactId>
    </dependency>
  </dependencies>

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

2. bootstrap.yml 配置

server:
  port: 8010

spring:
  application:
    name: huawei-provider
  cloud:
    servicecomb:
      discovery:
        enabled: true
        watch: false
        address: http://127.0.0.1:30100
        appName: hauweicloud-application
        serviceName: ${spring.application.name}
        version: 0.0.1
      config:
        serverType: kie
        serverAddr: http://127.0.0.1:30110

3. Controller 编写

@RestController
public class ProviderController {

  @GetMapping("/sayHello")
  public String sayHello(@RequestParam("name") String name) {
    return "Hello " + name + ", This is Spring cloud Huawei + CSE!";
  }

}

4. 主启动类

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

(四)服务消费者(huawei-consumer)

1. pom.xml 配置

<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>com.coffee</groupId>
    <artifactId>huawei-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>huawei-consumer</artifactId>
  <packaging>jar</packaging>

  <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.huaweicloud</groupId>
      <artifactId>spring-cloud-starter-huawei-service-engine</artifactId>
    </dependency>
  </dependencies>

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

2. bootstrap.yml 配置

server:
  port: 8020

spring:
  application:
    name: huawei-consumer
  cloud:
    servicecomb:
      discovery:
        enabled: true
        watch: false
        address: http://127.0.0.1:30100
        appName: hauweicloud-application
        serviceName: ${spring.application.name}
        version: 0.0.1
      config:
        serverType: kie
        serverAddr: http://127.0.0.1:30110

3. Controller 编写

@RestController
public class ConsumerController {
  @Autowired
  private RestTemplate restTemplate;

  @GetMapping("/sayHello")
  public String sayHello(@RequestParam("name") String name) {
    return restTemplate.getForObject("http://huawei-provider/sayHello?name={1}", String.class, name);
  }
}

4. 主启动类

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

  @LoadBalanced
  @Bean
  public RestTemplate restTemplate() {
    return new RestTemplate();
  }
}

(五)网关(huawei-gateway)

1. pom.xml 配置

<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>com.coffee</groupId>
    <artifactId>huawei-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>huawei-gateway</artifactId>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>com.huaweicloud</groupId>
      <artifactId>spring-cloud-starter-huawei-service-engine-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 配置

server:
  port: 9090

spring:
  application:
    name: huawei-gateway
  cloud:
    servicecomb:
      discovery:
        enabled: true
        watch: false
        address: http://127.0.0.1:30100
        appName: hauweicloud-application
        serviceName: ${spring.application.name}
        version: 0.0.1
      config:
        serverType: kie
        serverAddr: http://127.0.0.1:30110


    gateway:
      routes:
        - id: consumer
          uri: lb://huawei-consumer
          filters:
            - name: governance
          predicates:
            - Path=/**

3. 主启动类

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

(六)验证

  1. 分别启动 huawei-provider、huawei-consumer 和 huawei-gateway
  2. 访问 http://localhost:9090/sayHello?name=huawei,返回 “Hello huawei, This is Spring cloud Huawei + CSE!” 则项目搭建成功。

你可能感兴趣的:(微服务,spring,cloud,微服务,huawei)