Spring Cloud入门系列(三)——服务注册与发现之Eureka(已停更,建议切换到Nacos)
新增服务消费者80
ApplicationContextConfig
package com.banana.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @author layman
* @description: 配置类
* @date 2021/1/9
*/
@Configuration
public class ApplicationContextConfig {
/**
* Configuration注解:定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,
* 这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,
* 并用于构建bean定义,初始化Spring容器
* LoadBalanced注解:可以使RestTemplate在请求时拥有客户端负载均衡的能力,只有加上,才能通过微服务名称调用服务
* @return RestTemplate :rest微服务请求的模板类
*/
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
OrderController
package com.banana.springcloud.controller;
import com.banana.springcloud.entity.CommonResult;
import com.banana.springcloud.entity.Payment;
import com.banana.springcloud.loadbalancer.MyLoadBalancer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.net.URI;
import java.util.List;
/**
* @author layman
* @description: 消费者controller
* @date 2021/1/9
*/
@RestController
@Slf4j
@RequestMapping("/consumer/payment")
public class OrderController {
//服务调用地址
private static final String PAYMENT_URL = "http://cloud-payment-service";
@Resource
private RestTemplate restTemplate;
@GetMapping("/create")
public CommonResult<Payment> create(Payment payment){
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
}
@GetMapping("/get/{id}")
public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
OrderMain80
package com.banana.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
/**
* @author layman
* @description: TODO
* @date 2021/1/9
*/
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
appliction.yml
server:
port: 80
eureka:
client:
#是否将自己注册到注册中心, 默认true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,单机无所谓,集群必须设置为true配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka #单机版(将payment8001注册到7001)
# defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
spring:
application:
name: cloud-order-service
启动服务消费者80
刷新Eureka服务端。
可以看到 cloud-order-service 成功注册到7001 Eureka服务端。
Eureka集群
集群配置:略(无非就是你注册我,我注册你,非常简单。)