Eureka 是 Netflix 公司开源的一个服务注册与发现的组件 。 Eureka 和其他 Netflix 公司的服务组件(例如负载均衡、熔断器、网关等) 一起,被 Spring Cloud 社区整合为
Spring-Cloud-Netflix 模块。 Eureka 包含两个组件:Eureka Server (注册中心) 和 Eureka Client (服务提供者、服务消费者)。
问题:

当service A机器发生故障后,provider机器配置的ip发生变化,所有访问provider的机器的路径也要发生变化,需要重新配置一下,往往会有多个访问的比较麻烦

解决:

当Consumer需要访问Proavider时,直接向Registry(注册中心)发送请求,注册中心会返回指定Provider的IP


(1.创建一个空项目做父项目——>修改maven仓库为自己的仓库——>删除src文件(用不到)——>用maven新建两个模块分别是consumer和provider
示例:

(2. 在父亲项目中引入springboot环境和导入字体编码依赖
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.1.0.RELEASEversion>
- <relativePath/>
- parent>
-
-
- <properties>
- <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
- <java.version>1.8java.version>
- properties>
(3.分别在consumer和provider中引入web依赖
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- dependencies>
(4.编写启动类
- @SpringBootApplication
- public class ProviderApp {
- public static void main(String[] args){
- SpringApplication.run(ProviderApp.class,args);
- }
- }
(5.完成provider

- @RestController
- @RequestMapping("/goods")
- public class GoodsController {
- @Autowired
- private GoodsService goodsService;
-
- @GetMapping("/findOne/{id}")
- public Goods findOne(@PathVariable("id") int id){
- Goods goods = goodsService.findOne(id);
- return goods;
- }
- }
- @Repository
- public class GoodsDao {
- public Goods findOne(int id){
- return new Goods(1,"华为手机",3999,10000);
- }
- }
- @Service
- public class GoodsService {
- @Autowired
- private GoodsDao goodsDao;
-
- public Goods findOne(int id){
- return goodsDao.findOne(id);
- }
-
- }
- package com.gao.domain;
-
- public class Goods {
- private int id;
- private String title;
- private double price;
- private int count;
-
- public Goods(int id, String title, double price, int count) {
- this.id = id;
- this.title = title;
- this.price = price;
- this.count = count;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public double getPrice() {
- return price;
- }
-
- public void setPrice(double price) {
- this.price = price;
- }
-
- public int getCount() {
- return count;
- }
-
- public void setCount(int count) {
- this.count = count;
- }
-
- @Override
- public String toString() {
- return "Goods{" +
- "id=" + id +
- ", title='" + title + '\'' +
- ", price=" + price +
- ", count=" + count +
- '}';
- }
- }
- server:
- port: 8000
(6.完成consumer

- import com.gao.domain.Goods;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- //服务的调用方
- @RestController
- @RequestMapping("/order")
- public class OrderController {
-
- @GetMapping("/goods/{id}")
- public Goods findGoodsById(@PathVariable("id") int id){
- System.out.println("findGoodsById"+id);
-
- //远程调用Goods服务中的findOne接口
- return null;
-
- }
- }
- server:
- port: 9000

(1. 定义Bean restTemplate
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.client.RestTemplate;
-
- @Configuration
- public class RestTemplateConfig {
- @Bean
- public RestTemplate restTemplate(){
- return new RestTemplate();
- }
- }
(2.注入Bean
- @Autowired
- private RestTemplate restTemplate;
(3.调用方法
- String url = "http://localhost:8000/goods/findOne/"+id;
- //3.调用方法
- Goods goods = restTemplate.getForObject(url,Goods.class);
-
- return goods;
注意:Goods里需要有空参构造
通过访问consumer的路径,可以访问到provider即可说明成功。

网络三要素:协议 IP 端口