Eureka 二

一主要讲了一些关于Eureka 的概念,这一篇用代码去实现

服务端  高可用

pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>

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

配置文件

spring.application.name=microservice-discovery-eureka
server.port=8761
#设置为Eureka Server交互的地址,将8761这个Eureka服务端注册到8762上面 8762和8761一样 相互注册
eureka.client.service-url.defaultZone=http://localhost:8762/eureka/

启动类:

@SpringBootApplication
//开启Eureka 服务注册中心
@EnableEurekaServer
public class MicroserviceDiscoveryEurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args);
    }
}
8762是一样的,这样就完成了一个服务的高可用

客户端:

pom.xml

<!--eureka的客户端-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>
配置文件:

server.port=8082
#Eureka
spring.application.name=microservice-consumer-movie
#向两个服务端注册自己
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/,http://localhost:8762/eureka/
eureka.instance.prefer-ip-address=true

启动类:

@EnableDiscoveryClient
@SpringBootApplication
public class MicroserviceSimpleConsumnerMovieApplication {

    public static void main(String[] args) {
        SpringApplication.run(MicroserviceSimpleConsumnerMovieApplication.class, args);
    }
    
}

这样就完成了一个服务的注册,服务名为microservice-consumer-movie。

另一个服务:

server.port=8081
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springcloud?characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true
spring.datasource.username=root
spring.datasource.password=root
#eureka
#注册到Eureka Server 上的应用名称
spring.application.name=microservice-provider-user
#Eureka Server的地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/,http://localhost:8762/eureka/
#默认为false true是将自己的IP注册到Eureka Server  如果为false 则表示注册微服务所在操作系统的hostName
eureka.instance.prefer-ip-address=true

这样就完成了两个服务的注册,第二个服务名为microservice-provider-user

在未使用Eureka之前  服务consumer调用服务user

package com.yjp.microservicesimpleconsumnermovie.client.controller;

import com.yjp.microservicesimpleconsumnermovie.client.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MovieController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable int id) {
        return restTemplate.getForObject("http://localhost:8081/" + id, User.class);
    }
}
这样会有一个问题,我们是通过ip和端口号去调用的,就会有单点故障的问题,达不到我们的高可用要求

Eureka之后:

我们可以用服务的名称去调用

package com.yjp.microservicesimpleconsumnermovie.client.controller;

import com.yjp.microservicesimpleconsumnermovie.client.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MovieController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/user/{id}")
    public User findById(@PathVariable int id) {
        return restTemplate.getForObject("http://microservice-provider-user/" + id, User.class);
    }
}
但是发现这样调用是失败的,这就引出了第二个主键 Ribbon 。

在使用restTemplate的时候需要注意,首先需要在启动类中 将其实例化

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

努力吧,皮卡丘





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值