搭建微服务基础组件-注册中心、配置中心、网关

因为前面有详细介绍过SpringCloud Alibaba及其组件,所以这里就快速地完成了;

http://www.jiguiquan.com/backend/cloud2-0-alibaba

选型:

  • 注册中心:SpringCloud Alibaba-Nacos

  • 配置中心:SpringCloud Alibaba-Nacos

  • 服务间调用:SpringCloud-OpenFeign

  • 网关:SpringCloud-Gateway

一、引入Nacos作为注册中心

1、在common模块的依赖文件中引入SpringCloud Alibaba的依赖管理

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.1.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2、引入Nacos作为注册中的依赖:

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

3、在所有项目的配置文件上加上nacos地址:

spring:
  application:
    name: zidanmall-coupon
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

4、在启动类,开启 @EnableDiscoveryClient 注解

5、所有项目都一样,只要执行3、4两步即可,然后启动所有的模块:

63.jpg

至此,Nacos注册中心已经使用完成;

6、一定要记得对Nacos进行持久化

不然到时候使用配置中心创建的东西就都不见了

http://www.jiguiquan.com/archives/943

6.1、执行 nacos-mysql.sql 脚本文件

6.2、修改 application.properties 文件


二、引入OpenFeign远程调用

引入SpringCloud和OpenFeign的依赖:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-dependencies</artifactId>
         <version>Greenwich.SR3</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
   </dependencies>
</dependencyManagement>

起始在搭建基础项目的时候,这个依赖我已经引入到项目了;


三、引入Nacos作为配置中心

1、在common的pom.xml中引入Nacos Config Starter

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

2、在Nacos中,为每个微服务创建自己的命名空间:

64.jpg

3、使用配置分组区分不同的环境配置dev、test、prod:

65.jpg

2、在所有项目的 resources 目录下创建 bootstrap.yml 配置文件

bootstrap.yml 将会优先于 application.yml 配置文件被读取:

server:
  port: 7000
spring:
  application:
    name: zidanmall-coupon
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        file-extension: yaml
        namespace: 24ddc4a1-6be1-4f38-ac99-4be92df17858
        group: dev
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# 这就是实际拼接好的配置文件的名称 "nacos-config-client-dev.yaml"
#当前缺省${spring.profile.active},所以文件dataId为zidanmall-coupon.yaml

3、写一个测试接口:

@Value("${coupon.user.name}")
private String name;

@RequestMapping("/test")
public R test(){
    return R.ok().put("name", name);
}

4、访问  http://localhost:7000/coupon/coupon/test  

66.jpg

配置中心生效!

5、起始配置中心可以将一个很大的配置文件拆分为多个不同的配置文件,然后进行组合使用:

spring.cloud.nacos.config.ext-config[0].data-id=datasource.yml
spring.cloud.nacos.config.ext-config[1].data-id=mybatis.yml
spring.cloud.nacos.config.ext-config[2].data-id=other.yml

但是开发阶段为了方便,我们还是会先将配置文件写在代码中;


四、引入SpringCloud Gateway作为服务网关

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 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.1.8.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.jiguiquan.zidanmall</groupId>
   <artifactId>zidanmall-gateway</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>zidanmall-gateway</name>
   <description>子丹商城-网关服务</description>

   <properties>
      <java.version>1.8</java.version>
      <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>com.jiguiquan.zidanmall</groupId>
         <artifactId>zidanmall-common</artifactId>
         <version>1.0-SNAPSHOT</version>
      </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>
      </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>
      </dependencies>
   </dependencyManagement>

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

</project>

2、在主启动类加上:

//排除由于依赖common模块而引入的数据库连接驱动
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})  
@EnableDiscoveryClient

3、配置文件,网关以88为端口:

server:
  port: 88

spring:
  application:
    name: zidanmall-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

正常启动:

67.jpg

4、简单做个测试:

server:
  port: 88

spring:
  application:
    name: zidanmall-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      routes:
        - id: test_route              #唯一标识
          uri: https://www.baidu.com  #路由
          predicates:                 #断言数组
            - Query=url,baidu         #有一个参数url=baidu

        - id: qq_route
          uri: https://www.qq.com
          predicates:
            - Query=url,qq

5、重启gateway服务,进行测试

68.jpg

很明显,网关的路由功能已经成功!

69.jpg

关于网关的详细介绍,可以看另一篇博文:

http://www.jiguiquan.com/archives/825

jiguiquan@163.com

文章作者信息...

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐