springcloud 使用RabbitMq

新建一个项目,三个module 分别为eureka-server,config-server,config-client,

eureka-server 的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>

    <groupId>com.example</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <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>Dalston.SR4</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</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-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>


</project>
View Code

application.yml的配置如下:

server:
  port: 8889

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

一个简单的eureka server就写好了。

config-server: 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>com.example</groupId>
 7     <artifactId>config-server</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>config-server</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>1.5.8.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>Dalston.SR4</spring-cloud.version>
26     </properties>
27 
28     <dependencies>
29         <dependency>
30             <groupId>org.springframework.cloud</groupId>
31             <artifactId>spring-cloud-config-server</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.cloud</groupId>
35             <artifactId>spring-cloud-starter-eureka</artifactId>
36         </dependency>
37         <dependency>
38             <groupId>org.springframework.boot</groupId>
39             <artifactId>spring-boot-starter-test</artifactId>
40             <scope>test</scope>
41         </dependency>
42     </dependencies>
43 
44     <dependencyManagement>
45         <dependencies>
46             <dependency>
47                 <groupId>org.springframework.cloud</groupId>
48                 <artifactId>spring-cloud-dependencies</artifactId>
49                 <version>${spring-cloud.version}</version>
50                 <type>pom</type>
51                 <scope>import</scope>
52             </dependency>
53         </dependencies>
54     </dependencyManagement>
55 
56     <build>
57         <plugins>
58             <plugin>
59                 <groupId>org.springframework.boot</groupId>
60                 <artifactId>spring-boot-maven-plugin</artifactId>
61             </plugin>
62         </plugins>
63     </build>
64 
65 
66 </project>
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/zhaowenbo1234/springcloudconfig/  #配置git仓库地址
          search-paths: respo   #配置仓库路径  
      label: master        #配置仓库的分支
server:
  port: 8888
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8889/eureka/
application.yml

启动类

1 @SpringBootApplication
2 @EnableConfigServer
3 @EnableEurekaClient
4 public class ConfigServerApplication {
5 
6     public static void main(String[] args) {
7         SpringApplication.run(ConfigServerApplication.class, args);
8     }
9 }
ConfigServerApplication

config-client,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>com.example</groupId>
 7     <artifactId>config-client</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>config-client</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>1.5.8.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>Dalston.SR4</spring-cloud.version>
26     </properties>
27 
28     <dependencies>
29         <dependency>
30             <groupId>org.springframework.retry</groupId>
31             <artifactId>spring-retry</artifactId>
32         </dependency>
33         <dependency>
34             <groupId>org.springframework.cloud</groupId>
35             <artifactId>spring-cloud-starter-config</artifactId>
36         </dependency>
37         <dependency>
38             <groupId>org.springframework.boot</groupId>
39             <artifactId>spring-boot-starter-web</artifactId>
40         </dependency>
41         <dependency>
42             <groupId>org.springframework.boot</groupId>
43             <artifactId>spring-boot-starter-aop</artifactId>
44         </dependency>
45         <dependency>
46             <groupId>org.springframework.cloud</groupId>
47             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
48         </dependency>
49 
50 
51         <dependency>
52             <groupId>org.springframework.cloud</groupId>
53             <artifactId>spring-cloud-starter-eureka</artifactId>
54         </dependency>
55 
56         <dependency>
57             <groupId>org.springframework.boot</groupId>
58             <artifactId>spring-boot-starter-actuator</artifactId>
59         </dependency>
60         <dependency>
61             <groupId>org.springframework.boot</groupId>
62             <artifactId>spring-boot-starter-test</artifactId>
63             <scope>test</scope>
64         </dependency>
65     </dependencies>
66 
67     <dependencyManagement>
68         <dependencies>
69             <dependency>
70                 <groupId>org.springframework.cloud</groupId>
71                 <artifactId>spring-cloud-dependencies</artifactId>
72                 <version>${spring-cloud.version}</version>
73                 <type>pom</type>
74                 <scope>import</scope>
75             </dependency>
76         </dependencies>
77     </dependencyManagement>
78 
79     <build>
80         <plugins>
81             <plugin>
82                 <groupId>org.springframework.boot</groupId>
83                 <artifactId>spring-boot-maven-plugin</artifactId>
84             </plugin>
85         </plugins>
86     </build>
87 </project>
View Code
 1 spring:
 2   application:
 3     name: config-client
 4   cloud:
 5     config:
 6       label: master
 7       profile: dev
 8       discovery:
 9         enabled: true
10         service-id: config-server
11   rabbitmq:
12     host: localhost
13     port: 5672
14 
15 eureka:
16   client:
17     service-url:
18       defaultZone: http://localhost:8889/eureka/
19 server:
20   port: 8881
21 
22 management:
23   security:
24     enabled: false
bootstrap.yml
 1 @SpringBootApplication
 2 @RestController
 3 @EnableEurekaClient
 4 @RefreshScope
 5 public class ConfigClientApplication {
 6 
 7     public static void main(String[] args) {
 8         SpringApplication.run(ConfigClientApplication.class, args);
 9     }
10 
11     @Value("${foo}")
12     private String foo;
13 
14     @RequestMapping("/hi")
15     public String hi() {
16         return foo;
17     }
18 }
ConfigClientApplication

在使用RabbitMq的时候,由于我不明白RabbitMq,本地一开始没有安装RabbitMq。导致无法使用,连接不上 ,修改GitHub上的配置,在刷新后无法生效,找了一个安装RabbitMq的教程,安装好之后,解决了这个问题,记下这个失误,防止以后再犯。

http://localhost:8881/bus/refresh刷新,这个页面是post请求的 ,我用postman发送的请求,另外,/bus/refresh接口可以指定服务,即使用”destination”参数,比如 “/bus/refresh?destination=customers:**” 即刷新服务名为customers的所有服务,不管ip。

原文地址:https://www.cnblogs.com/zwb1234/p/7831602.html