首页

关于nacos服务生产消费、网关和配置中心测试demo示例maven项目下载

标签:nacos配置列表,nacos服务调用,注册中心,自定义命名空间,配置隔离,nacos微服务拆分,nacos网关配置     发布时间:2022-05-04   
  • 云盘下载:
  • [提取码:1jwf]
  • 本地下载:
       ( 需积分:8  )

一、项目说明

该项目对nacos注册中心、服务生产消费、网关隔离配置等进行示例测试说明,项目结构如下

关于nacos服务生产消费、网关和配置中心测试demo示例maven项目下载

二、运行说明

1. 启动nacos注册中心服务(安装配置参考其他文章页面)

2. 下载导入该示例maven项目(将项目maven-settings.xml修改本地仓库地址后,映射maven配置)

关于nacos服务生产消费、网关和配置中心测试demo示例maven项目下载

3. 全文搜索修改server-addr并调整nacos的注册中心地址

4. nacos注册中心配置列表

4.1. 服务提供者 两个配置文件新增  -  nacos-provider.properties、shared-c1.properties (group都是默认DEFAULT_GROUP)

关于nacos服务生产消费、网关和配置中心测试demo示例maven项目下载

4.1.1 nacos-provider.properties

order.maxSize=1999@b@order.minSize=19

4.1.2 shared-c1.properties

share.c1.value=ccc111

4.1.3 服务提供者接口代码

package com.nacos.controller;@b@@b@import com.nacos.util.IpConfiguration;@b@import org.springframework.beans.factory.annotation.Autowired;@b@import org.springframework.beans.factory.annotation.Value;@b@import org.springframework.cloud.context.config.annotation.RefreshScope;@b@import org.springframework.web.bind.annotation.GetMapping;@b@import org.springframework.web.bind.annotation.PathVariable;@b@import org.springframework.web.bind.annotation.RequestMapping;@b@import org.springframework.web.bind.annotation.RestController;@b@@b@import java.util.HashMap;@b@import java.util.Map;@b@ @b@@RequestMapping("/order")@b@@RestController@b@@RefreshScope@b@public class OrderInfoController {@b@    @Autowired@b@    private IpConfiguration ipConfiguration;@b@@b@    @Value("${order.maxSize}")@b@    private  String  maxSize;@b@@b@    @Value("${order.minSize}")@b@    private  String  minSize;@b@@b@    @Value("${share.c1.value}")@b@    private  String  shareC1Value;@b@@b@@b@    @GetMapping("/getOrderInfo/{param}")@b@    public Map<String, Object> echo(@PathVariable String param){@b@        Map<String,Object> result = new HashMap<>(2);@b@        result.put("port",ipConfiguration.getServerPort());@b@        result.put("orderNo","20200003");@b@        result.put("code",param);@b@        result.put("name","商品名");@b@        result.put("number",1900);@b@        result.put("maxSize",maxSize);@b@        result.put("minSize",minSize);@b@        result.put("shareC1Value",shareC1Value);@b@        return result;@b@    }@b@}

4.2 服务消费者新增两个配置  - nacos-consumer.properties、nacos-consumer-aaa.properties (group都是分别为dev、dev2)

关于nacos服务生产消费、网关和配置中心测试demo示例maven项目下载

4.2.1 nacos控制台“命名空间”新增测试空间

关于nacos服务生产消费、网关和配置中心测试demo示例maven项目下载

4.2.2 nacos-consumer.properties

test.abc=testabc

4.2.3  nacos-consumer-aaa.properties 

test.dev2.aaa=aaa

4.2.4 服务消费者代码

package com.nacos.controller;@b@@b@import org.springframework.beans.factory.annotation.Autowired;@b@import org.springframework.beans.factory.annotation.Value;@b@import org.springframework.web.bind.annotation.GetMapping;@b@import org.springframework.web.bind.annotation.PathVariable;@b@import org.springframework.web.bind.annotation.RestController;@b@import org.springframework.web.client.RestTemplate;@b@@b@@RestController@b@public class TestController {@b@    @Autowired@b@    private RestTemplate restTemplate;@b@@b@    @Value("${test.abc}")@b@    private  String  testAbc;@b@@b@    @Value("${test.dev2.aaa}")@b@    private  String  testDev2Aaa;@b@@b@@b@@b@    @GetMapping("/getOrderInfo/{param}")@b@    public String echo(@PathVariable String param) {@b@        //直接访问服务,调过网关@b@        param=testAbc;//定义测试空间,并测试值@b@        param=param.concat("-").concat(testDev2Aaa);//测试子配置@b@        return restTemplate.getForObject("http://nacos-provider/order/getOrderInfo/" + param, String.class);@b@    }@b@}

5. 分别运行NacosProviderApplication.java、GatewayApplication.java、NacosConsumerApplication.java

5.1 访问服务提供者自己接口 http://localhost:8070/order/getOrderInfo/1,结果如下

{@b@	"number": 1900,@b@	"orderNo": "20200003",@b@	"code": "1",@b@	"port": 8070,@b@	"shareC1Value": "ccc111",@b@	"name": "商品名",@b@	"maxSize": "1999",@b@	"minSize": "19"@b@}

5.2 访问网关接口地址http://localhost/order/getOrderInfo/1 和  服务消费者接口地址 http://localhost:8082/getOrderInfo/1 ,结果一样如下

{@b@	"number": 1900,@b@	"orderNo": "20200003",@b@	"code": "testabc-aaa",@b@	"port": 8070,@b@	"shareC1Value": "ccc111",@b@	"name": "商品名",@b@	"maxSize": "1999",@b@	"minSize": "19"@b@}