- 使用idea新建一個maven項目 可以使用spring Initializr
- 替換pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yyl</groupId>
<artifactId>eureka-server-customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server-customer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 編寫啓動類
package com.yyl.customer;
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.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author yangyuanliang
*/
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServerCustomerApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaServerCustomerApplication.class, args);
}
}
- 測試controller
package com.yyl.customer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* author:yangyuanliang Date:2019-08-14 Time:17:39
**/
@RestController
public class CustomerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value = "/customer",method = RequestMethod.GET)
public String helloCustomer(){
return restTemplate.getForEntity("http://hello-service/hello",String.class).getBody();
}
}
- 編寫application.properties
server.port=8888
spring.application.name=customer
eureka.client.service-url.defaultZone=http://localhost:8672/eureka/,http://localhost:8673/eureka/
- 改造https://mp.csdn.net/postedit/99455466 服務提供者代碼或者下載
git clone https://github.com/Jacwo/eureka-server-provider.git
- 多節點啓動生產者即服務提供者
mvn package
//節點1
java -jar target/eureka-server-provider-0.0.1-SNAPSHOT.jar
//節點2
java -jar target/eureka-server-provider-0.0.1-SNAPSHOT.jar --spring.profiles.active=p1
- 啓動消費者 mvn spring-boot:run
- 開始測試 http://localhost:8888/customer 多次刷新查看生產者日誌