当前位置:网站首页>[micro services ~nacos] Nacos service providers and service consumers
[micro services ~nacos] Nacos service providers and service consumers
2022-06-24 08:30:00 【Taoran】

Here is 【 Microservices ~Nacos】, Pay attention to my learning cloud and don't get lost
If it helps you , Give the blogger a free praise to show encouragement
You are welcome to comment on the collection ️
Column introduction
【 Microservices ~Nacos】 At present, it mainly updates micro services , Learn together and progress together .
Introduction to this issue
This issue mainly introduces micro services ~Nacos
List of articles
Build parent project
Project name :nacos-parent-2.1
Add coordinates
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-build</artifactId>
<version>2.3.5.RELEASE</version>
</parent>
<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 Connection pool -->
<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>
- Project directory structure

Service providers Provider
Build services
Create project :nacos-provider-2.1
Add dependency :
<dependencies>
<!-- web Start class -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos Service discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
- establish yml file
#server.port=8070
#spring.application.name=service-provider
#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# Port number
server:
port: 8070
spring:
application:
name: service-provider # service name
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos Service address
Create services
- Start class

package com.czxy.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // Service discovery
public class TestNacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(TestNacosProviderApplication.class, args );
}
}- Processing class 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;
}
}
View service
- Access... Through a browser

- adopt Nacos Console view

Registration exception

Serving consumers Consumer
Build services
Project name :nacos-consumer-2.1
Add dependency
<dependencies>
<!-- web Start class -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos Service discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- openfeign The remote invocation -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
- create profile

#server.port=8071
#spring.application.name=service-consumer
#spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
# Port number
server:
port: 8071
spring:
application:
name: service-consumer # service name
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos Service address
Create services
- Create startup class

package com.czxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient // Service discovery
public class TestNacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(TestNacosConsumerApplication.class, args );
}
}- Remote call configuration class

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 // Load balancing
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}- Processing class
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);
}
}
Query service
- Access... Through a browser

adopt Nacos Console view

边栏推荐
- 2021-06-24: find the length of the longest non repeating character substring in a string.
- Detailed explanation of etcd backup and recovery principle and actual record of stepping on the pit
- Introduction to RCNN, fast RCNN and fast RCNN
- 基金的募集,交易与登记
- 贷款五级分类
- os.path.join()使用过程中遇到的坑
- 【无标题】
- WCF TCP protocol transmission
- 13 -- remove invalid parentheses
- 疫情下更合适的开发模式
猜你喜欢
随机推荐
jwt(json web token)
Understanding of the concept of "quality"
05-ubuntu安装mysql8
For a detailed explanation of flex:1, flex:1
How to configure networkpolicy for nodeport in kubernetes
新准则金融资产三分类:AMC、FVOCI和FVTPL
transformers PreTrainedTokenizer类
1279_ Vsock installation failure resolution when VMware player installs VMware Tools
软件过程与项目管理期末复习与重点
2021-03-16 COMP9021第九节课笔记
LabVIEW查找n个元素数组中的质数
5分钟,客服聊天处理技巧,炉火纯青
[acnoi2022] I have done it, but I can't
小样本故障诊断 - 注意力机制代码 - BiGRU代码解析实现
2022年制冷与空调设备运行操作上岗证题库及模拟考试
Qmenu response in pyqt
A preliminary study of IO model
Swift extension chainlayout (UI chain layout) (source code)
SQL intra statement operation
MAYA重新拓布
![3D数学基础[十七] 平方反比定理](/img/59/bef931d96883288766fc94e38e0ace.png)







