详解搭建spring cloud的实例Demo,跟着一步一步走,肯定可以的
准备工作:
- 1.首先创建一个父项目,一个普通的java项目
- 2.创建好父项目后,在pom.xml中配置springcloud和springboot的统一版本
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
<springboot.version>2.0.5.RELEASE</springboot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
开始创建spring cloud的注册中心(eureka)
- 1.创建子项目
项目名字随便取(跳过)。。 - 2.创建好子项目后,配置子项目的pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--Eureka服务端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
- 3.在eureka项目中创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class Application_Eureka {
public static void main(String[] args){
SpringApplication.run(Application_Eureka.class);
}
}
- 4.创建配置文件application.yml(名称必须),配置端口和地址
server:
port: 7001
eureka:
instance:
hostname: 127.0.0.1
client:
fetch-registry: false
register-with-eureka: false
- 5.启动main方法,访问 按上面配置 例:127.0.0.1:7001
如果创建一个服务提供者,
- 1.创建项目方式和eureka一致,在父项目下创建子项目,直接配置pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Eureka客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
- 2.创建启动类,提供服务
- 3.启动类
@SpringBootApplication
@EnableEurekaClient
public class Application_Provider {
public static void main(String[] args){
SpringApplication.run(Application_Provider.class);
}
}
- 4.暴露服务
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("provider")
public class ProviderController {
@RequestMapping("get")
public String getData(){
return "提供了数据";
}
}
- 5…创建配置文件application.yml(名称必须)
server:
port: 8001
spring:
application:
name: TEST-PROVIDER #服务名称,随意取
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka #注册到Eureka的地址
- 6.(测试)启动项目,访问 127.0.0.1:8001/provider/get
- 再查看Eureka的页面: 127.0.0.1:7001
有了服务提供者,就创建一个项目来消费
- 1.创建项目方式和上面一致
- 2.配置pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
- 3.创建启动类
@SpringBootApplication
public class Application_Consumer {
public static void main(String[] args){
SpringApplication.run(Application_Consumer.class);
}
}
- 4.创建消费接口
@RestController
@RequestMapping("consumer")
public class ConsumerController {
//资源路径
private final String url = "http://127.0.0.1:8001/provider/get";
@RequestMapping("get")
public String get(){
RestTemplate template = new RestTemplate();
String data = template.getForObject(url, String.class);
return data;
}
}
- 5.配置文件application.yml(名称必须),配置端口
server:
port: 9001
- 6.启动项目,访问: 127.0.0.1:9001/consumer/get 成功获取到了数据
创建spring cloud的网关
- 1.创建子项目,方式和上面的一致
- 2.配置pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--Eureka的客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--Zuul网关的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
- 2.创建启动类
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class Application_Zuul {
public static void main(String[] args){
SpringApplication.run(Application_Zuul.class);
}
}
- 3.配置文件application.yml(名称必须)
server:
port: 9527
spring:
application:
name: ZUUL-SERVER #Zuul网关在Eureka的服务名称,随便取
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka #Eureka的注册地址
zuul:
routes:
provider.serviceId: TEST-PROVIDER #没有提示,自己取名称,值为服务的名称
- 4.启动网关,通过网关访问服务提供
规则:127.0.0.1:9527/网关服务名称(小写)/服务提供者名称(小写)/服务路径
例:127.0.0.1:9527/zuul-server/test-provider/provider/get
- 5.这样的话,路径太长麻烦,不安全,在配置文件中继续配置
server:
port: 9527
spring:
application:
name: ZUUL-SERVER
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
zuul:
routes:
provider.serviceId: TEST-PROVIDER
provider.path: /test/** #匹配指定的路径,资源匹配的路径才会拦截,转发
- 6.重新启动项目
原来路径:127.0.0.1:9527/zuul-server/test-provider/provider/get
网关访问路径变为: 127.0.0.1:9527/test/provider/get
!!!但是我们还可以使用原来的网关地址访问资源,需要给屏蔽掉!!! - 7.修改配置文件
server:
port: 9527
spring:
application:
name: ZUUL-SERVER
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
zuul:
routes:
provider.serviceId: TEST-PROVIDER
provider.path: /test/** #匹配指定的路径,资源匹配的路径才会拦截,转发
ignored-services: "*" #忽略直接使用服务名 *忽略所有的服务
prefix: /server #统一给访问前缀
- 8.访问路径变为: 127.0.0.1:9527/server/test/provider/get
!!!并且原来的网关地址已经不能访问了!!!
fegin的内部调用
我们要在一个服务提供者中使用另一个服务提供者的服务,需要使用fegin进行内部服务的调用
- 1.创建一个项目,就不多说了,和上面一样
- 2.导入依赖,pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--Eureka的客户端的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--Feign的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
- 3.首先创建项目结构、启动类,和配置文件application.yml(名称必须)
启动类:
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients("com.wlg.feign.client") //feign接口的地址(上图的client目录)
public class Application_Feign {
public static void main(String[] args){
SpringApplication.run(Application_Feign.class);
}
}
application.yml
server:
port: 9002
spring:
application:
name: FEIGN-CONSUMER
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
- 4.在client目录下,创建Client接口,映射和方法名建议复制对应的Controller层
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "TEST-PROVIDER") //要调用的服务名称
@RequestMapping("provider") //和调用服务controller的一致
public interface DataClient {
//和调用服务controller的一致
@RequestMapping("get")
public String getData();
}
- 5.在需要使用的地方注入,就可以调用了
import com.wlg.feign.client.DataClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("consumer")
public class DataFeignController {
@Autowired
private DataClient dataClient;
@RequestMapping("feignGet")
public String get(){
return dataClient.getData();
}
}
- 6.启动项目测试一波
127.0.0.1:9002/consumer/feignGet
配置中心(config),熔断器(hystrix),(负载均衡)ribbon
下一章:https://blog.csdn.net/qq_44575680/article/details/106784855