SpringCloud简单入门测试以及注解介绍

SpringCloud简述

SpringCloud是一个微服务框架 , 相比于Dubbo等RPC框架 , SpringCloud提供的全套的分布式系统解决方案 .

SpringCloud对微服务基础框架Netflix的多个开源组件进行了封装 , 同时又实现了和云端平台以及和SpringBoot开发框架的集成 .

SpringCloud为微服务框架开发涉及的配置管理 , 服务治理 , 熔断机制 , 智能路由 , 微代理 , 控制总线 , 一次性token , 全局一次性锁 , leader选举 , 分布式session , 集成状态管理等操作提供了一种简单的开发方式 .

SpringCloud为开发者提供了快速构建分布式系统的工具 , 开发者可以快速地启动服务或构建应用 , 同时能快速和云平台资源进行对接 .

简而言之
SpringCloud就是致力于分布式系统 , 云服务的框架 ;
SpringCloud为开发人员提供了快速构建分布式系统中一些常见的模式工具 ; SpringCloud与SpringBoot
SpringCloud基于SpringBoot来进行构建服务 , 这样开发组件时 , 就能依托SpringBoot来实现快速开发 ;

SpringCloud常用注解

@SpringBootApplication是SpringBoot的启动类 , 包括三个注解 , 及其作用 ;

@Configuration : 表示该类作用SpringBooot的配置文件类 ;

@EnableAutoConfiguration : 表示程序启动时 , 自动加载SpringBoot默认的配置 ;

@ComponentScan : 表示程序启动时 , 自动扫描当前包及子包下的所有类 ;

@EnableDiscoveryClient & EnableEurekaClient :

@EnableDiscoveryClient 是基于 spring-cloud-commons , @EnableEurekaClient 是基于spring-cloud-Netflix , 如果选用的注册中心是Eureka , 那么就推荐使用@EnableEurekaClient ,
如果是其他的注册中心 , 那么推荐使用@EnableDiscoveryClient ;

@ComponentScan & @Configuration & @EnableAutoConfiguration :
@ComponentScan 如果不设置basePackage的话会默认扫描所有的类 , 所有最好还是写basePackage ;

@Configuration 表示这个类定义了Bean , 会把这个类中的Bean加载到Spring容器中 ;

@EnableAutoConfiguration 表示会在你开启某个功能的时候自动配置 , 这个注解告诉SpringBoot根据添加的jar依赖猜测你会如何配置Spring , 由于spring-boot-start-web添加了
Tomcat和SpringMVC所以AutoConfiguration会假定你正在开发一个web应用 , 并对spring进行一个相应的设置 ;

@Mapper & @MapperScan :
Mapper类上添加@Mapper注解 , 这种方式要求每一个Mapper类都要添加此注解 ;
使用@MapperScan注解 , 可以指定要扫描的Mapper类的包的路径 ;

@Bean & @Configuration :
@Bean标注在方法上 , 等价于spring的xml配置文件中的 , 作用为注册bean对象 ;

@Configuration标注在类上 , 相当于把该类作为springxml配置文件中的 ,作用为配置spring容器 (应用上下文) ;

@LoadBalanced
SpringCloud 的 Commons模块提供了一个@LoadBalanced注解 , 方便我们RestTemlate添加了一个LoadBalancerClient , 以实现客户端的负载均衡 ;

搭建注册服务中心eureka

项目我使用的是maven在线构建工具直接成的,注意版本号就行。然后就会创建一个镜像文件,我们导入idea。

添加eureka的pom依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

在这里插入图片描述
注意版本号就行

配置yml

在这里插入图片描述

#端口号
server.port=1234
# eureka.client.registerWithEureka :表示是否将自己注册到Eureka Server,默认为true。
# 由于当前这个应用就是Eureka Server,故而设为false
eureka.client.register-with-eureka=false
# eureka.client.fetchRegistry :表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,
# 不需要同步其他的Eureka Server节点的数据,故而设为false。
eureka.client.fetch-registry=false
#本地
eureka.instance.hostname=localhost
# eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

启动类

在这里插入图片描述

@EnableEurekaServer这个注释的意思 上面我们总结了一些springcloud的注解可以去看看,这里声明此处为服务注册中心

最后我们访问路径 http://localhost:1234 端口号要对应我们yml文件中的port,如果你访问的结果是这样,那就说eureka注册服务中心搭建成功了
在这里插入图片描述

搭建服务端生产者

项目构建和上面的一致,可以选择拷贝服务中心项目或者用构建工具生成。

pom依赖

在这里插入图片描述

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<!-- sping cloud 注册服务 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

配置yml

在这里插入图片描述
name=compute-server可以随便取 没啥要求 但是注册中心要和之前的url对应上,端口号也不能和注册中心的一样,之前的是1234,我这里用的是1111

#服务名称
spring.application.name=compute-service1
#端口号
server.port=1111
#注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1234/eureka/
spring.cloud.config.discovery.enabled=true
#注册中心的服务id
spring.cloud.config.discovery.serviceId=compute-server

启动类

在这里插入图片描述
这里我们做一个小测试,这里我们为了方便获取值,就用@RestController注解,访问路径 http://localhost:1111/hi?name=fzx 这里1111端口号是刚刚配置文件配置的,hi是我们访问哪个方法,name是我们的参数。
在这里插入图片描述访问成功会就是酱紫,这时候我们可以在注册服务中心里看到,我们的生产者名字和端口号等等
在这里插入图片描述

搭建消费者

pom依赖

在这里插入图片描述

<dependencies>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-ribbon</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<!--断路器-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>

		<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>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

yml配置

在这里插入图片描述

eureka.client.serviceUrl.defaultZone=http://localhost:1234/eureka/
server.port=2222
spring.application.name=service-ribbon

启动类

在这里插入图片描述

@EnableDiscoveryClient
@SpringBootApplication
public class SpringBoot2Application {

	public static void main(String[] args) {

		SpringApplication.run(SpringBoot2Application.class, args);
	}


	@Bean
	@LoadBalanced
	RestTemplate restTemplate(){
		return new RestTemplate();
	}
}

写一个controller
在这里插入图片描述
这里也需要注意一个点@RequestMapping中的名字 要和生产者的一致 不然就要报错

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @RequestMapping("/hi")
    public String hello(@RequestParam String name) {

        return helloService.hiService(name);
    }
}

写一个service
在这里插入图片描述

这个名字compute-service1很坑爹 之前做的时候出现bug,花了好久才解决,这个名字要对应我们生产者的名字 这个时候我们可以看看之前生产者的配置文件。原因是当我们访问消费者的时候,Controller收到请求,并调用Service中的方法,Service 中通过定义的restTemplate 去调用生产者也就是上面框框,如果名字不对 那就找不到报错了

测试

启动项目运行测试 访问路径 thttp://localhost:2222/hiname=fzx
在这里插入图片描述
这里可以看到,我们调用的端口是消费者的,但是实用的方法是生产者,说明我们消费者中定义的RestTemplate成功调用了生产者中的方法。
在这里插入图片描述
不管是服务提供端还是消费端都可以在注册服务中心看到自己的信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值