springcloud之eureka

新建eureka服务类

  1. 启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
//开启eureka服务
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

2 配置文件

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:${server.port}/eureka/

3.pom文件

    <dependencies>
        <dependency>
        <!--eureka需要的依赖-->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

建立订单服务

  1. 启动类
@SpringBootApplication
public class UserApplication {

//    启动负载均衡
    @LoadBalanced
    //获取远程调用模板
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(UserApplication.class,args);
    }
}
  1. 控制类
package com.chh.orderService.controller;


import com.chh.orderService.domain.Product;
import com.chh.orderService.domain.User;
import com.chh.orderService.service.UserService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    /*
    * 服务原子
    * */
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("test")
    public User result(){
        User result = userService.result();
        return result;
    }
    /*
    单机调用
    * */
/*    @GetMapping("product")
    public Product product(){
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance serviceInstance = instances.get(0);
        val host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        Product product = new Product();  product=restTemplate.getForObject("http://"+host+":"+port+"/product/test",Product.class);
        return product;
    }*/
    /*
    * 负载均衡调用
    * */
        @GetMapping("product")
        public Product product(){
            List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
            ServiceInstance serviceInstance = instances.get(0);
            val host = serviceInstance.getHost();
            int port = serviceInstance.getPort();
            Product product = new Product();
            
            product=restTemplate.getForObject("http://service-product/product/test",Product.class);
            return product;
        }

}

  1. pom文件
    <dependencies>
        <!--引入EurekaClient-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

5.文件配置

server:
  port: 9002 #端口
spring:
  cloud:
    loadbalancer:
      retry:
        enabled: true # 开启Spring Cloud的重试功能
  application:
    name: service-order #服务名称
#  datasource:
#    driver-class-name: com.mysql.cj.jdbc.Driver
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
#    username: root
#    password: 1213
#  jpa:
#    database: MySQL
#    show-sql: true
#    open-in-view: true
#配置Eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true #使用ip地址注册

生产者

1.控制类

package com.chh.orderService.controller;

import com.chh.orderService.domain.Product;

import com.chh.orderService.service.ProductService;

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("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("test")
    public Product result(){
        Product result = productService.result();
        return result;
    }

}

2.配置类

server:
  port: 9001 #端口
spring:
  cloud:
    loadbalancer:
      retry:
        enabled: true # 开启Spring Cloud的重试功能
  application:
    name: service-product #服务名称

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

3.pom文件

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

4.启动类正常的springboot启动就行

验证负载均衡

把消费再复制一份一模一样的工程,修改配置文件的启动端口号,服务名称不能修改,两个生产者的服务名一样都是product-service

上一篇:Eureka注册中心简介与详解


下一篇:CentOS安装Redis单实例