• springcloud(二):运行第一个Eureka程序


    1. Eureka

    1.1 关于Eureka

    为springcloud提供了Eureka服务端和客户端,主要用于服务管理。

    Eureka架构:

    1.2 搭建Eureka服务器

    创建一个项目ServerDemo:

    需要导入的jar包:

    <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.SR3</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka-server</artifactId>
            </dependency>
        </dependencies>

    分别是springcloud和eureka服务端的依赖包。

    在resources包下创建application.yml配置文件:

    server:
      port: 8761
    eureka:
      client: 
        register-with-eureka: false
        fetch-registry: false

    禁止自己当做服务注册

    register-with-eureka: false

    屏蔽注册信息

    fetch-registry: false

    在java包下创建ServerApp.java:

    package org.crazyit.cloud;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class ServerApp {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(ServerApp.class).web(true).run(args);
        }
    
    }

    @EnableEurekaServer:将项目作为SpringCloud中的注册中心。

    运行项目:

    这样我们服务器就搭建好了。

    1.3 建立服务提供者 

    创建项目ServerPolice。

    导入jar包:

    <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.SR3</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
        </dependencies>

    在resources包下创建application.yml配置文件:

    spring:
      application:
        name: first-police
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/

    创建PoliceServer.java文件:

    package org.crazyit.cloud;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class PoliceServer {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(PoliceServer.class).web(true).run(args);
        }
    
    }

    @EnableEurekaClient:与服务器配合向外提供注册与发现服务接口。

    创建实体类Police.java:

    package org.crazyit.cloud;
    
    public class Police {
    
        private Integer id;
        private String name;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
    }

    创建PoliceController.java:

    package org.crazyit.cloud;
    
    import org.springframework.http.MediaType;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class PoliceController {
    
        @RequestMapping(value = "/call/{id}", method = RequestMethod.GET, 
                produces = MediaType.APPLICATION_JSON_VALUE)
        public Police call(@PathVariable Integer id) {
            Police p = new Police();
            p.setId(id);
            p.setName("angus");
            return p;
        }
    }

    运行程序:

    可以看到多了一个叫first-police的服务。

    也可以直接调用这个服务:

    1.4  建立服务调用者

    创建项目PersonClient

    导入jar包:

    <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.SR3</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-ribbon</artifactId>
            </dependency>
        </dependencies>

    配置文件application.yml:

    server:
      port: 8081
    spring:
      application:
        name: first-person
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/

    创建PersonServer.java:

    package org.crazyit.cloud;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableEurekaClient
    public class PersonServer {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(PersonServer.class).web(true).run(args);
        }
    
    }

    创建TestController.java:

    package org.crazyit.cloud;
    
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.client.RestTemplate;
    
    @Controller
    @Configuration
    public class TestController {
        
        @Bean
        @LoadBalanced
        public RestTemplate getRestTemplate() {
            return new RestTemplate();
        }
    
        @GetMapping("/router")
        @ResponseBody
        public String router() {
            RestTemplate tpl = getRestTemplate();
            String json = tpl.getForObject("http://first-police/call/1", String.class);
            return json;
        }
    
    }

    运行程序:

    最终的程序结构:

  • 相关阅读:
    一次排查线上接口偶发异常耗时引起的思考!
    台阶很高,青蛙跳不跳?
    从零开始认识堆排序
    Redis SDS 深入一点,看到更多!
    偏见是怎么产生的?
    TCP 粘包拆包
    Netty中的这些知识点,你需要知道!
    心有 netty 一点通!
    服务化最佳实践
    职场的“诱惑”?
  • 原文地址:https://www.cnblogs.com/liuhui0308/p/13880833.html
Copyright © 2020-2023  润新知