1、eureka 服務端配置web
一、pom依賴 spring
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>2.2.0.RELEASE</version> </dependency>
二、配置文件服務器
spring.application.name=springcloud-eureka-client server.port=8000 ## 取消服務器的自我註冊 eureka.client.register-with-eureka=false ## 不檢索服務 eureka.client.fetch-registry=false ## Eureka客戶端服務註冊地址 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
三、配置服務啓動app
@SpringBootApplication @EnableEurekaServer //開啓eureka服務 public class EuretaApplication { public static void main(String[] args) { SpringApplication.run(EuretaApplication.class, args); System.out.println("註冊服務中心啓動。。。。。。。"); } }
2、eureka 客戶端(註冊服務)負載均衡
一、pom依賴 spring-boot
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.2.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.0.RELEASE</version> </dependency>
二、配置文件fetch
spring.application.name=spring-cloud-client server.port=8004 ## Eureka客戶端服務註冊地址 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ ## 實現自我註冊 eureka.client.register-with-eureka=true ## 檢索服務 eureka.client.fetch-registry=true
三、代碼業務this
@RestController public class MemberApiContorller { @RequestMapping("/getMember") public String getMember(){ return "this is member,and server port is :"+serverport; } }
四、客戶端啓動註冊url
@SpringBootApplication @EnableEurekaClient //開啓客戶端註冊 public class EurekaCliApplication { public static void main(String[] args) { SpringApplication.run(EurekaCliApplication.class, args); } }
3、消費者rest
一、pom依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.0.RELEASE</version> </dependency>
二、配置文件
spring.application.name=spring-cloud-order server.port=8002 ## Eureka客戶端服務註冊地址 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ ## 實現自我註冊 eureka.client.register-with-eureka=true ## 檢索服務 eureka.client.fetch-registry=true
三、代碼業務
@RestController public class OrderController { @Autowired private RestTemplate restTemplate; @RequestMapping("/getOrder") public String getOrder(){ //說明:spring-cloud-client是註冊服務的id,不是域名! String url = "http://spring-cloud-client/getMember"; String result = restTemplate.getForObject(url, String.class); System.out.println("訂單服務返回:"+result); return result; } }
四、客戶端啓動註冊
@SpringBootApplication @EnableEurekaClient public class EurekaOrderApplication { public static void main(String[] args) { SpringApplication.run(EurekaOrderApplication.class, args); } //解決RestTemplate找不到 @Bean @LoadBalanced //解決負載均衡問題,若是不配置此註解,請求"http://spring-cloud-client/getMember",報找不到此路徑錯誤,加上此註解, // 自動找打服務名稱spring-cloud-client對應的服務,並返回正常的結果值 RestTemplate getRestTemplate(){ return new RestTemplate(); } }
注:初次研究,歡迎你們指正。