当前位置:网站首页>[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

边栏推荐
猜你喜欢
随机推荐
App Startup
WPS的JS宏实现图片正文在同一段落的分离方法
5分钟,客服聊天处理技巧,炉火纯青
VsCode主题推荐
蓝桥杯_N 皇后问题
Export MySQL database to xxx SQL, set xxx The SQL file is imported into MySQL on the server. Project deployment.
Fund raising, trading and registration
JVM underlying principle analysis
pyQt 常用系统的事件
Industrial computer anti cracking
os.path.join()使用过程中遇到的坑
Three categories of financial assets under the new standards: AMC, fvoci and FVTPL
李白最经典的20首诗排行榜
Four models of iPhone 13 series have been exposed, and indeed, they are 13 fragrant!
【无标题】
51单片机_外部中断 与 定时/计数器中断
Blue Bridge Cup_ Queen n problem
Question bank and simulation examination for operation certificate of refrigeration and air conditioning equipment in 2022
3D数学基础[十七] 平方反比定理
Future trends in automated testing




![3D数学基础[十七] 平方反比定理](/img/59/bef931d96883288766fc94e38e0ace.png)



