|
| 1 | +# Eureka 服务注册与发现的原理和简单使用 |
| 2 | + |
| 3 | +## 一、基本原理 |
| 4 | + |
| 5 | +现在有一个订单服务部署在一台机器上,另外有两个商品服务部署在两台机器上。 |
| 6 | + |
| 7 | +所有服务都注册到 eureka 上,当订单服务想要调用商品服务时,其实是从 eureka 上获取商品服务的地址。 |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +当有一个商品服务宕机后,eureka 会把这个服务的注册信息移除掉,订单服务也不会调用这个商品服务。 |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +下面我们简单使用下 Eureka。 |
| 16 | + |
| 17 | +> 场景:首先有一个服务 A,一个服务 B,服务 B 不知道服务 A 的 IP 地址,只知道服务 A 的名字,服务 B 通过向 eureka 获取到了服务 A 的具体 IP 地址,然后成功调用服务 A 的 passjava API。 |
| 18 | +
|
| 19 | +## 二、创建 Eureka Server |
| 20 | + |
| 21 | +创建一个 Eureka 服务。 |
| 22 | + |
| 23 | +Spring Cloud 基于 Hoxton.SR10 版本,Spring Boot 基于 2.3.2.RELEASE。 |
| 24 | + |
| 25 | +### pom.xml 文件 |
| 26 | + |
| 27 | +``` |
| 28 | + <parent> |
| 29 | + <groupId>org.springframework.boot</groupId> |
| 30 | + <artifactId>spring-boot-starter-parent</artifactId> |
| 31 | + <version>2.3.2.RELEASE</version> |
| 32 | + </parent> |
| 33 | + |
| 34 | + <dependencyManagement> |
| 35 | + <dependencies> |
| 36 | + <dependency> |
| 37 | + <groupId>org.springframework.cloud</groupId> |
| 38 | + <artifactId>spring-cloud-dependencies</artifactId> |
| 39 | + <version>Hoxton.SR10</version> |
| 40 | + <type>pom</type> |
| 41 | + <scope>import</scope> |
| 42 | + </dependency> |
| 43 | + </dependencies> |
| 44 | + </dependencyManagement> |
| 45 | + |
| 46 | + <dependencies> |
| 47 | + <dependency> |
| 48 | + <groupId>org.springframework.cloud</groupId> |
| 49 | + <artifactId>spring-cloud-starter</artifactId> |
| 50 | + </dependency> |
| 51 | + <dependency> |
| 52 | + <groupId>org.springframework.cloud</groupId> |
| 53 | + <artifactId>spring-cloud-starter-config</artifactId> |
| 54 | + </dependency> |
| 55 | + <dependency> |
| 56 | + <groupId>org.springframework.cloud</groupId> |
| 57 | + <artifactId>spring-cloud-starter-eureka</artifactId> |
| 58 | + </dependency> |
| 59 | + <dependency> |
| 60 | + <groupId>org.springframework.cloud</groupId> |
| 61 | + <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> |
| 62 | + </dependency> |
| 63 | +
|
| 64 | + </dependencies> |
| 65 | +``` |
| 66 | + |
| 67 | +### 启动类 |
| 68 | + |
| 69 | +```java |
| 70 | +/** |
| 71 | + * @Author: 公众号 | 悟空聊架构 |
| 72 | + * @Date: 2021/9/13 7:28 |
| 73 | + * @Site: www.passjava.cn |
| 74 | + * @Github: https://github.com/Jackson0714/eureka-learning |
| 75 | + */ |
| 76 | +@SpringBootApplication |
| 77 | +@EnableEurekaServer |
| 78 | +public class EurekaServerApplication { |
| 79 | + public static void main(String[] args) { |
| 80 | + SpringApplication.run(EurekaServerApplication.class, args); |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### 配置项, application.yml 文件 |
| 86 | + |
| 87 | +```yaml |
| 88 | +server: |
| 89 | + port: 8761 |
| 90 | +eureka: |
| 91 | + client: |
| 92 | + register-with-eureka: false |
| 93 | + fetch-registry: false |
| 94 | +``` |
| 95 | +
|
| 96 | +register-with-eureka: 不要把自己注册到 Eureka,因为它自己就是 Eureka 服务,让别人来注册的 |
| 97 | +
|
| 98 | +fetch-registry: 不要到 Euraka 服务抓取注册信息 |
| 99 | +
|
| 100 | +### 启动 |
| 101 | +
|
| 102 | +浏览器访问 http://localhost:8761/ |
| 103 | +
|
| 104 | + |
| 105 | +
|
| 106 | +## 三、创建服务 A |
| 107 | +
|
| 108 | +### pom.xml 文件 |
| 109 | +
|
| 110 | +``` |
| 111 | +<dependencyManagement> |
| 112 | + <dependencies> |
| 113 | + <dependency> |
| 114 | + <groupId>org.springframework.cloud</groupId> |
| 115 | + <artifactId>spring-cloud-dependencies</artifactId> |
| 116 | + <version>Hoxton.SR10</version> |
| 117 | + <type>pom</type> |
| 118 | + <scope>import</scope> |
| 119 | + </dependency> |
| 120 | + </dependencies> |
| 121 | +</dependencyManagement> |
| 122 | + |
| 123 | +<dependencies> |
| 124 | + <dependency> |
| 125 | + <groupId>org.springframework.boot</groupId> |
| 126 | + <artifactId>spring-boot-starter-web</artifactId> |
| 127 | + </dependency> |
| 128 | + <dependency> |
| 129 | + <groupId>org.springframework.cloud</groupId> |
| 130 | + <artifactId>spring-cloud-starter-config</artifactId> |
| 131 | + </dependency> |
| 132 | + <dependency> |
| 133 | + <groupId>org.springframework.cloud</groupId> |
| 134 | + <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> |
| 135 | + </dependency> |
| 136 | +</dependencies> |
| 137 | +``` |
| 138 | + |
| 139 | +### application.yml 配置 |
| 140 | + |
| 141 | +```yaml |
| 142 | +server: |
| 143 | + port: 8091 |
| 144 | +spring: |
| 145 | + application: |
| 146 | + name: ServiceA |
| 147 | +eureka: |
| 148 | + instance: |
| 149 | + hostname: localhost |
| 150 | + client: |
| 151 | + serviceUrl: |
| 152 | + defaultZone: http://localhost:8761/eureka |
| 153 | +``` |
| 154 | +
|
| 155 | +### Controller 类 |
| 156 | +
|
| 157 | +```java |
| 158 | +@RestController |
| 159 | +public class ServiceAController { |
| 160 | + |
| 161 | + @RequestMapping(value = "/passjava/{name}", |
| 162 | + method = RequestMethod.GET) |
| 163 | + public String sayHello(@PathVariable("name") String name) { |
| 164 | + |
| 165 | + return "hello" + name; |
| 166 | + } |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +### 启动类 |
| 171 | + |
| 172 | +```java |
| 173 | +/** |
| 174 | + * @Author: 公众号 | 悟空聊架构 |
| 175 | + * @Date: 2021/9/13 7:28 |
| 176 | + * @Site: www.passjava.cn |
| 177 | + * @Github: https://github.com/Jackson0714/eureka-learning |
| 178 | + */ |
| 179 | +@SpringBootApplication |
| 180 | +@EnableEurekaClient |
| 181 | +public class ServiceAApplication { |
| 182 | + |
| 183 | + public static void main(String[] args) { |
| 184 | + SpringApplication.run(ServiceAApplication.class, args); |
| 185 | + } |
| 186 | + |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +### 测试服务 A 是否已启动 |
| 191 | + |
| 192 | +Eureka 控制台可以看到服务 A 已启动 |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | +测试服务 A 是否能访问 |
| 197 | + |
| 198 | +```sh |
| 199 | +http://localhost:8005/passjava/悟空 |
| 200 | +``` |
| 201 | + |
| 202 | +返回结果: |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | +## 四、创建服务 B |
| 207 | + |
| 208 | +### 服务 B 和 服务 A 的区别 |
| 209 | + |
| 210 | +- 多了一个 netflix。 |
| 211 | + |
| 212 | +- 服务 B 会调用服务 A。 |
| 213 | + |
| 214 | +``` YAML |
| 215 | +<dependency> |
| 216 | + <groupId>org.springframework.cloud</groupId> |
| 217 | + <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> |
| 218 | +</dependency> |
| 219 | +``` |
| 220 | + |
| 221 | +### 服务 B 调用服务 A 的 passjava api |
| 222 | + |
| 223 | +```java |
| 224 | +public class ServiceBController { |
| 225 | + |
| 226 | + @Bean |
| 227 | + @LoadBalanced |
| 228 | + public RestTemplate getRestTemplate() { |
| 229 | + return new RestTemplate(); |
| 230 | + } |
| 231 | + |
| 232 | + @RequestMapping(value = "/test/{name}", method = RequestMethod.GET) |
| 233 | + public String test(@PathVariable("name") String name) { |
| 234 | + RestTemplate restTemplate = getRestTemplate(); |
| 235 | + return restTemplate.getForObject("http://ServiceA/passjava/" + name, String.class); |
| 236 | + } |
| 237 | + |
| 238 | +} |
| 239 | +``` |
| 240 | + |
| 241 | +我们看到有一个 @LoadBalanced 注解,这里做下说明: |
| 242 | + |
| 243 | +RestTemplate 用来实现单个 http 请求的,加了 @LoadBalanced 注解后,就可以实现负载均衡,访问 |
| 244 | + |
| 245 | +### 测试 |
| 246 | + |
| 247 | +启动服务 B,可以在 eureka 控制台看到服务 B 在注册列表上。 |
| 248 | + |
| 249 | + |
| 250 | + |
| 251 | +测试访问 test api |
| 252 | + |
| 253 | +```SH |
| 254 | +http://localhost:9001/test/悟空2 |
| 255 | +``` |
| 256 | + |
| 257 | +测试结果: |
| 258 | + |
| 259 | + |
| 260 | + |
| 261 | +说明服务 B 拿到了 服务 A 的 url 信息,然后成功调用服务 A。 |
| 262 | + |