一、前言
demo 級別
二、代碼與結(jié)構(gòu)
代碼放在 github 上了:https://github.com/TheWays/springCloud.git
1、注冊中心

① RegisterApplication
僅添加注解:@EnableEurekaServer 的操作
package com.cun.register;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class RegisterApplication {
public static void main(String[] args) {
SpringApplication.run(RegisterApplication.class, args);
}
}
② application.yml 配置

③ pom 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
④ 效果:http://localhost:9500
(紅色框的內(nèi)容為 Provider 啟動后才有的)

2、服務(wù)提供者

① ProviderApplication (沒改過)
package com.cun.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
② MessageService
package com.cun.provider.service;
import org.springframework.stereotype.Service;
@Service
public class MessageService {
public String getMessage(){
return "ITAEM";
}
}
③ MessageController
package com.cun.provider.controller;
import com.cun.provider.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MessageController {
@Autowired
MessageService messageService;
@GetMapping("/get")
public String getMessage(){
return "provider提供信息:"+messageService.getMessage();
}
}
④ application.yml 配置
server:
port: 9400
spring:
application:
name: provider-message
eureka:
instance:
prefer-ip-address: true # 注冊服務(wù)的時候使用服務(wù)的ip地址
client:
service-url:
defaultZone: http://localhost:9500/eureka/
⑤ pom 依賴
<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>
</dependency>
⑥ 效果:http://localhost:9400/get

3、服務(wù)消費者

① ConsumerApplication
@EnableDiscoveryClient 開啟發(fā)現(xiàn)服務(wù)功能、RestTemplate 使用負(fù)載均衡機(jī)制
package com.cun.consumer;
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;
@EnableDiscoveryClient //開啟發(fā)現(xiàn)服務(wù)功能
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@LoadBalanced //使用負(fù)載均衡機(jī)制
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
② MessageController
package com.cun.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class MessageController {
@Autowired
RestTemplate restTemplate;
@GetMapping("/show")
public String showMessage(){
// 服務(wù)提供者url、返回數(shù)據(jù)類型
String s = restTemplate.getForObject("http://provider-message/get", String.class);
return "consumer獲取信息:"+s;
}
}
③ application.yml 配置
spring:
application:
name: consumer-message
server:
port: 9600
eureka:
instance:
prefer-ip-address: true # 注冊服務(wù)的時候使用服務(wù)的ip地址
client:
service-url:
defaultZone: http://localhost:9500/eureka/
④ pom 依賴
<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>
</dependency>
⑤ 效果:http://localhost:9600/show
