spring boot+spring cloud+Eureka微服务demo
首先讲一个一定要注意的细节,spring boot和spring cloud的版本并不是完全兼容的。具体的兼容性可以参照官网兼容性查询如果不兼容就会出现以下错误1.编写Eureka服务microservices-discovery-eureka-demo1.1新建spring boot项目我这里用的idea新建spring boot项目,选择Spring Initial...
首先讲一个一定要注意的细节,spring boot和spring cloud的版本并不是完全兼容的。具体的兼容性可以参照官网
兼容性查询
如果不兼容就会出现以下错误
1.编写Eureka服务microservices-discovery-eureka-demo
1.1新建spring boot项目
我这里用的idea新建spring boot项目,选择Spring Initializr,大家也可以在https://start.spring.io上创建再导入本地即可。
大家根据实际情况修改group等相关信息。
这里注意右上角的spring boot的版本选择。
最后直接点击完成即可。
创建完后首先打开pom.xml看看,然后对照加入相关依赖,其中dependencyManagement可以从官网拷贝过来。http://projects.spring.io/spring-cloud/
注意选择的spring cloud和spring boot的兼容性。
<?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.dewey</groupId>
<artifactId>microservices-discovery-eureka-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>microservices-discovery-eureka-demo</name>
<description>eureka service</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.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>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后打开启动类并为其加上@EnableEurekaServer
package com.dewey.microservicesdiscoveryeurekademo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class MicroservicesDiscoveryEurekaDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MicroservicesDiscoveryEurekaDemoApplication.class, args);
}
}
然后配置eureka server,在application.yml文件中加入如下配置,其中registerWithEureka:false表示它不会自己像自己注册,fetchRegistry 为客户端是否获取eureka服务器注册表上的注册信息,默认为true,由于我们是本来就是编写eureka server,所以这里设置为flase不然启动会报错。registerWithEureka: false 和 fetchRegistry: false就代表这是一个eureka server。 当然这里也可以用系统生成的application.properties文件配置,但yaml文件的可读性要优于他,所以这里用了yaml文件配置。
server:
port: 8000
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
然后我们就可以启动该eureka server了,运行启动类后在浏览器访问https://localhsot:8000/就可以看到微服务查看面板了。
2.编写服务提供者microservices-provider-demo
按照上面的创建方式创建项目microservices-provider-demo,然后打开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.dewey</groupId>
<artifactId>microservice-provider-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>microservice-provider-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.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>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后打开启动类添加@EnableDiscoveryClient注解。
package com.dewey.microserviceproviderdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class MicroserviceProviderDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceProviderDemoApplication.class, args);
}
}
然后配置该服务的端口,注册的eureka server地址等。
spring:
application:
name: service-provider
server:
port: 8001
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/ #eureka服务注册地址
然后编写一个简单的controller。注意编写的controller一定要在启动类目录级别或下层。不然不会加载。
package com.dewey.microserviceproviderdemo.controller;
import org.springframework.web.bind.annotation.*;
@RestController
public class HelloWorld {
@GetMapping("/test/{id}")
public String test(@PathVariable String id){
return "hello"+id.toString();
}
}c
这时候启动该项目就可以在eureka server查看面板上看到该服务了。
3.编写服务提供者microservices-consumer-demo,使用ribbon。
按照上面的创建方式创建项目microservices-consumer-demo,然后打开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.dewey</groupId>
<artifactId>microservice-consumer-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>microservice-consumer-demo</name>
<description>consumer</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.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-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后找到启动类加上@EnableDiscoveryClient,并加入RestTemplate的bean,RestTemplate是spring用来操作rest资源的类,使用了模板模式。同时注意注解@LoadBalanced,只需要这个注解就可以为RestTemplate整合ribbon,从而实现负载均衡。而且eureka和ribbon配合使用时会将服务名自动映射成微服务的网络地址。使得大妈可伸缩性增强。具体见下面controller。
package com.dewey.microserviceconsumerdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class MicroserviceConsumerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerDemoApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
最后编写controller,这里restTemplate.getForObject中的url换成http://localhost:8001/test也是可以的,但是这样的话耦合度是比较高的,如果服务提供者的地址发生了变化那这个消费者就不能正常运行了。由于集成了ribbon,所以这里可以换成服务名。
package com.dewey.microserviceconsumerdemo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
@RestController
public class Test {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/test/{id}")
public String test(@PathVariable String id){
return this.restTemplate.getForObject("http://service-hello/test/"+id,String.class);
}
}
3.编写服务提供者microservices-consumer-feign-demo,使用feign。
使用restTemplate实现rest API调用时,在代码中拼接url使得代码不够优美,而且比较长的url会使拼接变得复杂,使用feign来定义接口只需要相应的注解,非常简单易读。便于维护。
首先修改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.dewey</groupId>
<artifactId>microservice-consumer-feign-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>microservice-consumer-feign-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.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>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建feign接口。@FeignClient中的name表示服务提供者的名称。注意这里不能用@GetMapping和@PostMapping等,不然会报错。大家可以试试。
package com.dewey.microserviceconsumerfeigndemo.feign;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name="service-provider")
public interface testFeignClient {
@RequestMapping(value = "/test/{id}",method = RequestMethod.GET)
public String test(@PathVariable("id") String id);
}
配置文件application.yml
spring:
application:
name: client-feign
server:
port: 8003
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
最后编写controller
package com.dewey.microserviceconsumerfeigndemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class MicroserviceConsumerFeignDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerFeignDemoApplication.class, args);
}
}
启动完成后我们可以通过http://localhost:8003/test/10来验证。
四个项目打包下载地址,下载即可体验微服务。
更多推荐
所有评论(0)