分佈式-SpringBoot-SpringCloud-Eureka

環境

  • Java version 1.8

  • SpringBoot version 2.1.7


搭建註冊中心 Eureka-server

  1. pom.xml 依賴以下所示:java

    <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
    
    </dependencies>
  2. 配置 Eureka application.propertiesweb

    # 應用啓動端口
    server.port=8090
    
    # 註冊中心管理中的 應用名稱
    spring.application.name=eureka-server
    
    # 登錄註冊管理中的的帳號密碼
    spring.security.user.roles=SUPERUSER
    spring.security.user.name=eureka
    spring.security.user.password=123456
    
    # 是否把本身註冊到註冊中心
    eureka.client.register-with-eureka=true
    
    # 是否從eureka上來獲取服務的註冊信息
    eureka.client.fetch-registry=false
    
    eureka.instance.hostname=localhost
    eureka.client.serviceUrl.defaultZone=http://eureka:123456@localhost:8090/eureka
  3. 啓動註冊中心 啓動後訪問(http://127.0.0.1:8090)spring

    1. 登錄界面

clipboard.png

2. 輸入設置的帳號密碼(user:eureka pwd:123456)

3. 進入註冊中心頁面

clipboard.png

  1. 須要注意的是 須要處理下CSRF否則服務提供者註冊不進來 啓動入口文件以下
  2. EurekaServiceApplication.javaapi

    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaServiceApplication.class, args);
        }
    
        //忽略 uri /eureka/** 的CSRF檢測
        @EnableWebSecurity
        static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().ignoringAntMatchers("/eureka/**");
                super.configure(http);
            }
        }
    }
  3. 簡單的註冊中心已經搭建完成

搭建服務提供者 Provider-service

  1. 首先仍是配置文件 application.propertiesapp

    服務端口
    server.port=8081
    
    # 服務應用名稱
    spring.application.name=provider-ticket
    
    # 是否容許使用ip鏈接
    eureka.instance.ip-address=true
    
    # 注意 http://用戶名:密碼@主機:端口/eureka 須要與服務中內心配置的同樣
    eureka.client.serviceUrl.defaultZone=http://eureka:123456@localhost:8090/eureka
  2. 啓動 項目後會自動把服務註冊到配置的服務中心
  3. 如下是我作的測試代碼

clipboard.png

  1. TicketController.java負載均衡

    package com.yourdream.providerticket.controller;
    
    import com.yourdream.providerticket.service.TicketService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class TicketController {
    
        @Autowired
        TicketService ticketService;
    
        @GetMapping("/ticket")
        public String getTicket(){
            System.out.println("server-8081");
            return ticketService.getTicket();
        }
    }
  2. TicketServiceide

    package com.yourdream.providerticket.service;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class TicketService {
    
        public String getTicket()
        {
            return "《哪吒之魔童降世》";
        }
    }
  3. ProviderTicketApplicationspring-boot

    package com.yourdream.providerticket;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class ProviderTicketApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ProviderTicketApplication.class, args);
        }
    
    }
  4. 到此一個簡單的服務提供者搭建完成

搭建服務消費者 Consumer-user

  1. 首先仍是配置文件 application.properties
  2. server.port=8100
    spring.application.name=consumer-user
    eureka.client.serviceUrl.defaultZone=http://eureka:123456@localhost:8090/eureka
    eureka.instance.ip-address=true
  3. 首先須要明確 消費者與服務提供者之間通信協議是HTTP
  4. 看下實例代碼

clipboard.png

  1. 啓動入口 ConsumerUserApplication.java測試

    package com.yourdream.consumeruser;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @EnableDiscoveryClient //開啓發現服務功能
    @SpringBootApplication
    public class ConsumerUserApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConsumerUserApplication.class, args);
        }
    
        @LoadBalanced //開啓負載均衡器
        @Bean //註冊 RestTemplate 服務
        public RestTemplate restTemplate()
        {
            return new RestTemplate();
        }
    }
  2. UserController.javafetch

    package com.yourdream.consumeruser.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    
    
    @RestController
    public class UserController {
    
        // 幫助咱們向 註冊的實例服務 發起http請求
        @Autowired
        RestTemplate restTemplate;
    
        @GetMapping("/buy")
        public String buyTicket(String name)
        {
            //PROVIDER-TICKET 服務提供者名稱
            String forObject = restTemplate.getForObject("http://PROVIDER-TICKET/ticket", String.class);
            return name + "購買了" + forObject;
        }
    }
  3. 啓動服務 訪問http://127.0.0.1:8100/buy
  4. 到此一個簡單的服務消費者搭建完成

原文
相關文章
相關標籤/搜索