1.创建Discovery Server服务
//导入包,实际是通过Spring Initializr导入的,Eureka Server,DevTools,Actuactor
spring-boot-devtools
spring-cloud-starter-netflix-eureka-server
spring-boot-starter-actuator
//配置启动类注解EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class DiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryServerApplication.class, args);
}
}
//此时启动会报错,需要配置
spring.application.name=discovery-server
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
server.port=8761
2.创建Service服务
//导入包,实际是通过Spring Initializr导入的,Eureka Discovery,DevTools,Actuactor
spring-cloud-starter-netflix-eureka-client
spring-boot-starter-actuator
spring-boot-devtools
//配置启动类注解EnableDiscoveryClient
@EnableDiscoveryClient
@SpringBootApplication
@RestController
public class ServiceApplication {
@Value("${service.instance.name}")
private String instance;
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
@RequestMapping("/")
public String message(){
return "Hello from " + instance;
}
}
//配置文件
spring.application.name=service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
3.分别配置两个启动服务,每个配置端口以及节点名称,第一个为instance1 8081,第二个为instance2 8082
//第一个节点配置
service.instance.name= instance 1
server.port=8081
//第二个节点配置
service.instance.name= instance 2
server.port=8082
4.启动后,两个servic日志中会显示204,表示注册成功
2020-04-11 21:51:03.000 INFO 964 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SERVICE/localhost:service:8081: registering service...
2020-04-11 21:51:03.072 INFO 964 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SERVICE/localhost:service:8081 - registration status: 204
5.server端会显示两个节点注册成功
2020-04-11 21:51:03.070 INFO 899 --- [nio-8761-exec-5] c.n.e.registry.AbstractInstanceRegistry : Registered instance SERVICE/localhost:service:8081 with status UP (replication=false)
2020-04-11 21:51:03.595 INFO 899 --- [nio-8761-exec-6] c.n.e.registry.AbstractInstanceRegistry : Registered instance SERVICE/localhost:service:8081 with status UP (replication=true)
2020-04-11 21:52:11.887 INFO 899 --- [nio-8761-exec-3] c.n.e.registry.AbstractInstanceRegistry : Registered instance SERVICE/192.168.31.76:service:8082 with status UP (replication=false)
2020-04-11 21:52:12.408 INFO 899 --- [nio-8761-exec-5] c.n.e.registry.AbstractInstanceRegistry : Registered instance SERVICE/192.168.31.76:service:8082 with status UP (replication=true)
6.创建Client服务
//导入包,实际是通过Spring Initializr导入的,Eureka Discovery,DevTools,Actuactor
spring-cloud-starter-netflix-eureka-client
spring-boot-starter-actuator
spring-boot-devtools
//配置启动类注解EnableDiscoveryClient,以及后去eureka服务中注册的servic地址
@EnableDiscoveryClient
@SpringBootApplication
@RestController
public class ClientApplication {
@Autowired
private EurekaClient eurekaClient;
@Autowired
private RestTemplateBuilder restTemplateBuilder;
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
@RequestMapping("/")
public String callService(){
RestTemplate restTemplate = restTemplateBuilder.build();
InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("service",false);
String baseUrl = instanceInfo.getHomePageUrl();
ResponseEntity<String> reponse =
restTemplate.exchange(baseUrl,HttpMethod.GET,null,String.class);
return reponse.getBody();
}
}
//配置文件,其中设置不被发现所以设置为false
spring.application.name=client
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
eureka.client.register-with-eureka=false
//启动服务调用http://localhost:8080/可以每次访问到不同的两个service节点
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。