一、前言
demo 级别
二、代码与结构
代码放在 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
(红色框的内容为 Provider 启动后才有的)
2、服务提供者
① 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 # 注册服务的时候使用服务的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、服务消费者
① ConsumerApplication
@EnableDiscoveryClient 开启发现服务功能、RestTemplate 使用负载均衡机制
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 //开启发现服务功能
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@LoadBalanced //使用负载均衡机制
@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(){
// 服务提供者url、返回数据类型
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 # 注册服务的时候使用服务的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