1.spring cloud
spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等。它运行环境简单,可以在开发人员的电脑上跑。另外说明spring cloud是基于springboot的,所以需要开发中对springboot有一定的了解
2. Eureka服务注册中心
使用spring cloud,我们需要用的的组件上Spring Cloud Netflix的Eureka ,eureka是一个服务注册和发现模块
eureka有点像dubbo和zookeeper的集合体, 它既能是注册中心,又能是客户端
3. 注册中心创建
3.1 创建maven主工程 spring-cloud-demo
3.2 创建model子工程 eurekaServer
右键主工程->创建model-> 选择spring initialir 如下图
下一步下一步
选择cloud discovery->eureka server
创建后会生成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.zgd.springcloud</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.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>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
spring boot的main启动类 , 加上@EnableEurekaServer
表示是一个eureka server
package com.zgd.springcloud.eurekaServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* eureka server 服务器
* @author zgd
*/
@EnableEurekaServer
@SpringBootApplication
public class ServerApp {
public static void main(String[] args) {
SpringApplication.run(ServerApp.class);
}
}
application.yml配置
server:
port: 8761
eureka:
instance:
hostname: localhost
#当前eureka server 即是服务器又是客户端,所以需要作为客户端进行配置
client:
# 是否注册到eureka false表明了自己就是eureka server
registerWithEureka: false
fetchRegistry: false
# 注册中心的url地址,这里其实就是自己
serviceUrl:
defaultZone : http://${eureka.instance.hostname}:${server.port}/eureka/
现在就可以访问
localhost:8761
可以看到管理页面 , 上面一串红字,不要紧,application.yml给应用取名就好了
spring:
application:
name: server
3.3 创建client客户端
按照上面创建eurekaServer的方式, 同样创建子模块eureka-client-01
不同的就是application.yml
server:
port: 8081
eureka:
#当前eureka是客户端,所以只需要配置服务注册的地址
serviceUrl:
defaultZone : http://localhost:8761/eureka/
spring:
application:
name: client01
还有一个就是springboot的app启动类上面的注解
package com.zgd.springcloud.eurekaClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* eureka client 客户端
*/
@EnableEurekaClient
@SpringBootApplication
public class Client01App {
public static void main(String[] args) {
SpringApplication.run(Client01App.class, args);
}
}
运行起来可以看到eureka的管理页面多了一个client01
这样一个client,也就是一个服务生产者就在注册中心进行了注册
为了方便测试, 还可以写一个Controller方法
package com.zgd.springcloud.eurekaClient.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 服务提供者
* @author zgd
*/
@RestController
@RequestMapping("/client1")
public class ProducerController {
@Value("${server.port}")
String port;
@RequestMapping("/hello")
public String hello(@RequestParam String name){
return "hi "+name+",i am from port:" + port;
}
}
3.4 多个client客户端
在IDea的启动配置中,去除Sigle instance only
这个勾选
然后我们启动一个client , 改一下端口和application name, 再启动,就重复启动了两个不同端口的client,