• Spring Cloud练习1


    pom.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     4     <modelVersion>4.0.0</modelVersion>
     5 
     6     <groupId>org.jimmy</groupId>
     7     <artifactId>springclouddemo</artifactId>
     8     <version>0.0.1-SNAPSHOT</version>
     9     <packaging>jar</packaging>
    10 
    11     <name>SpringCloudDemo20180502</name>
    12     <description>Demo project for Spring Boot</description>
    13 
    14     <parent>
    15         <groupId>org.springframework.boot</groupId>
    16         <artifactId>spring-boot-starter-parent</artifactId>
    17         <version>2.0.1.RELEASE</version>
    18         <relativePath/> <!-- lookup parent from repository -->
    19     </parent>
    20 
    21     <properties>
    22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    24         <java.version>1.8</java.version>
    25         <spring-cloud.version>Finchley.RC1</spring-cloud.version>
    26     </properties>
    27 
    28     <dependencies>
    29         <dependency>
    30             <groupId>org.springframework.cloud</groupId>
    31             <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    32         </dependency>
    33 
    34         <dependency>
    35             <groupId>org.springframework.boot</groupId>
    36             <artifactId>spring-boot-starter-test</artifactId>
    37             <scope>test</scope>
    38         </dependency>
    39 
    40         <dependency>
    41             <groupId>org.springframework.cloud</groupId>
    42             <artifactId>spring-cloud-starter-eureka-server</artifactId>
    43         </dependency>
    44     </dependencies>
    45 
    46     <dependencyManagement>
    47         <dependencies>
    48             <dependency>
    49                 <groupId>org.springframework.cloud</groupId>
    50                 <artifactId>spring-cloud-dependencies</artifactId>
    51                 <version>${spring-cloud.version}</version>
    52                 <type>pom</type>
    53                 <scope>import</scope>
    54             </dependency>
    55         </dependencies>
    56     </dependencyManagement>
    57 
    58     <build>
    59         <plugins>
    60             <plugin>
    61                 <groupId>org.springframework.boot</groupId>
    62                 <artifactId>spring-boot-maven-plugin</artifactId>
    63             </plugin>
    64         </plugins>
    65     </build>
    66 
    67     <repositories>
    68         <repository>
    69             <id>spring-milestones</id>
    70             <name>Spring Milestones</name>
    71             <url>https://repo.spring.io/milestone</url>
    72             <snapshots>
    73                 <enabled>false</enabled>
    74             </snapshots>
    75         </repository>
    76     </repositories>
    77 
    78 </project>
    application-server.yml
    代码如下:
    eureka:
      client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
    defaultZone: http://localhost:8761/eureka/
    server:
    port: 8761
    spring:
    application:
    name: service-hi

    application.yml

    代码如下:

    spring:
    profiles:
    active: server

    application-client.yml
    代码如下:
    eureka:
    client:
    serviceUrl:
    defaultZone: http://localhost:8761/eureka/
    server:
    port: 8762
    spring:
    application:
    name: service-hi

    Config.java
     1 package org.jimmy;
     2 
     3 import org.springframework.cloud.client.loadbalancer.LoadBalanced;
     4 import org.springframework.context.annotation.Bean;
     5 import org.springframework.context.annotation.Configuration;
     6 import org.springframework.web.client.RestTemplate;
     7 
     8 /**
     9  * Created by Administrator on 2018/5/2 0002.
    10  */
    11 @Configuration
    12 public class Config {
    13     @LoadBalanced
    14     @Bean
    15     public RestTemplate restTemplate() {
    16         return new RestTemplate();
    17     }
    18 }

    ServiceHiApplication.java

     1 package org.jimmy;
     2 
     3 import org.springframework.beans.factory.annotation.Value;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.autoconfigure.SpringBootApplication;
     6 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
     7 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
     8 import org.springframework.web.bind.annotation.RequestMapping;
     9 import org.springframework.web.bind.annotation.RequestParam;
    10 
    11 @SpringBootApplication
    12 @EnableEurekaServer
    13 public class ServiceHiApplication  {
    14     public static void main(String[] args) throws Exception {
    15         SpringApplication.run(ServiceHiApplication.class, args);
    16     }
    17 
    18 }

    ClientHiApplication.java

     1 package org.jimmy;
     2 
     3 import org.springframework.beans.factory.annotation.Value;
     4 import org.springframework.boot.SpringApplication;
     5 import org.springframework.boot.autoconfigure.SpringBootApplication;
     6 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.RequestParam;
     9 import org.springframework.web.bind.annotation.RestController;
    10 
    11 /**
    12  * Created by Administrator on 2018/5/3 0003.
    13  */
    14 @SpringBootApplication
    15 @EnableDiscoveryClient
    16 @RestController
    17 public class ClientHiApplication {
    18 
    19     public static void main(String[] args) {
    20         SpringApplication.run(ClientHiApplication.class, args);
    21     }
    22 
    23     @Value("${server.port}")
    24     String port;
    25     @RequestMapping("/hi")
    26     public String home(@RequestParam String name) {
    27         System.out.println("name:" + name + ",port:" + port);
    28         return "hi "+name+",i am from port:" +port;
    29     }
    30 }

    第一步,运行ServiceHiApplication.java.服务可以启动了.

    第二步,将

    application.yml

    改为如下:

    spring:
    profiles:
    active: client
    之前的Server是服务端,此时再启动一个客户端.(server对应application-server.yml,client对应application-client.yml)
    运行ClientHiApplication.java.客户端可以启动了.
    在Web浏览器(Chrome,FireFox等)中访问url:

    http://localhost:8762/hi?name=Dawn

    如果页面显示:hi Dawn,i am from port:8762
    说明代码没有问题,可以成功访问了.
    2015年10月-2016年3月 总计:5个月.
    2016年11月-2017年6月 总计:7个月.
    2017年7月-2018年4月 总计:9个月.
    2018年5月-2018年5月 总计:1个月.
    2018年6月-2018年12月 总计:6个月.
    2019年1月-2019年12月 总计11个月.
    2020年2月-2021年2月 总计13个月.
    所有总计:5+7+9+1+6+11+13=52个月(4年4个月).
    本人认同二元论.我是理想主义者,现实主义者,乐观主义者,有一定的完美主义倾向.不过,一直都是咸鱼(菜鸟),就算有机会,我也不想咸鱼翻身.(并不矛盾,因为具体情况具体分析)
    英语,高等数学,考研,其他知识学习打卡交流QQ群:946556683
  • 相关阅读:
    【2020-05-17】人生十三信条
    【2020-05-16】评价与骄傲
    【2020-05-15】每天都充满向上的激情
    【04NOIP普及组】火星人(信息学奥赛一本通 1929)(洛谷 1088)
    next_permutation(全排列算法)
    【03NOIP普及组】麦森数(信息学奥赛一本通 1925)(洛谷 1045)
    快速幂
    【03NOIP普及组】栈(信息学奥赛一本通 1924)(洛谷 1044)
    【06NOIP普及组】数列(信息学奥赛一本通 1937)(洛谷 1062)
    【00NOIP普及组】计算器的改良(信息学奥赛一本通 1910)(洛谷 1022)
  • 原文地址:https://www.cnblogs.com/JimmySeraph/p/8984958.html
Copyright © 2020-2023  润新知