建立父工程
新建Maven空项目
删除src文件夹
Eureka注册中心
新建Module SpringBoot项目 eureka-server
并导入Eureka Server
启动类添加
@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
application.yml
server:
port: 8080
spring:
application:
#注册名称
name: eureka-server
eureka:
instance:
hostname: 127.0.0.1
client:
#是否获取eureka服务器的注册信息,默认为true
fetch-registry: false
#是否注册到eureka服务器,默认为true
register-with-eureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
启动erueka-server
浏览器访问 http://localhost:8080/
服务提供者
新建Module SpringBoot项目 provider-server
并勾选Spring Web 、Eureka Discovery Client
启动类添加
@EnableEurekaClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ProviderServerApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderServerApplication.class, args);
}
}
application.yml
server:
port: 8181
spring:
application:
name: provider-server
eureka:
client:
#获取注册服务
fetch-registry: false
#注册在eureka上
register-with-eureka: true
service-url:
defaultZone: http://localhost:8080/eureka
添加Controller
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("learn")
public class MathController {
@RequestMapping("/math")
public String learnMath(){
return "学习数学";
}
}
启动服务
刷新Eureka
浏览器访问 http://localhost:8181/learn/math
消费者服务
新建Module SpringBoot项目 consumer-server
并勾选Spring Web 、Eureka Discovery Client
启动类添加
@EnableEurekaClient
application.yml
server:
port: 8282
spring:
application:
name: consumer-server
eureka:
client:
#获取注册服务
fetch-registry: true
#注册在eureka上
register-with-eureka: true
service-url:
defaultZone: http://localhost:8080/eureka
启动类:
这里调用接口提供两个方法,使用RestTemplate和OpenFeign
方法一:用RestTemplate调用
1.启动类添加 @EnableEurekaClient
2.注入 RestTemplate
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableEurekaClient
public class ConsumerServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerServerApplication.class, args);
}
@Bean
//客户端负载均衡
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
新建service
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class LearnService {
@Autowired
private RestTemplate restTemplate;
public String getMath() {
return restTemplate.getForObject("http://provider-server/learn/math", String.class);
}
}
新建controller
import com.gm.consumerserver.service.LearnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("learn")
public class LearnController {
@Autowired
private LearnService service;
@GetMapping("math")
public String math(){
return service.getMath();
}
}
启动consumer-server
刷新Eureka
浏览器输入http://localhost:8282/learn/math
方法二: OpenFeign
导入 spring-cloud-starter-openfeign
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
修改启动类:
添加 @EnableFeignClients
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerServerApplication.class, args);
}
// @Bean
//客户端负载均衡
// @LoadBalanced
// RestTemplate restTemplate() {
// return new RestTemplate();
// }
}
修改service,注意已修改为interface
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
@Service
@FeignClient(name = "provider-server",path = "/learn")
public interface LearnService {
// @Autowired
// private RestTemplate restTemplate;
//
// public String getMath() {
// return restTemplate.getForObject("http://provider-server/learn/math", String.class);
// }
@RequestMapping(value = "math")
String getMath();
}
重启该服务,继续访问http://localhost:8282/learn/math