当前位置:网站首页>【微服务~Nacos】Nacos服务提供者和服务消费者
【微服务~Nacos】Nacos服务提供者和服务消费者
2022-07-28 22:23:00 【心动的偏执】
??这里是【微服务~Nacos】,关注我学习云原生不迷路
??如果对你有帮助,给博主一个免费的点赞以示鼓励
欢迎各位??点赞??评论收藏
??专栏介绍
【微服务~Nacos】 目前主要更新微服务,一起学习一起进步。
??本期介绍
本期主要介绍微服务~Nacos
文章目录
搭建父项目
项目名:nacos-parent-2.1
添加坐标
org.springframework.cloud spring-cloud-build 2.3.5.RELEASE<properties> <spring.cloud.version>Hoxton.SR12</spring.cloud.version> <spring.cloud.alibaba.version>2.2.7-SNAPSHOT</spring.cloud.alibaba.version> <mybatis.plus.starter.version>3.4.0</mybatis.plus.starter.version> <durid.starter.version>1.1.10</durid.starter.version> <swagger.version>2.7.0</swagger.version> <jwt.jjwt.version>0.9.0</jwt.jjwt.version> <jwt.joda.version>2.9.7</jwt.joda.version> </properties> <dependencyManagement> <dependencies> <!-- Spring 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.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.7.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis.plus.starter.version}</version> </dependency> <!-- Druid连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${durid.starter.version}</version> </dependency> <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build>项目目录结构

服务提供者Provider
搭建服务
创建项目:nacos-provider-2.1
添加依赖:
org.springframework.boot spring-boot-starter-web<!-- nacos 服务发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies>创建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;
@RestController
public class EchoController {
@Resource
private HttpServletRequest request;
@GetMapping("/echo/{string}")
public String echo(@PathVariable String string) {
int serverPort = request.getServerPort();
return "Hello Nacos Discovery " + string + ":" + serverPort;
}
}
查看服务
- 通过浏览器访问

- 通过Nacos控制台查看

注册异常

服务消费者Consumer
搭建服务
项目名:nacos-consumer-2.1
添加依赖
org.springframework.boot spring-boot-starter-web<!-- nacos 服务发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- openfeign 远程调用 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>创建配置文件

#server.port=8071
#spring.application.name=service-consumer
#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
#端口号
server:
port: 8071
spring:
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;
@Component
public class RestTemplateConfig {
@LoadBalanced //负载均衡
@Bean
public 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;
@RestController
public class TestRestController {
@Resource
private 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); }}
查询服务
- 通过浏览器访问

通过Nacos控制台查看


边栏推荐
- 【TA-霜狼_may-《百人计划》】图形3.6 纹理压缩——包体瘦身术
- Sword finger offer 64. find 1+2+... +n, logical operator short circuit effect
- JS高级 之 ES6~ES13 新特性
- Event extraction and documentation (2008-2017)
- Those "experiences and traps" in the data center
- 1-4 类的复习
- 动态规划问题(七)
- Introduction and solution of common security vulnerabilities in web system CSRF attack
- IDEA报错Error running ‘Application‘ Command line is too long解决方案
- EN 1935 building hardware. Single axis hinge - CE certification
猜你喜欢

How NAT configures address translation

JS高级 之 ES6~ES13 新特性

“Method Not Allowed“,405问题分析及解决

CV instance segmentation model sketch (1)

Android studio connects to MySQL and completes simple login and registration functions

Develop effective Tao spell

Have passed hcip and joined the company of your choice, and share the learning experience and experience of Huawei certification

How can Plato obtain premium income through elephant swap in a bear market?

[TA frost wolf _may- "hundred people plan"] art 2.2 model basis

Idea2021.2 installation and configuration (continuous update)
随机推荐
JS advanced ES6 ~ es13 new features
Immutable x officially opens IMX token pledge detailed IMX pledge introduction optimistic about the development prospect of IMX
1-8 basic use of props
递归/回溯刷题(中)
Introduction and solution of common security vulnerabilities in web system CSRF attack
动态规划问题(三)
How NAT configures address translation
Concurrency in go
CV instance segmentation model sketch (1)
Es6操作教程
Oracle超全SQL,细节狂魔
Develop effective Tao spell
What is in word?:^ p
Doip test development practice
1-6 state and binding events
Dual for loop optimization
Multi sensor fusion positioning (I) -- 3D laser odometer
[TA frost wolf \u may - "hundred people plan"] Figure 3.6 texture compression - inclusion slimming
mysql索引失效的常见9种原因详解
ACM SIGIR 2022 | interpretation of selected papers of meituan technical team