Spring Cloud学习笔记—基于Nacos的服务注册与发现实操练习

Spring Cloud学习笔记—基于Nacos的服务注册与发现实操练习


关于Nacos的安装与配置不在本文范围中,详情请参看Nacos官网: https://nacos.io/

1、使用eclipse-STS创建服务提供程序

1、打开eclipse-sts,点击File->New->Spring Starter Project,出现新建向导对话框,如下图:
在这里插入图片描述
Name输入NacosServiceProvider1
Group输入wongoing
Artifact输入ms
Package输入com.wongoing
2、点击"Next"按钮,出现下图:
在这里插入图片描述
勾选Spring Web
3、点击"Finish"按钮,完成工程创建
4、修改pom.xml,增加以下依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>2.2.3.RELEASE</version>
</dependency>	

5、修改pom.xml中Spring boot的版本与nacos-discovery版本一致,完整pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>wongoing</groupId>
	<artifactId>ms</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>NacosServiceProvider1</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
		    <groupId>com.alibaba.cloud</groupId>
		    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
		    <version>2.2.3.RELEASE</version>
		</dependency>		

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</pluginRepository>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

</project>

6、在spring boot启动类中增加@EnableDiscoveryClient注解,代码如下:

package com.wongoing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosServiceProvider1Application {

	public static void main(String[] args) {
		SpringApplication.run(NacosServiceProvider1Application.class, args);
	}

}

7、新建一个包com.wongoing.controller,在此包下创建一个类TestController.java,代码如下:

package com.wongoing.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/test")
@RestController
public class TestController {

	@RequestMapping(value="/echo/{str}")
	public String echo(@PathVariable String str) {
		return "Hello Nacos Discovery " + str;
	}
}

8、重命名src/main/resources/application.properties为src/main/resources/application.yml,完整内如下:

server:
  port: 9001
spring:
  application:
    name: service-provider-1
    
  cloud:
    nacos:
      discovery:
        server-addr: 121.36.94.212:8848

9、启动此项目,启动后可以在Nacos管理控制台中看到下图:
在这里插入图片描述

2、使用eclipse-STS创建服务消费程序

1、在eclipse-sts中点击File->New->Spring Starter Project,如下图:
在这里插入图片描述
Name输入NacosServiceConsumer1
Group输入wongoing
Artifact输入ms
Package输入com.wongoing
2、点击"Next"按钮,出现下图:
在这里插入图片描述
选择依赖Spring Web
3、点击“Finish"按钮完成工程创建。
4、修改pom.xml,增加以下依赖:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>2.2.3.RELEASE</version>
</dependency>

5、修改pom.xml中Spring boot的版本与nacos-discovery版本一致,完整pom.xml代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>wongoing</groupId>
	<artifactId>ms</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>NacosServiceConsumer1</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
		    <groupId>com.alibaba.cloud</groupId>
		    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
		    <version>2.2.3.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
		<repository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</pluginRepository>
		<pluginRepository>
			<id>spring-snapshots</id>
			<name>Spring Snapshots</name>
			<url>https://repo.spring.io/snapshot</url>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

</project>

6、修改spring boot启动类,增加@EnableDiscoveryClient注解,并增加一个RestTemplate的Bean,完整代码如下:

package com.wongoing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosServiceConsumer1Application {

	@LoadBalanced
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
	public static void main(String[] args) {
		SpringApplication.run(NacosServiceConsumer1Application.class, args);
	}
}

7、创建一个包com.wongoing.service.remote,在此包下创建一个类TestService.java,代码如下:

package com.wongoing.service.remote;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class TestService {
	
	@Autowired
	private RestTemplate restTemplate;
	
	public String echo(String str) {
		return this.restTemplate.getForObject("http://service-provider-1/test/echo/" + str, String.class);
	}
}

8、创建一个包com.wongoing.controller,在此包下创建一个类TestController.java,代码如下:

package com.wongoing.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wongoing.service.remote.TestService;

@RestController
public class TestController {

	@Autowired
	private TestService service;
	
	@RequestMapping("/test")
	public String test() {
		return this.service.echo("wongoing");
	}
}

9、启动此项目,并打开浏览器在地址栏中输入:http://localhost:8080/test,出现下图:
在这里插入图片描述
说明在访问service-consumer-1中的一个功能时,service-consumer-1又调用了service-provider-1的功能,并调用成功!此案例模拟的服务注册(服务提供者)与服务发现(服务消费者)的调用实现。

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页