一、springboot集成springcloud
创建maven项目作为父工程cloud-parent,并在父工程下创建两个springboot项目的module:cloud-eureka(注册中心)、cloud-eureka2(注册中心2)、cloud-server(服务端),cloud-client(客户端)项目结构如下:
二、配置cloud-eureka
1、pom文件(创建工程勾选Cloud Discovery-->Eureka Server)
复制代码 4.0.0 org.springframework.boot spring-boot-starter-parent 1.5.18.RELEASE com.wxx.demo cloud-server 0.0.1-SNAPSHOT jar cloud-server Demo project for Spring Boot 1.8 Edgware.SR5 org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
2、application.properties配置文件
server.port=8096eureka.client.registerWithEureka=falseeureka.client.fetchRegistry=falseeureka.client.serviceUrl.defaultZone=http://localhost:8098/eureka/ #配置eureka2的注册中心地址做集群处理复制代码
3、启动类配置
package com.wxx.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServer//作为Eureka注册中心public class CloudServerApplication { public static void main(String[] args) { SpringApplication.run(CloudServerApplication.class, args); }}复制代码
4、启动服务端并访问:http://localhost:8096/
5、配置cloud-eureka2同上把注册地址修改为cloud-eureka地址
三、配置cloud-client
1、pom文件同上配置
复制代码 4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.1.RELEASE com.wxx.demo cloud-client 0.0.1-SNAPSHOT cloud-client Demo project for Spring Boot 1.8 Greenwich.RC2 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-server com.wxx.demo cloud-server 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin spring-milestones Spring Milestones https://repo.spring.io/milestone
2、application.properties配置文件
server.port=8090spring.application.name=cloud-clienteureka.client.serviceUrl.defaultZone=http://localhost:8096/eureka/ #eureka.client.serviceUrl.defaultZone=http://localhost:8096/eureka/,http://localhost:8096/eureka/ 服务注册地址两种写法都可以复制代码
3、启动类配置
package com.wxx.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClient//Eureka客户端注解public class CloudClientApplication { public static void main(String[] args) { SpringApplication.run(CloudClientApplication.class, args); }}复制代码
4、客户端代码
package com.wxx.demo.client;import com.wxx.demo.service.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class DemoClient { @Autowired private DemoService demoService; @GetMapping("/hello") public String sayHello(@RequestParam String hello){ return demoService.sayHello(hello); }}复制代码
5、启动客户端项目访问:http://localhost:8090/hello?hello=%22spring-cloud%22
查看服务注册情况