springCloud

Spring Cloud Eureka

2017-09-23  本文已影响0人  叶小飞_

服务发现

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
@EnableEurekaServer //启用服务器的配置中心
@SpringBootApplication
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}
server.port=1000
eureka.client.service-url.defaultZone=http://eureka2:1000/eureka/
eureka.instance.hostname=eureka1
#客户端向服务器(注册中心发送心跳得时间间隔)
eureka.instance.lease-renewal-interval-in-seconds=10  
#服务器(注册中心)租期到期的时间, 也就是说服务器在收到最后一次心跳的时间上线
eureka.instance.lease-expiration-duration-in-seconds=120
Eureka.png

服务提供者 Provider

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
</dependencies>
@EnableDiscoveryClient    
@SpringBootApplication
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}
spring.application.name=provider-service
server.context-path=/provider
server.port=2001
eureka.client.service-url.defaultZone=http://eureka1:1000/eureka/
@RestController
public class IndexController {


    @RequestMapping(value = "/hello",method = {RequestMethod.GET})
    public String hello(){
        return "hello world";
    }
}

服务消费者 consumer

@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {

    @Bean
    @LoadBalanced //自动负载均衡 机制是通过 application name 去寻找服务发现 然后去做负载均衡
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}
@RestController
public class IndexController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/getAppNameUrl", method = {RequestMethod.GET})
    public String getApplicationNameUrl(){
         ResponseEntity<String> response  = restTemplate.getForEntity("http://provider-service/provider/hello",String.class);
        System.err.println("body:" + response.getBody());
        return "调用成功";
    }

    @RequestMapping(value = "/getUrl", method = {RequestMethod.GET})
    public String getUrl() {
        RestTemplate rest = new RestTemplate();
        ResponseEntity<String> response = rest.getForEntity("http://localhost:2001/provider/hello", String.class);
        System.err.println("body:" + response.getBody());
        return "调用成功";
    }
}
 http://localhost:2000/consumer/getUrl 调用P端服务是通过路径调用得
 http://localhost:2000/consumer/getAppNameUrl 是通过P 端得application name调用得

高可用Eureka服务注册中心

 在微服务架构环境中,一个非常关键部分就是高可用性,需要充分考虑发生故障得路径,必须对
 各个组件进行高可用,只需要建立两个Eureka服务,把两个注册中心地址相互配置到对方地址即可。
 在P、C端加上eureka.client.service-url.defaultZone=http://eureka1:1000/eureka/,http://eureka2:1001/eureka/
 当一个服务中心宕掉,还可以访问

服务发现注册机制

下节Ribbon

上一篇下一篇

猜你喜欢

热点阅读