zoukankan      html  css  js  c++  java
  • spring cloud 搭建(服务)

    创建服务:(我们创建2个服务,service1和service2。操作是一样的)

    选择Spring Initializr模板

    选择Spring Cloud Discovery-->Eureka Discovery Client

    pom文件(注意增加 spring-cloud-config-client )用来监听配置中心

    <?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.2.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.test</groupId>
        <artifactId>service1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>service1</name>
        <description>Demo project for Spring Boot</description>
     
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Hoxton.SR3</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-config-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-commons</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </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>

    添加服务注解:@EnableEurekaClient

    接着,我们创建一个controller文件夹,

    并且创建TestController类

    (service2也一样)

    @RestController
    @RequestMapping("test")
    public class TestController {
        @Value("${isDebug}")
        private String isDebug;
     
        @GetMapping(value = "/hello")
        public String hello(){
            return isDebug;
        }
    }

    application.yml

    server:
      port: 9040
     
    spring:
      application:
        name: service1
     
    eureka:
      client:
        service-url:
          defaultZone: ${registry.url}
      instance:
        lease-expiration-duration-in-seconds: 60
        lease-renewal-interval-in-seconds: 30
        preferIpAddress: true
        instanceId: ${spring.cloud.client.ip-address}:${server.port}

    bootstrap.yml(service2服务,我们选择prod作为配置文件)

    spring:
      http:
        encoding:
          charset: UTF-8
          enabled: true
          force: true
      cloud:
        config:
          uri: http://${host:localhost}:9020
          name: config
          profile: ${active:dev}
          # service2 我们采用prod的配置文件
          # profile: ${active:prod}

    然后,我们按照顺序启动。

    1 注册中心,配置中心

    2 service1,service2

    eureka

    我们看到,配置中心和服务都注册了。

    我们访问服务

    由于service1访问的是dev,

    service2访问的是prod,

    所以他们显示的信息不一样,这说明我们切换配置文件成功了。

  • 相关阅读:
    熟悉常用的HBase操作
    爬虫大作业
    熟悉常用的HDFS操作
    数据结构化与保存
    获取全部校园新闻
    爬取校园新闻首页的新闻的详情,使用正则表达式,函数抽离+网络爬虫基础练习
    中文词频统计
    英语词频统计
    AXIOS中文文档
    overload方法重载
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/12575858.html
Copyright © 2011-2022 走看看