使用Eureka的步骤

1.搭建Eureka Server

   1.1创建工程,导入依赖
 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 </dependency>
   1.2配置application.yml
server:
  port: 9000
#配置Eureka server
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false #是否将自己注册到注册中心
    fetch-registry: false #是否从eureka中获取注册信息
    service-url: #配置暴露给Eureka Client的请求地址
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
   1.3 配置启动类
@SpringBootApplication
@EnableEurekaServer  //激活Eureka Server
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

2.将服务提供者注册到Eureka Server上

   2.1 引入Eureka Client坐标
        <!--引入Eureka Client依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
   2.2 修改application.yml添加EurekaServer的信息
  #配置Eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/

  instance:
    prefer-ip-address: true #使用ip地址注册
   2.3 修改启动类,添加服务发现的支持(可选)
 */
@SpringBootApplication
@EntityScan("com.bjpowernode.product.entity")
@EnableEurekaClient   //激活Eureka Client
public class ProductApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class, args);
    }
}

3.服务消费者通过注册中心获取服务列表,并调用

   3.1 引入Eureka Client依赖
        <!--引入Eureka Client依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
   3,2 配置application.yml文件
#配置Eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/ #多个eurekaserver之间用,隔开
  instance:
    prefer-ip-address: true #使用ip地址注册
   3.3 修改启动类,添加服务发现的支持(可选)
@SpringBootApplication
@EntityScan("com.bjpowernode.eureka.entity")
@EnableDiscoveryClient  //激活Eureka Client
public class OrderApplication {

    /**
     * 使用spring提供的RestTemplate发送请求到商品服务
     *      1.创建RestTemplate对象交给容器管理
     *      2.在使用的时候,调用其他方法完成
     */

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }


    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}

   3.4 在服务调用者中的Controller层,注入DiscoveryClient
    /**注入DiscoveryClient : 
    	springcloud提供的获取元数据的工具类
    		调用方法获取服务的元数据
    */
    private DiscoveryClient discoveryClient;
   3.5 调用discoveryClient方法
@Controller
@RequestMapping("/order")
public class OrderController {

    //注入RestTemplate对象
    @Autowired
    private RestTemplate restTemplate;

    //注入DiscoveryClient : springcloud提供的获取元数据的工具类,调用方法获取服务的元数据
    private DiscoveryClient discoveryClient;

    @RequestMapping("/buy/{id}")
    @ResponseBody
    public Product findById(@PathVariable Long id){

        //调用discoveryClient方法
        // 根据服务名称获取服务提供者的实例
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");

        //获取第一个元数据
        ServiceInstance instance = instances.get(0);

        //获取主机名称和端口号
        String host = instance.getHost();
        int port = instance.getPort();

        //根据主机名称和端口号进行拼接
        Product product = restTemplate.getForObject("http://"+host+":"+port+"/product/"+id, Product.class);
        return product;
    }

}

    搞定!!!!!!!

发布了4 篇原创文章 · 获赞 0 · 访问量 64

猜你喜欢

转载自blog.csdn.net/weixin_45396356/article/details/104732828