SpringCloud整合Alibaba环境搭建
Posted 潮汐先生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud整合Alibaba环境搭建相关的知识,希望对你有一定的参考价值。
SpringCloud整合Alibaba环境搭建
准备工作
环境要求
JDK 1.8.+、Maven 3.6.3、springboot 2.2.5、springcloud Hoxton+、springcloud alibaba 2.2.1.RELEASE
整合思路
服务注册中心 nacos
统一配置中心 nacos
服务熔断组件 sentinel
服务间通信组件 openfeign + ribbon
服务网关组件 gateway
启动服务
在上面整个思路里面可以看到我们需要nacos
和sentinel
。所以在开始搭建之前我们先启动这两个服务。如下图所示
搭建步骤
初始化parent
1.新建parent
microservices
microservices
作为项目空间用作隔离其他的项目,仅仅是一个EmptyProject
.
springcloud_alibaba_parent
我们在项目空间中新建一个Module – springcloud_alibaba_parent
作为我们微服务框架的父项目,用来管理依赖及其版本
2.配置parent
maven
首先我们更改maven的路径为本地(一般新建项目都需要检查),如下图所示:
Log Support
在Other Settings
中找到Log Support
,修改其Framework
为slf4j
。如下图所示:
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.christy</groupId>
<artifactId>springcloud_alibaba_parent</artifactId>
<version>1.0-SNAPSHOT</version>
<!--继承springboot父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<spring.cloud.version>Hoxton.SR6</spring.cloud.version>
<spring.cloud.alibaba.version>2.2.1.RELEASE</spring.cloud.alibaba.version>
</properties>
<!--维护依赖-->
<dependencyManagement>
<dependencies>
<!--维护springcloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>$spring.cloud.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--维护springcloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>$spring.cloud.alibaba.version</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
初始化common
1.新建Module
springcloud_alibaba_common
作为公共依赖(暂时不做处理),主要存放后期的entity
或者utils
。我们新建一个Module,父项目选择上面的parent。如下图所示:
初始化users
springcloud_alibaba_users
与springcloud_alibaba_products
作为本次微服务框架搭建的两个主要示例,主要展示在users中调用product中的方法来展示微服务间的调用。首先我们来看下springcloud_alibaba_users
的搭建
1.新建Module
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud_alibaba_parent</artifactId>
<groupId>com.christy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud_alibaba_users</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
3.application.properties
server.port=8881
spring.application.name=USERS
# 注册到nacos
spring.cloud.nacos.server-addr=192.168.8.190:8848
4.UserApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author Christy
* @Date 2021/8/27 9:49
**/
@SpringBootApplication
@EnableDiscoveryClient
public class UserApplication
public static void main(String[] args)
SpringApplication.run(UserApplication.class, args);
5.启动
我们启动该模块,然后在nacos的服务管理列表中能够看到该服务,则说明我们的springcloud_alibaba_users
初始搭建是没有问题的。
初始化products
1.新建Module
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud_alibaba_parent</artifactId>
<groupId>com.christy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud_alibaba_products</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
3.application.properties
server.port=8882
spring.application.name=PRODUCTS
# 注册到nacos
spring.cloud.nacos.server-addr=192.168.8.190:8848
4.ProductApplicatioon.java
package com.christy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author Christy
* @Date 2021/8/27 9:56
**/
@SpringBootApplication
@EnableDiscoveryClient
public class ProductApplication
public static void main(String[] args)
SpringApplication.run(ProductApplication.class, args);
5.启动
我们启动该模块,然后在nacos的服务管理列表中能够看到该服务,则说明我们的springcloud_alibaba_products
初始搭建是没有问题的。
服务间调用
上面我们的USERS
和PRODUCTS
两个服务都已经启动成功了并且成功注册到了nacos
,下面我们就要实现USERS
调用PRODUCTS
中的方法来实现微服务间的调用。
PRODUCTS
首先针对PRODUCTS,我们针对其ProductController
实现如下方法:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author Christy
* @Date 2021/8/27 10:03
**/
@RestController
@RequestMapping("product")
public class ProductController
private static final Logger log = LoggerFactory.getLogger(ProductController.class);
@Value("$server.port")
private int port;
@GetMapping("port")
public String product()
log.info("hello product!");
return "hello product! this port is: "+port;
USERS
下面我们就要实现如何在USERS
中调用PRODUCTS
中的/product/port
。在学习springcloud的时候我们知道通过ribbon+restTemplate
的方式可以实现微服务间的通信。我们首先看下该种方法下的通信实现
ribbon+restTemplate
1.BeansConfig
我们在USERS
中的config
包下新建BeansConfig
,在里面将RestTemplate
交给Spring管理。通过ribbon
的注解@LoadBalance
实现负载均衡。代码如下:
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @Author Christy
* @Date 2021/8/27 10:09
**/
@Configuration
public class BeansConfig
@Bean
@LoadBalanced
public RestTemplate restTemplate()
return new RestTemplate();
2.UserController
在UserController
中我们在invoke
方法中通过RestTemplate
调用PRODUCTS
中的方法。代码如下:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @Author Christy
* @Date 2021/8/27 10:14
**/
@RestController
@RequestMapping("user")
public class UserController
private static final Logger log = LoggerFactory.getLogger(UserController.class);
@Autowired
private RestTemplate restTemplate;
@RequestMapping("invoke")
public String invokeProduct()
log.info("Hello users!");
String result = restTemplate.getForObject("http://PRODUCTS/product/port", String.class);
log.info("result is ", result);
return result;
3.测试
重新启动USERS
和PRODUCTS
,在浏览器中访问http://localhost:8881/user/invoke
。结果如下图所示:
我们说这种方式不好:他将路径写死在了代码中,不利于维护。所以后来我们讲到了OpenFeign
OpenFeign
我们来修改下USERS
利用OpenFeign
实现微服务间的通信。想要使用OpenFeign必须先引入相关依赖
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">
<parent>
<artifactId>springcloud_alibaba_parent</artifactId>
<groupId>com.christy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud_alibaba_users</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- OpenFeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
2.UserApplication.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;
/**
* @Author Christy
* @Date 2021/8/27 9:49
**/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //开启OpenFeign调用
public class UserApplication
public static void main(String[] args)
SpringApplication.run(UserApplication.class, args);
3.ProductClient.java
我们在USERS中的feignclients包中新建ProductClient.java
。内容如下:
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Author Christy
* @Date 2021/8/27 10:37
**/
@FeignClient("PRODUCTS")
public interface ProductClient
@RequestMapping("/product/port")
String product();
4.UserControlller.java
import com.christy.feignclients.ProductClient;
以上是关于SpringCloud整合Alibaba环境搭建的主要内容,如果未能解决你的问题,请参考以下文章
springcloud 微服务Spring Cloud Alibaba整合Sentinel详解