Nacos(三)GateWay 和 Nacos结合,使用Nacos作为配置中心和注册中心

文章目录

  • 一、工程介绍
    • 1、父模块
    • 2、nacos-provider
      • 2.1 pom文件
      • 2.2 启动类
      • 2.3 controller
      • 2.4 bootstrap文件
    • 3、nacos-consumer模块
      • 3.1 pom文件
      • 3.2 启动类
      • 3.3 controller
      • 3.4 bootstrap文件
    • 4、gateway模块
      • 4.1 pom文件
      • 4.2 启动类
      • 4.3 bootstrap文件
  • 二、 nocas注册中心和配置中心相关配置
    • 1、在注册中心建立不同环境的命名空间
    • 2、在配置中心编写相关配置
      • 2.1 nacos-provider相关配置
      • 2.2 nacos-consumer相关配置
      • 2.3 gateway相关配置文件
  • 三、启动项目
    • 1、启动三个模块
    • 2、接口访问


一、工程介绍

本次采用的是一个父模块,三个子模块(网关,nacosprovider,nacosconsumer)的方式来构建的。

1、父模块

pom文件


<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.0modelVersion>
    <packaging>pompackaging>
    <modules>
        <module>nacos-providermodule>
        <module>nacos-consumermodule>
        <module>gatewaymodule>
    modules>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-autoconfigureartifactId>
        dependency>
    dependencies>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.3.2.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com._520xuzaigroupId>
    <artifactId>xuzai-parentartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>xuzai-parentname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
        <java.version>1.8java.version>
        <spring-cloud.version>Hoxton.SR8spring-cloud.version>
        <spring-cloud-alibaba.version>2.2.4.RELEASEspring-cloud-alibaba.version>

    properties>

    <dependencyManagement>   
        <dependencies>
            
            
            
            
            

            
            
            
            
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
                <version>${spring-cloud-alibaba.version}version>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
                <version>${spring-cloud-alibaba.version}version>
            dependency>

        dependencies>
    dependencyManagement>

    
    
    
    
    
    
    
    
    

project>

2、nacos-provider

2.1 pom文件


<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">
    <parent>
        <artifactId>xuzai-parentartifactId>
        <groupId>com._520xuzaigroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>nacos-providerartifactId>
    <groupId>com._520xuzai.nacosprovodergroupId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-contextartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-commonsartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>

    dependencies>


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

2.2 启动类


package com._520xuzai.nacosprovider;

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

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 思源
 * \* Date: 2021/1/30
 * \* Time: 22:59
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

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

}

2.3 controller

package com._520xuzai.nacosprovider.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 思源
 * \* Date: 2021/1/31
 * \* Time: 9:02
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
@RefreshScope
@Slf4j
@RestController
@RequestMapping("/provider")
public class ProviderTestController {


    @RequestMapping("/get")
    public String get() {
        return "you get me -- provider";
    }


}


2.4 bootstrap文件

spring:
  application:
    name: nacosprovider
  cloud:
    nacos:
      config:
        file-extension: yaml
        server-addr: 192.168.76.134:8848 #这里是配置中心的地址
        group: DEFAULT_GROUP
        username: app
        password: app
  #启动环境
  profiles:
    active: dev

3、nacos-consumer模块

3.1 pom文件


<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">
    <parent>
        <artifactId>xuzai-parentartifactId>
        <groupId>com._520xuzaigroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>nacos-consumerartifactId>
    <groupId>com._520xuzai.nacosconsumergroupId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-contextartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-commonsartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
    dependencies>

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

3.2 启动类

package com._520xuzai.nacosconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 思源
 * \* Date: 2021/1/31
 * \* Time: 9:01
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

3.3 controller

package com._520xuzai.nacosconsumer.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 思源
 * \* Date: 2021/1/31
 * \* Time: 9:02
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
@RefreshScope
@Slf4j
@RestController
@RequestMapping("/consumer")
public class ConsumerTestController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${useLocalCache:false}")
    private boolean useLocalCache;

    @RequestMapping("/get")
    public boolean get() {
        return useLocalCache;
    }

    @RequestMapping("/getProvider")
    public String getProvider() {
        return restTemplate.getForObject("http://nacosprovider/provider/get",String.class);
    }
}

3.4 bootstrap文件

spring:
  application:
    name: nacosconsumer
  cloud:
    nacos:
      config:
        file-extension: yaml
        server-addr: 192.168.76.134:8848 #这里是配置中心的地址
        group: DEFAULT_GROUP
        username: app
        password: app
  #启动环境
  profiles:
    active: dev

4、gateway模块

4.1 pom文件


<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">
    <parent>
        <artifactId>xuzai-parentartifactId>
        <groupId>com._520xuzaigroupId>
        <version>0.0.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>gatewayartifactId>
    <groupId>com._520xuzai.gatewaygroupId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
        dependency>

        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
        dependency>
    dependencies>

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

4.2 启动类

package com._520xuzai.gateway;

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

/**
 * \* Created with IntelliJ IDEA.
 * \* User: 思源
 * \* Date: 2021/1/31
 * \* Time: 9:04
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

}


4.3 bootstrap文件

spring:
  application:
    name: gateway
  cloud:
    nacos:
      config:
        file-extension: yaml
        server-addr: 192.168.76.134:8848 #这里是配置中心的地址
        group: DEFAULT_GROUP
        username: app
        password: app
  #启动环境
  profiles:
    active: dev        

二、 nocas注册中心和配置中心相关配置

为了更好理解配置中心和注册中心不是一回事,我们设置配置中心地址为192.168.76.134:8848
注册中心为192.168.76.133:8848

1、在注册中心建立不同环境的命名空间

这里我们使用dev环境来测试
Nacos(三)GateWay 和 Nacos结合,使用Nacos作为配置中心和注册中心_第1张图片

2、在配置中心编写相关配置

Nacos(三)GateWay 和 Nacos结合,使用Nacos作为配置中心和注册中心_第2张图片

2.1 nacos-provider相关配置


server:
  port: 9200

spring:
  #nacos 服务发现
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.76.133:8848 #这里是注册中心的地址
        service: ${spring.application.name}
        namespace: 7dfd49ba-c17d-470b-b082-34dfb9d586ff #这里是注册中心的dev环境的命名空间
        username: app
        password: app

2.2 nacos-consumer相关配置

server:
  port: 9300

spring:
  #nacos 服务发现
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.76.133:8848 #这里是注册中心的地址
        service: ${spring.application.name}
        namespace: 7dfd49ba-c17d-470b-b082-34dfb9d586ff
        username: app
        password: app

2.3 gateway相关配置文件

server:
  port: 9400

spring:
  #nacos 服务发现
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.76.133:8848 #这里是注册中心的地址
        service: ${spring.application.name}
        namespace: 7dfd49ba-c17d-470b-b082-34dfb9d586ff
        username: app
        password: app
    gateway:
      discovery:
        locator:
          enabled: true  #表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务。
          lowerCaseServiceId: true   #是将请求路径上的服务名配置为小写(因为服务注册的时候,向注册中心注册时将服务名转成大写的了),比如以/service-hi/*的请求路径被路由转发到服务名为service-hi的服务上。
      routes:
        - id: nacosconsumer-server
          uri: lb://nacosconsumer #负载到nacosconsumer
          predicates:
            - Path=/consumer/**
        - id: nacosprovider-server
          uri: lb://nacosprovider #负载到nacosprovider
          predicates:
            - Path=/provider/**

三、启动项目

1、启动三个模块

在注册中心可以看到如下内容

Nacos(三)GateWay 和 Nacos结合,使用Nacos作为配置中心和注册中心_第3张图片

2、接口访问

调用相关接口,即可转发到对应的服务上

你可能感兴趣的:(naocs,java,gateway,网关)