保姆教程系列一、Linux搭建Nacos
保姆教程系列二、Nacos实现注册中心
保姆教程系列三、Nacos Config–服务配置
请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i、
上篇我们介绍到 保姆教程系列一、Linux搭建Nacos
提示:以下是本篇文章正文内容,下面案例可供参考
IDEA中创建聚合项目nacos作为父工程,其pom.xml如下
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.3.RELEASEversion>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>nacosartifactId>
<version>0.0.1version>
<name>nacosname>
<description>Spring Cloud Nacosdescription>
<properties>
<java.version>1.8java.version>
properties>
<modules>
<module>providermodule>
<module>consumermodule>
modules>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>0.2.2.RELEASEversion>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Greenwich.SR2version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
project>
在父工程Nacos下创建springboot子工程provider,其pom.xml文件为
<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.0modelVersion>
<parent>
<groupId>com.examplegroupId>
<artifactId>nacosartifactId>
<version>0.0.1version>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>providerartifactId>
<version>0.0.1version>
<name>providername>
<description>Provider Nacosdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
在启动类ProviderApplication.java中增加@EnableDiscoveryClient注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient//开启注册
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
配置文件application.yml进行如下配置
#生产者配置
server:
port: 8081
spring:
application:
name: nacos-provider #服务名称
cloud:
nacos:
discovery: #使用注册中心
server-addr: 192.168.36.135:8848 #Nacos访问地址
enabled: true
在服务提供方创建一个对外接口
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/provider")
@RestController
public class IndexController {
@GetMapping("/hello")
public String hello() {
return "我是provider服务生产者";
}
}
在父工程Nacos下创建springboot子工程consumer,其pom.xml文件为:
<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.0modelVersion>
<parent>
<groupId>com.examplegroupId>
<artifactId>nacosartifactId>
<version>0.0.1version>
<relativePath/>
parent>
<groupId>com.examplegroupId>
<artifactId>consumerartifactId>
<version>0.0.1version>
<name>consumername>
<description>Consumer Nacosdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
在启动类ConsumerApplication.java中增加注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient //开启注册
@EnableFeignClients //开启Feign服务
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
配置文件application.yml进行如下配置
#消费者配置
server:
port: 8082
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery: #使用注册中心
server-addr: 192.168.36.135:8848
enabled: true
#开启断路器
feign:
hystrix:
enabled: true
使用FeginClient进行服务调用,hystrix进行熔断
import com.example.consumer.hystrix.HystrixUtils;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "nacos-provider",fallback = HystrixUtils.class)//服务出现异常进行容错
public interface ProviderFeignClient {
/**
* .
* 调用生产者服务
* @return
*/
@GetMapping("/provider/hello")
String hello();
}
HystrixUtils.class容错类
import com.example.consumer.client.ProviderFeignClient;
import org.springframework.stereotype.Component;
@Component
public class HystrixUtils implements ProviderFeignClient {
/**
* .
* 方法重写
*
* @return
*/
@Override
public String hello() {
return "断路器容错,服务开小差了,稍等片刻...";
}
}
service层服务调用
import com.example.consumer.client.ProviderFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class IndexService {
@Autowired
private ProviderFeignClient client; //注入
public String hello() {
return client.hello(); // 服务调用
}
}
在controller中调用service的接口,像一般的接口调用一样
import com.example.consumer.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/consumer")
@RestController
public class IndexController {
@Autowired
private IndexService service;
@GetMapping("/hello")
public String hello(){
return service.hello();
}
}
启动完成后,在服务提供者和消费者的日志中应该可以分别看到如下信息
INFO 18388 --- [ main] o.s.c.a.n.registry.NacosServiceRegistry : nacos registry, nacos-provider 192.168.22.1:8081 register finished
你会发现服务列表中,已经显示了我们刚才创建的两个项目,并可以对其进行简单的监控和管理。
打开浏览器输入:http://localhost:8082/consumer/hello
该处使用的url网络请求的数据。
Nacos服务发现与Eureak服务发现无差异
敬请关注下篇 保姆教程系列三、Nacos Config–服务配置中心
我是南国以南i记录点滴每天成长一点点,学习是永无止境的!转载请附原文链接!!!
参考链接