前篇:项目结构及说明
本文搭建一个 SpringCloudAlibaba 真实项目环境,基于nacos 的微服务项目,使用openFeign 客户端,openFeign默认使用的Ribbon 来实现负载均衡
1、本文环境使用
Nacos : 注册中心
openFeign :rpc 调用工具(默认集成了 Ribbon)
Nacos 环境搭建参考:https://blog.csdn.net/qq_41463655/article/details/104002968
2、SpringCloudAlibaba 技术栈一览
Spring Cloud Alibaba Nacos 服务注册
Spring Cloud Alibaba Nacos 分布式配置中心
Spring Cloud Alibaba Sentinel服务保护
SpringCloud Alibaba Seata分布式事务解决框架
Alibaba Cloud OSS 阿里云存储
Alibaba Cloud SchedulerX 分布式任务调度平台
Alibaba Cloud SMS 分布式短信系统
3、当前项目结构
spring-cloud-alibaba-demo
------alibaba-api
------------alibaba-server-api
------------alibaba-client-api
------alibaba-api-impl
------------alibaba-client-api-impl
------------alibaba-server-api-impl
已下是代码实现
一、spring-cloud-alibaba-demo (父工程 POM)
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.0.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>spring-cloud-alibaba-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-cloud-alibaba-demo</name><description>Demo project for Spring Boot</description><packaging>pom</packaging><properties><java.version>1.8</java.version></properties><modules><module>alibaba-api</module><module>alibaba-api/alibaba-client-api</module><module>alibaba-api/alibaba-server-api</module><module>alibaba-api-impl</module><module>alibaba-api-impl/alibaba-client-api-impl</module><module>alibaba-api-impl/alibaba-server-api-impl</module></modules><dependencies><!-- springboot 整合web组件--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- nacos 注册中心 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>0.2.2.RELEASE</version></dependency><!-- openfeign 客户端(rpc) --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>2.0.0.RELEASE</version></dependency><!-- --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
二、alibaba-api (接口父工程)
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"><parent><artifactId>spring-cloud-alibaba-demo</artifactId><groupId>com.example</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>alibaba-api</artifactId><packaging>pom</packaging>
</project>
三、alibaba-api-impl(接口实现父工程)
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"><parent><artifactId>spring-cloud-alibaba-demo</artifactId><groupId>com.example</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>alibaba-api-impl</artifactId><packaging>pom</packaging></project>
四、alibaba-server-api(接口子工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><artifactId>alibaba-api</artifactId><groupId>com.example</groupId><version>0.0.1-SNAPSHOT</version></parent><artifactId>alibaba-server-api</artifactId><version>0.0.1-SNAPSHOT</version><name>alibaba-server-api</name><description>Demo project for Spring Boot</description>
</project>
接口 UserService
public interface UserService {/*** TODO 查询用户信息*/@GetMapping("/getUserId")public String getUserId(@RequestParam("userId") String userId);
}
五、alibaba-client-api(接口子工程2-消费者)
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><artifactId>alibaba-api</artifactId><groupId>com.example</groupId><version>0.0.1-SNAPSHOT</version></parent><groupId>com.example</groupId><artifactId>alibaba-client-api</artifactId><version>0.0.1-SNAPSHOT</version><name>alibaba-client-api</name><description>Demo project for Spring Boot</description>
</project>
接口 LoginService
package com.example.alibabaclient.server;import org.springframework.web.bind.annotation.GetMapping;public interface LoginService {/*** TODO 登录*/@GetMapping("/login")public String login();}
六、alibaba-server-api-impl (实现子工程-生产者)
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><artifactId>alibaba-api-impl</artifactId><groupId>com.example</groupId><version>0.0.1-SNAPSHOT</version></parent><artifactId>alibaba-server-api-impl</artifactId><version>0.0.1-SNAPSHOT</version><name>alibaba-server-api-impl</name><description>Demo project for Spring Boot</description><dependencies><dependency><groupId>com.example</groupId><artifactId>alibaba-server-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>
application.yml
server:port: 8082
spring:application:## 服务名称name: alibaba-servercloud:nacos:discovery:## 服务注册地址server-addr: 192.168.177.128:8848
实现接口
package com.example.alibabaserver.service;import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
// MediaType.APPLICATION_JSON_UTF8_VALUE 表示返回json字符串
@RestController
@RequestMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UserServiceImpl implements UserService{@Value("${server.port}")private String port;public String getUserId(@RequestParam("userId") String userId){return "userId:"+userId+ "|| port:"+port;}
}
启动类
@SpringBootApplication
class AlibabaServerApplication {public static void main(String[] args) {SpringApplication.run(AlibabaServerApplication.class, args);}}
七、alibaba-client-api-impl (实现子工程-消费者)
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><artifactId>alibaba-api-impl</artifactId><groupId>com.example</groupId><version>0.0.1-SNAPSHOT</version></parent><artifactId>alibaba-client-api-impl</artifactId><version>0.0.1-SNAPSHOT</version><name>alibaba-client-api-impl</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>com.example</groupId><artifactId>alibaba-server-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>com.example</groupId><artifactId>alibaba-client-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.yml
server:port: 8081
spring:application:## 服务名称name: alibaba-clientcloud:nacos:discovery:## 服务注册地址server-addr: 192.168.177.128:8848
openFeign 客服端接口
package com.example.alibabaclient.feign;import com.example.alibabaserver.service.UserService;
import org.springframework.cloud.openfeign.FeignClient;
/*** TODO alibaba-server = 服务名* 直接依赖接口项目依赖,减少代码量,使实现类可以直接使用* @return*/
@FeignClient("alibaba-server")
public interface UserServiceFeign extends UserService {}
实现接口
package com.example.alibabaclient.server;import com.example.alibabaclient.feign.UserServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;@RestController
public class LoginServiceImpl implements LoginService {@Autowiredprivate UserServiceFeign userServiceFeign;@Overridepublic String login() {String result = userServiceFeign.getUserId("123456789");System.out.println("调用alibaba-server服务,result:" + result);return result;}
}
启动类
添加 @EnableFeignClients 注解
package com.example.alibabaclient;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableFeignClients
class AlibabaClientApplication {public static void main(String[] args) {SpringApplication.run(AlibabaClientApplication.class, args);}
}
八、测试
启动 alibaba-server-api-impl 两次,集群(第二次启动修改端口号)
启动 alibaba-client-api-impl
访问 alibaba-client-api-impl 的 login 接口,采用openFeign 访问成功