• 第十四章:(3)Spring Boot 与 分布式 之 SpringCloud


    一、Eureka 注册中心

      1、pom.xml 配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.6.2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.njf</groupId>
        <artifactId>spring-boot-14-springcloud-eureka-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-boot-14-springcloud-eureka-server</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>2021.0.0</spring-cloud.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

      2、配置 Eureka 信息

        在application.yml 中配置 Eureka 的信息

    server:
      port: 8761
    
    eureka:
      instance:
        hostname: eureka-server #eureka 实例的主机名
    
      client:
        register-with-eureka: false  #不把eureka自己注册到eureka中
        fetch-registry: false        #不从eureka上获取服务的注册信息
        service-url:   #注册中心地址,服务注册地址
          defaultZone: http://localhost:8761/eureka

      3、使用 @EnableEurekaServer 启动 EurekaServer 注册中心

    /**
     * 注册中心
     * 1、配置 Eureka 信息
     * 2、@EnableEurekaServer 启动 EurekaServer 注册中心
     */
    @EnableEurekaServer
    @SpringBootApplication
    public class SpringBoot14SpringcloudEurekaServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBoot14SpringcloudEurekaServerApplication.class, args);
        }
    
    }

      4、启动注册中心

      可以看到,现在还没有任何可用的应用服务。 

    二、创建服务提供者,并注册

      1、pom.xml 配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.6.2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.njf</groupId>
        <artifactId>spring-boot-14-springcloud-provider</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-boot-14-springcloud-provider</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>2021.0.0</spring-cloud.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</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-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

      2、配置 Eureka 信息

    server:
      port: 8002
    
    spring:
      application:
        name: provider-ticket
    
    eureka:
      instance:
        prefer-ip-address: true   #注册服务的时候使用服务的 ip 地址进行注册
    
      client:
        service-url: #注册中心地址,服务注册地址
          defaultZone: http://localhost:8761/eureka

      3、提供服务

        TicketService:

    @Service
    public class TicketService {
    
        public String getTicket() {
            System.out.println("8002");
            return "《厉害了,我的国!》";
        }
    }

        TicketController:

    @RestController
    public class TicketController {
    
    
        @Autowired
        TicketService ticketService;
    
        @GetMapping(value = "/ticket")
        public String getTicket() {
            return ticketService.getTicket();
        }
    }

      4、将服务提供者分别打成两个jar包,端口分别为:8001与8002

        

      5、启动这两个服务,会注册到注册中心中

    三、创建服务消费者,并注册

      1、pom.xml 配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.6.2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.njf</groupId>
        <artifactId>spring-boot-14-springcloud-consumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>spring-boot-14-springcloud-consumer</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>2021.0.0</spring-cloud.version>
        </properties>
        <dependencies>
            <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>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

      2、配置 Eureka 信息

    server:
      port: 8200
    
    spring:
      application:
        name: consomer-user
    
    eureka:
      instance:
        prefer-ip-address: true   #注册服务的时候使用服务的 ip 地址进行注册
    
      client:
        service-url: #注册中心地址,服务注册地址
          defaultZone: http://localhost:8761/eureka

      

      3、开启服务发现

    @EnableDiscoveryClient  //开启发现服务功能
    @SpringBootApplication
    public class SpringBoot14SpringcloudConsumerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBoot14SpringcloudConsumerApplication.class, args);
        }
    
        @LoadBalanced  //使用负载均衡机制
        @Bean          // RestTemplate 发送 Http 请求
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }

      4、使用提供者的服务,使用 RestTemplate 来调用

    @RestController
    public class UserController {
    
    
        @Autowired
        RestTemplate restTemplate;
    
        @GetMapping(value = "/buy")
        public String buyTicket(String name) {
            String s = restTemplate.getForObject("http://provider-ticket/ticket", String.class);
            return name + "购买了" + s;
        }
    }

      5、测试

      服务提供者与使用者都注册在注册中心里

      

      发送请求:

        

      使用了负载均衡机制: 

  • 相关阅读:
    面试题:redis事务
    哲学家就餐问题
    面试题:Redis常见性能问题和解决方案?
    取势、明道、优术、践行、合众
    自主可控和开源
    关于独立思考
    警惕软件复杂度困局
    如何构建研发体系
    大数据整体链路
    银行数仓的建设实践
  • 原文地址:https://www.cnblogs.com/niujifei/p/15778967.html
Copyright © 2020-2023  润新知