@EnableDiscoveryClient
@EnableFeignClients
public class SpringRestConsumerBootstrap {
public static void main(String[] args) {
SpringApplication.run(SpringRestConsumerBootstrap.class, args);
}
}
总结:通过上面的学习,我们已经了解Spring cloud的微服务是如何协作的,通过哪些组件的配合能够完成服务间协作?我们了解了什么是负载均衡,Feign用于服务间Http调用,Ribbon用于执行负载均衡算法选取访问实例,而Ribbon的实例列表来源是由Spring cloud的服务发现中心提供(当前实现为Nacos),更详细的内容请学习Spring Cloud的相关课程 。
[](()1.2、搭建Nacos服务端
做nacos服务端的下载及安装操作。
[](()1.3、创建父工程
为了规范依赖的版本,这里创建父工程,指定依赖的版本。
父工程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 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
com.demo.nacos
nacos-discovery
1.0-SNAPSHOT
pom
<project.build.sourceEncoding>UTF‐8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF‐8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.1.0.RELEASE
pom
import
org.springframework.cloud
spring-cloud-dependencies
Greenwich.RELEASE
pom
import
org.springframework.boot
spring-boot-dependencies
2.1.3.RELEASE
pom
import
org.springframework.boot
spring-boot-maven-plugin
[](()1.4、服务生产者
创建生产者模块
以下步骤演示了如何将一个服务生产者注册到 Nacos。
- 1、pom.xml的配置。
包括Spring Cloud Feign组件、Spring Cloud Alibaba Nacos Discovery组件以及Spring boot web相关组件依赖。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
nacos-discovery
com.demo.nacos
1.0-SNAPSHOT
4.0.0
quickstart-provider
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
- 2、application.yml配置。一些关于 Nacos 基本的配置也必须在application.yml(也可以是application.properties)
配置,如下所示: application.yml
server:
port: 56010 # 启动端口
spring:
application:
name: quickstart-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
logging:
level:
root: info
org.springframework: info
Note: spring.cloud.nacos.discovery.server-addr 指定了Nacos Server的网络地址和端口号。
- 3、启动Provider(生产者)
package com.demo.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
-
Description:
-
DATE: 2022/1/26 17:14
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosProviderApp {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApp.class,args);
}
}
- 4、Provider(生产者)服务实现
package com.demo.nacos.controller;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.logging.Logger;
/**
-
Description:
-
DATE: 2022/1/26 17:15
*/
@RestController
public class ProviderController {
private static final Logger LOG = (Logger) LoggerFactory.getLogger(ProviderController.class);
@GetMapping(“/servicre”)
public String service(){
《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 LOG.info(“provider invoke”);
return “provider invoke”;
}
}
[](()1.5、服务消费者
1、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 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
nacos-discovery
com.demo.nacos
1.0-SNAPSHOT
4.0.0
quickstart-consumer
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
2、application.yml配置
server:
port: 56020 # 启动端口
spring:
application:
name: quickstart-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
logging:
level:
root: info
org.springframework: info
3、Consumer(消费者)启动类
package com.demo.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
-
Description:
-
DATE: 2022/1/26 17:42
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerApp {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApp.class, args);
}
}
4、Provider(生产者)远程代理定义
package com.demo.nacos.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
-
Description:
-
DATE: 2022/1/26 17:36
*/
@FeignClient(value = “quickstart-provider”)