先建立父工程
..
..一路next
搭建注册中心(需要建立三个工程,端口不一样)
..
..
..
修改入口类
package com.cloud.eurekaserver1111;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class EurekaServer1111Application { public static void main(String[] args) { SpringApplication.run(EurekaServer1111Application.class, args); }}
修改属性文件(一共建立三个Eureka-Server服务,端口分别为1111,2222,3333)
server.port=1111eureka.instance.hostname=server.one.comeureka.client.fetch-registry=falseeureka.client.register-with-eureka=falseeureka.client.service-url.defaultZone=http://server.two.com:2222/eureka,http://server.three.com:3333/eureka
..
server.port=2222eureka.instance.hostname=server.two.comeureka.client.fetch-registry=falseeureka.client.register-with-eureka=falseeureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.three.com:3333/eureka
..
server.port=3333eureka.instance.hostname=server.three.comeureka.client.fetch-registry=falseeureka.client.register-with-eureka=falseeureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka
..
修改hosts文件(C:\Windows\System32\drivers\etc)
127.0.0.1 server.one.com127.0.0.1 server.two.com127.0.0.1 server.three.com
修改pom文件,把parent改成父工程
com.cloud cloud-parent-two 0.0.1-SNAPSHOT
建立服务提供者-8081
..
..
..
修改pom
com.cloud cloud-parent-two 0.0.1-SNAPSHOT
修改入口类:
package com.cloud.bookservice8081;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class BookService8081Application { public static void main(String[] args) { SpringApplication.run(BookService8081Application.class, args); }}
修改属性文件
server.port=8081# 服务名spring.application.name=BookService# 注册地址eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka# 注册名eureka.instance.instance-id=book-service:8081eureka.instance.prefer-ip-address=true
新建controller
package com.cloud.bookservice8081.controller;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@Slf4j@RestControllerpublic class BookController { @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/book") public String index(){ Listservices = discoveryClient.getServices(); services.forEach(e -> log.info("book-service:8081:" + e)); List list = discoveryClient.getInstances("BOOKSERVICEPROVIDER"); list.forEach(e -> { log.info("book-service:8081:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri()); }); return "{\n" + " \"bookName\": \"Apache Kafka实战\",\n" + " \"bookSize\": \"16开\",\n" + " \"pack\": \"平装\",\n" + " \"isbn\": \"9787121337765\",\n" + " \"publisher\": \"电子工业出版社\",\n" + " \"publishTime\": \"2018-05-01\",\n" + " \"service\": \"book-service:8081\"\n" + "}"; }}
建立服务提供者-8082
..
..
修改pom
com.cloud cloud-parent-two 0.0.1-SNAPSHOT
修改入口类
package com.cloud.bookservice8082;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClient@EnableDiscoveryClientpublic class BookService8082Application { public static void main(String[] args) { SpringApplication.run(BookService8082Application.class, args); }}
修改属性文件
server.port=8082# 服务名spring.application.name=BookService# 注册地址eureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka# 注册名eureka.instance.instance-id=book-service:8082eureka.instance.prefer-ip-address=true
增加controller
package com.cloud.bookservice8082.controller;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;@Slf4j@RestControllerpublic class BookController { @Autowired private DiscoveryClient discoveryClient; @RequestMapping("/book") public String index(){ Listservices = discoveryClient.getServices(); services.forEach(e -> log.info("book-service:8082:" + e)); List list = discoveryClient.getInstances("BOOKSERVICEPROVIDER"); list.forEach(e -> { log.info("book-service:8082:" + e.getServiceId() + "," + e.getHost() + "," + e.getPort() + "," + e.getUri()); }); return "{\n" + " \"bookName\": \"Apache Kafka实战\",\n" + " \"bookSize\": \"16开\",\n" + " \"pack\": \"平装\",\n" + " \"isbn\": \"9787121337765\",\n" + " \"publisher\": \"电子工业出版社\",\n" + " \"publishTime\": \"2018-05-01\",\n" + " \"service\": \"book-service:8082\"\n" + "}"; }}
最后建立消费者-8080
..
..
修改pom
com.cloud cloud-parent-two 0.0.1-SNAPSHOT
修改入口类(配置了@LoadBalanced注解的Bean)
package com.cloud.bookconsumer8080;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;@SpringBootApplication@EnableDiscoveryClient@EnableEurekaClientpublic class BookConsumer8080Application { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(BookConsumer8080Application.class, args); }}
属性文件
server.port=8080eureka.client.register-with-eureka=falseeureka.client.service-url.defaultZone=http://server.one.com:1111/eureka,http://server.two.com:2222/eureka,http://server.three.com:3333/eureka
controller
package com.cloud.bookconsumer8080.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestControllerpublic class BookController { private static final String PREFIX = "http://BOOKSERVICE"; // 微服务名字 @Autowired private RestTemplate restTemplate; @RequestMapping("consumeBook") public String index(){ return restTemplate.getForEntity(PREFIX + "/book",String.class).getBody(); }}
与上一节不同,这次指向的是服务名
目录结构
下面开始运行
访问 出现
可以看见当前Eureka连接另外两个Eureka,证明注册中心高可用集群搭建成功。
再看下面的服务,有两个,证明服务已经注册进来了
下面访问消费者
刷新
证明负载均衡也成功了