客户端注册到Eureka集群

前提Eureka集群已经创建号
1.创建客户端注册到Eureka集群eureka-server-ha-clent
2.创建客户端启动类和Controller

package com;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableEurekaClient
@SpringBootApplication(scanBasePackages = { "com.controller" }/* 包扫描 */)
@RibbonClient(name = "provider-user")
public class ConsumerMain {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}

public static void main(String[] args) {
SpringApplication app = new SpringApplication(ConsumerMain.class);
app.setBannerMode(Banner.Mode.OFF);// 打开关闭Banner
app.run(args);// 负责启动引导程序
}
}

package com.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
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;

import com.po.User;

@RestController
public class DemoController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;

@GetMapping("/getUser/{id}")
public User getUser(@PathVariable String id) {
System.out.println("+++++++++++++++");
return this.restTemplate.getForObject("http://provider-user/getUser/" + id, User.class);
}
@GetMapping("/test")
public String test() {
//调用不同的服务端,可以看出不同的负载策略
ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-provider-user");
System.out.println("111" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":"
+ serviceInstance.getPort());

ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("microservice-provider-user2");
System.out.println("222" + ":" + serviceInstance2.getServiceId() + ":" + serviceInstance2.getHost() + ":"
+ serviceInstance2.getPort());
return "1";
}
}

package com.po;

public class User {

private String id;
private String name;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int i) {
this.age = i;
}
}



3.创建配置文件
spring:
application:
name: consumer-movie
server:
port: 8089
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://peer1:8761/eureka,http://peer2:8762/eureka,http://peer3:8763/eureka
instance:
prefer-ip-address: true
4.查看浏览器

http://peer1:8761/
http://peer3:8763/
http://peer2:8762/

5.调起服务端

http://127.0.0.1:8089/getUser/1

 

posted @ 2018-09-29 11:12  两个馒头  阅读(455)  评论(0)    收藏  举报