这里是【微服务~Nacos】,关注我学习云原生不迷路
如果对你有帮助,给博主一个免费的点赞以示鼓励
欢迎各位点赞评论收藏⭐️

专栏介绍

【微服务~Nacos】 目前主要更新微服务,一起学习一起进步。

本期介绍

本期主要介绍微服务~Nacos

文章目录

搭建父项目

服务提供者Provider

搭建服务

创建服务

查看服务

注册异常

服务消费者Consumer

搭建服务

创建服务

查询服务

搭建父项目

  • 项目名:nacos-parent-2.1

  • 添加坐标

 org.springframework.cloudspring-cloud-build2.3.5.RELEASEHoxton.SR122.2.7-SNAPSHOT3.4.01.1.102.7.00.9.02.9.7org.springframework.bootspring-boot-dependencies${spring-boot.version}pomimportorg.springframework.cloudspring-cloud-dependencies${spring.cloud.version}pomimportcom.alibaba.cloudspring-cloud-alibaba-dependencies2.2.7.RELEASEpomimportcom.baomidoumybatis-plus-boot-starter${mybatis.plus.starter.version}com.alibabadruid-spring-boot-starter${durid.starter.version}io.springfoxspringfox-swagger2${swagger.version}io.springfoxspringfox-swagger-ui${swagger.version}org.apache.maven.pluginsmaven-javadoc-plugintrue
  • 项目目录结构

服务提供者Provider

搭建服务

  • 创建项目:nacos-provider-2.1

  • 添加依赖:

org.springframework.bootspring-boot-starter-webcom.alibaba.cloudspring-cloud-starter-alibaba-nacos-discovery
  • 创建yml文件
#server.port=8070#spring.application.name=service-provider#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848#端口号server:port: 8070spring:application:name: service-provider#服务名cloud:nacos:discovery:server-addr: 127.0.0.1:8848 #nacos服务地址

创建服务

  • 启动类

package com.czxy.nacos;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClient//服务发现public class TestNacosProviderApplication {public static void main(String[] args) {SpringApplication.run(TestNacosProviderApplication.class, args );}}
  •  处理类controller

package com.czxy.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;@RestControllerpublic class EchoController {@Resourceprivate HttpServletRequest request;@GetMapping("/echo/{string}")public String echo(@PathVariable String string) {int serverPort = request.getServerPort();return "Hello Nacos Discovery " + string + ":" + serverPort;}}

查看服务

  • 通过浏览器访问

http://localhost:8070/echo/abc

  • 通过Nacos控制台查看  

注册异常

服务消费者Consumer

搭建服务

  • 项目名:nacos-consumer-2.1

  • 添加依赖

org.springframework.bootspring-boot-starter-webcom.alibaba.cloudspring-cloud-starter-alibaba-nacos-discoveryorg.springframework.cloudspring-cloud-starter-openfeign
  • 创建配置文件

#server.port=8071#spring.application.name=service-consumer#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848#端口号server:port: 8071spring:application:name: service-consumer#服务名cloud:nacos:discovery:server-addr: 127.0.0.1:8848 #nacos服务地址

创建服务

  • 创建启动类

package com.czxy;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClient//服务发现public class TestNacosConsumerApplication {public static void main(String[] args) {SpringApplication.run(TestNacosConsumerApplication.class, args );}}
  •  远程调用配置类

package com.czxy.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component;import org.springframework.web.client.RestTemplate;@Componentpublic class RestTemplateConfig {@LoadBalanced //负载均衡@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}}
  •  处理类
package com.czxy.nacos.controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;@RestControllerpublic class TestRestController {@Resourceprivate RestTemplate restTemplate;@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)public String echo(@PathVariable String str) {return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);}}

查询服务

  • 通过浏览器访问

http://localhost:8071/echo/abc

通过Nacos控制台查看