当前位置:网站首页>[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
边栏推荐
- (PKCS1) RSA 公私钥 pem 文件解析
- C language_ Love and hate between string and pointer
- 独立站运营中如何提升客户留存率?客户细分很重要!
- List of Li Bai's 20 most classic poems
- 小样本故障诊断 - 注意力机制代码 - BiGRU代码解析实现
- Vscode topic recommendation
- 根据网络上的视频的m3u8文件通过ffmpeg进行合成视频
- [acnoi2022] I have done it, but I can't
- Qmenu response in pyqt
- Markdown to realize text link jump
猜你喜欢
随机推荐
WPS的JS宏实现图片正文在同一段落的分离方法
Future trends in automated testing
LabVIEW finds prime numbers in an array of n elements
How to implement approval function in Tekton
根据网络上的视频的m3u8文件通过ffmpeg进行合成视频
C language_ Love and hate between string and pointer
App Startup
1279_ Vsock installation failure resolution when VMware player installs VMware Tools
普通token
The article takes you to understand the security of Windows operating system and protect your computer from infringement
11--无重复字符的最长子串
Swift foundation features unique to swift
Use of swift basic closure /block (source code)
pyQt 常用系统的事件
5分钟,客服聊天处理技巧,炉火纯青
你还只知道测试金字塔?
AUTO PWN
Search and recommend those things
自动化测试的未来趋势
dhcp、tftp基础