• SpringCloud的学习记录(2)


    这一章节主要讲如何搭建eureka-client项目.

    在我们生成的Demo项目上右键点击New->Module->spring Initializr, 然后next, 填写GroupArtifact等信息,

    这里Artifact填写eurekaclient, 再次next,

    这里选择的Web下的webCloud Discovery下的Eureka Discovery.

    最后在Module Name中填写eureka-client.

    生成的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 http://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.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.xum</groupId>
        <artifactId>eureka-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>eureka-client</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.RELEASE</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>
        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
            </repository>
        </repositories>
    </project>

    然后在EurekaClientApplication上加@EnableEurekaClient注解:

    package com.xum.eurekaclient;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @EnableEurekaClient
    @SpringBootApplication
    public class EurekaClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(EurekaClientApplication.class, args);
        }
    
    }

    然后新建application.yml文件, 内容如下:

    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/    这里的defaultZone是上一章节的对应eureka-server
    server:
      port: 8762
    spring:
      application:
        name: eureka-client

    然后写一个controller去获取数据, 这里是获取git上的数据, 关于config-server的项目下一章节说

    package com.xum.eurekaclient.controller;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.client.ServiceInstance;
    import org.springframework.cloud.client.discovery.DiscoveryClient;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.lang.reflect.Method;
    import java.util.List;
    
    @RefreshScope   //这里加上RefreshScope注解,这样才能获取到git上的内容
    @RestController
    @RequestMapping(value = "/testone")
    public class TestOneController {
    
        private static final Logger LOG = LoggerFactory.getLogger(TestOneController.class);
    
        @Autowired
        private DiscoveryClient discoveryClient;
    
        @Value("${server.port}")
        String port;
    
        /* from git config */
        @Value("${democonfigclient.message}")
        String message;
    
        /* from git config */
        @Value("${foo}")
        String foo;
    
        @RequestMapping(value = "/test")
        public String test(@RequestParam(value = "name", required = false, defaultValue = "testOneClient")String name) {
            /*List<ServiceInstance> list = discoveryClient.getInstances("eureka-client");
            LOG.info("discoveryClient.getServices().size() = " + discoveryClient.getServices().size());
            for( String s :  discoveryClient.getServices()){
                List<ServiceInstance> serviceInstances =  discoveryClient.getInstances(s);
                for(ServiceInstance si : serviceInstances){
                    LOG.info("services:" + s + ":getHost()=" + si.getHost());
                    LOG.info("services:" + s + ":getPort()=" + si.getPort());
                    LOG.info("services:" + s + ":getServiceId()=" + si.getServiceId());
                    LOG.info("services:" + s + ":getUri()=" + si.getUri());
                }
            }*/
            String info = "Hi " + name + ", this is EurekaClient, port is " + port;
            return info;
        }
    
        @RequestMapping(value = "/config", method = RequestMethod.GET)
        public String config() {
            String info = "port:" + port + ",message:" + message + ",foo:" + foo;
            return info;
        }
    }

    最后通过Run DashBoard运行项目,

    首先运行上一章节的eureka-server项目, 然后运行eureka-client的项目.

    在浏览器上输入http://localhost:8761,显示如下: (我这里同时运行了config-server项目, 这个项目下一章节讲)

    先在浏览器中输入http://localhost:8762/testone/test, 显示如下

    port是eureka-client的端口

    然后在浏览器中输入http://localhost:8762/testone/config, 显示如下

    port是eureka-client的端口, message和foo是从git仓库中获取的, 是通过config-server项目获取的.

    下一章节讲config-server项目的搭建.

  • 相关阅读:
    Cmakelist.txt
    rename 后缀
    vector list array deque
    Primary Expression
    9 css中通用选择器的用法
    8 css中包含选择器的用法
    7 css中子选择器的用法
    6 id选择器的用法
    5 css类选择器的使用
    4 css外部链接式写法
  • 原文地址:https://www.cnblogs.com/xumBlog/p/10606576.html
Copyright © 2020-2023  润新知