0011-Eureka服務端和客戶端單節點搭建

1. 工程結構

  • 父工程 spring-cloud-demo
  • 服務註冊中心 eureka-server-7001
  • 服務提供者 eureka-provider-8001

2. 父工程依賴

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-boot.version>2.0.6.RELEASE</spring-boot.version>
        <!--springboot與springcloud的版本對應 https://spring.io/projects/spring-cloud#overview-->
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--使用import可以實現多繼承-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <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>
        <finalName>SpringCloud</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

3. Eureka Server服務註冊中心

  • 接收客戶端註冊
  • 爲客戶端提供註冊列表

3.1 依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

3.2 yml配置

server:
  port: 7001

eureka:
  instance:
    hostname: eureka-server-7001
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://eureka-server-7001:7001/eureka/

3.3 主啓動類

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7001Application {

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

4. Eureka Provider服務提供者

  • 向服務端註冊
  • 服務續約

4.1 依賴

<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-actuator</artifactId>
        </dependency>
    </dependencies>

4.2 yml配置

server:
  port: 8001

eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka-server-7001:7001/eureka/
  instance:
    instance-id: eureka-provider-7001 # 服務名稱
    prefer-ip-address: true # 顯示ip地址

info: # 點擊註冊列表未服務出現的信息
  app.name: springcloud
  company.name: www.honor.com
  build.artifactId: @project.artifactId@
  build.version: @project.version@

spring:
  application:
    name: eureka-provider

4.3 服務內容

假設這是這個項目提供的服務, 注意service的接口抽出來在接口層存放,後面利用feign做服務調用的時候需要用到接口

// Controller
@RestController
public class HelloController {
    @Autowired
    private IHelloService helloService;

    @RequestMapping("/hello/{name}")
    public String sayHello(@PathVariable String name) {
        return helloService.sayHello(name);
    }
}

//ServiceImpl
@Service
public class HelloServiceImpl implements IHelloService {
    @Autowired
    private HttpServletRequest request;

    public String sayHello(String name) {
        return "Hello " + name + ", This port is " + request.getServerPort();
    }
}

4.4 主啓動類

@SpringBootApplication
@EnableEurekaClient
public class EurekaProvider8001Application {
    public static void main(String[] args) {
        SpringApplication.run(EurekaProvider8001Application.class, args);
    }
}

5. 驗證

http://eureka-server-7001:7001可以看見服務註冊列表

http://localhost:8001/mike可以正常提供服務

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章