一、注册中心搭建
- 引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- application.yml
server:
port: 3000
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defalutZone: "http://${eureka.instance.hostname}:{server.port}/eureka/"
spring:
application:
name: eureka
- springboot启动类
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
@EnableEurekaServer注解,启动
二、服务提供者
- 引入依赖
<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>
</dependency>
- application.yml
server:
port: 3001
eureka:
instance:
preferIpAddress: true
client:
serviceUrl:
defaultZone: http://localhost:3000/eureka ## 注册到 eureka
spring:
application:
name: single-provider ## 应用程序名称,后面会在消费者中用到
- 控制器
@Slf4j
@RestController
public class ProviderController {
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping(value = "/hello")
public String hello(){
List<String> services = discoveryClient.getServices();
for(String s : services){
log.info(s);
}
return "hello spring cloud!";
}
@RequestMapping(value = "/nice")
public String nice(){
List<String> services = discoveryClient.getServices();
for(String s : services){
log.info("gogogo" + s);
}
return "nice to meet you!";
}
}
- 启动类 @EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication
public class SingleProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SingleProviderApplication.class,args);
}
}
三、服务消费者
- 引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- application.yml
server:
port: 3002
eureka:
client:
serviceUrl:
defaultZone: http://localhost:3000/eureka ## 注册到 eureka
instance:
preferIpAddress: true
spring:
application:
name: single-customer
- 启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class singleCustomerApplication {
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(singleCustomerApplication.class,args);
}
}
- 服务消费
public class CustomerController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private HelloService helloService;
private static final String applicationName = "single-provider";
// feign实现
@RequestMapping(value = "feignRequest")
public Object feignRequest(){
return helloService.nice();
}
// restTemplate实现
@RequestMapping(value = "commonRequest")
public Object commonRequest(){
String url = "http://"+ applicationName +"/hello";
String s = restTemplate.getForObject(url,String.class);
return s;
}
}
- feign 接口
@FeignClient("single-provider")
public interface HelloService {
@RequestMapping("/nice")
String nice();
}
注册中心显示: