spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等html
Springboot集中声明web
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> </parent>
Springcloud版本spring
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
主项目管理的依赖Jar:架构
<dependencies> <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>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Springboot组件:app
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
一个model工程做为服务注册中心,即Eureka Server,另外一个做为Eureka Client。jsp
引入主项目:maven
<parent> <groupId>com.fsdm</groupId> <artifactId>SpringCloud_test1</artifactId> <version>1.0-SNAPSHOT</version> </parent>
引入spring-cloud-starter-netflix-eureka-server的依赖:分布式
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
Application启动类:spring-boot
@EnableEurekaServer @SpringBootApplication public class Demo1Application { public static void main(String[] args) { SpringApplication.run(Demo1Application.class, args); } }
注解解析:工具
@SpringBootApplication
1. 复合注解主要包括@ComponentScan,和@SpringBootConfiguration,@EnableAutoConfiguration。
2. @SpringBootConfiguration标注当前类是配置类
3. @EnableAutoConfiguration启动自动的配置,根据你添加的jar包来配置你项目的默认配置
4. @EnableAutoConfiguration扫描当前包及其子包下被@Component,@Controller,@Service,@Repository注解标记的类并归入到spring容器中进行管理
@EnableEurekaServer
1. 代表这是一个EurekaServer(服务注册中心)
2. 配合yml文件使用:
Eureka:
Client:
registerWithEureka: false fetchRegistry: false
yml配置:
server: port: 8761 eureka: instance: hostname: localhost client: # 代表本身是一个eureka server. registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ spring: application: name: eurka-server
启动程序,访问http://localhost:8761/
当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每一个client实例接收心跳消息。 若是心跳超时,则一般将该实例从注册server中删除。
No application available 表示没有服务被发现
由于咱们尚未注册服务固然没有发现服务啦,
项目架构:
引入主项目:
<parent> <groupId>com.fsdm</groupId> <artifactId>SpringCloud_test1</artifactId> <version>1.0-SNAPSHOT</version> </parent>
须要的jar以及组件:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Application启动类:
@SpringBootApplication @EnableEurekaClient @RestController public class Demo2Application { public static void main(String[] args) { SpringApplication.run(Demo2Application.class, args); } @Value("${server.port}") String port; @RequestMapping("/hi") public String home(@RequestParam String name) { return "hi "+name+",i am from port:" +port; } }
注解解析:
@EnableEurekaClient
1. 代表这是一个EurekaClient(服务提供者)
2. 基于spring-cloud-netflix,只能为eureka做用。
@RestController
1. 复合注解主要包括@Controller和@ResponseBody
2. 标注controller层,可供url访问
3. 没法返回jsp页面,或者html,配置的视图解InternalResourceViewResolver不起做用,返回的内容就是Return 里的内容。
yml配置:
server: port: 8762 spring: application: #工程名称 name: service-hi eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
须要指明spring.application.name,这个很重要,这在之后的服务与服务之间相互调用通常都是根据这个name 。
启动这个项目,刷新http://localhost:8761/
发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为8762
你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI
关于红色字体的警告解释:
https://www.cnblogs.com/breath-taking/articles/7940364.html
访问http://localhost:8762/hi?name=fsdm
未完,待续。。。